Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Hashie #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/hashie/hash/rash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'hashie/hash'
require 'hashie/mash'

module Hashie
class Hash
class Rash < Hashie::Mash

protected

def convert_key(key) #:nodoc:
underscore_string(key.to_s)
end

# Unlike its parent Mash, a Rash will convert other Hashie::Hash values to a Rash when assigning
# instead of respecting the existing subclass
def convert_value(val, duping=false) #:nodoc:
case val
when self.class
val.dup
when ::Hash
val = val.dup if duping
self.class.new(val)
when Array
val.collect{ |e| convert_value(e) }
else
val
end
end

# converts a camel_cased string to a underscore string
# subs spaces with underscores, strips whitespace
# Same way ActiveSupport does string.underscore
def underscore_string(str)
str.to_s.strip.
gsub(' ', '_').
gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
squeeze("_").
downcase
end

end
end
end
43 changes: 0 additions & 43 deletions lib/hashie/rash.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rash.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'hashie/rash'
require 'hashie/hash/rash'
2 changes: 1 addition & 1 deletion rash.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|

s.version = Rash::VERSION

s.add_dependency 'hashie', '~> 2.0.0'
s.add_dependency 'hashie', '~> 3.0'
s.add_development_dependency 'rake', '~> 0.9'
s.add_development_dependency 'rdoc', '~> 3.9'
s.add_development_dependency 'rspec', '~> 2.5'
Expand Down
18 changes: 9 additions & 9 deletions spec/rash_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'spec_helper'

describe Hashie::Rash do
describe Hashie::Hash::Rash do
subject {
Hashie::Rash.new({
Hashie::Hash::Rash.new({
"varOne" => 1,
"two" => 2,
:three => 3,
Expand All @@ -23,19 +23,19 @@
})
}

it { should be_a(Hashie::Mash) }
it { should be_a(Hashie::Hash) }

it "should create a new rash where all the keys are underscored instead of camelcased" do
subject.var_one.should == 1
subject.two.should == 2
subject.three.should == 3
subject.var_four.should == 4
subject.five_hump_humps.should == 5
subject.nested.should be_a(Hashie::Rash)
subject.nested.should be_a(Hashie::Hash::Rash)
subject.nested.nested_one.should == "One"
subject.nested.two.should == "two"
subject.nested.nested_three.should == "three"
subject.nested_two.should be_a(Hashie::Rash)
subject.nested_two.should be_a(Hashie::Hash::Rash)
subject.nested_two.nested_two.should == 22
subject.nested_two.nested_three.should == 23
subject.spaced_key.should == "When would this happen?"
Expand Down Expand Up @@ -64,7 +64,7 @@

merged.nested.four_times.should == "a charm"
merged.nested.fourTimes.should == "a charm"
merged.nested3.should be_a(Hashie::Rash)
merged.nested3.should be_a(Hashie::Hash::Rash)
merged.nested3.hello_world.should == "hi"
merged.nested3.helloWorld.should == "hi"
merged[:nested3][:helloWorld].should == "hi"
Expand All @@ -78,7 +78,7 @@

subject.nested.four_times.should == "a charm"
subject.nested.fourTimes.should == "a charm"
subject.nested3.should be_a(Hashie::Rash)
subject.nested3.should be_a(Hashie::Hash::Rash)
subject.nested3.hello_world.should == "hi"
subject.nested3.helloWorld.should == "hi"
subject[:nested3][:helloWorld].should == "hi"
Expand All @@ -92,7 +92,7 @@

merged.nested.four_times.should == "work like a charm"
merged.nested.fourTimes.should == "work like a charm"
merged.nested3.should be_a(Hashie::Rash)
merged.nested3.should be_a(Hashie::Hash::Rash)
merged.nested3.hello_world.should == "hi"
merged.nested3.helloWorld.should == "hi"
merged[:nested3][:helloWorld].should == "hi"
Expand All @@ -101,7 +101,7 @@
it "should handle assigning a new Hash and convert it to a rash" do
subject.nested3 = {:helloWorld => "hi"}

subject.nested3.should be_a(Hashie::Rash)
subject.nested3.should be_a(Hashie::Hash::Rash)
subject.nested3.hello_world.should == "hi"
subject.nested3.helloWorld.should == "hi"
subject[:nested3][:helloWorld].should == "hi"
Expand Down