diff --git a/lib/hashie/hash/rash.rb b/lib/hashie/hash/rash.rb new file mode 100644 index 0000000..0f7ae59 --- /dev/null +++ b/lib/hashie/hash/rash.rb @@ -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 diff --git a/lib/hashie/rash.rb b/lib/hashie/rash.rb deleted file mode 100644 index a341517..0000000 --- a/lib/hashie/rash.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'hashie/mash' - -module Hashie - class Rash < 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 diff --git a/lib/rash.rb b/lib/rash.rb index 903a924..1703ec8 100644 --- a/lib/rash.rb +++ b/lib/rash.rb @@ -1 +1 @@ -require 'hashie/rash' \ No newline at end of file +require 'hashie/hash/rash' \ No newline at end of file diff --git a/rash.gemspec b/rash.gemspec index eae55ce..09554b4 100644 --- a/rash.gemspec +++ b/rash.gemspec @@ -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' diff --git a/spec/rash_spec.rb b/spec/rash_spec.rb index c66d535..b13269f 100644 --- a/spec/rash_spec.rb +++ b/spec/rash_spec.rb @@ -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, @@ -23,7 +23,7 @@ }) } - 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 @@ -31,11 +31,11 @@ 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?" @@ -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" @@ -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" @@ -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" @@ -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"