diff options
author | Michael Bleigh <mbleigh@mbleigh.com> | 2013-02-26 14:10:37 -0800 |
---|---|---|
committer | Michael Bleigh <mbleigh@mbleigh.com> | 2013-02-26 14:10:37 -0800 |
commit | 67c0bc692106ff55701bfb33f55924d5b118fdb7 (patch) | |
tree | 156ce2f81a8238e189ea400befe06dd09f787e04 | |
parent | 328cf56de6c39ffea06770207f21ea722b9497a0 (diff) | |
parent | 0a46fe360afb8b7dcd92d42aaa3177940d25c81b (diff) | |
download | hashie-67c0bc692106ff55701bfb33f55924d5b118fdb7.tar.gz |
Merge pull request #85 from cromulus/reinstant_symbolize_keys
reinstating symbolize keys in the to_hash method.
-rw-r--r-- | lib/hashie/hash.rb | 4 | ||||
-rw-r--r-- | spec/hashie/mash_spec.rb | 13 |
2 files changed, 10 insertions, 7 deletions
diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb index 3a6ad52..c36bb91 100644 --- a/lib/hashie/hash.rb +++ b/lib/hashie/hash.rb @@ -8,15 +8,17 @@ module Hashie include HashExtensions # Converts a mash back to a hash (with stringified keys) - def to_hash + def to_hash(options={}) out = {} keys.each do |k| if self[k].is_a?(Array) + k = options[:symbolize_keys] ? k.to_sym : k.to_s out[k] ||= [] self[k].each do |array_object| out[k] << (Hash === array_object ? array_object.to_hash : array_object) end else + k = options[:symbolize_keys] ? k.to_sym : k.to_s out[k] = Hash === self[k] ? self[k].to_hash : self[k] end end diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index 6cd3994..049c04a 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -317,37 +317,38 @@ describe Hashie::Mash do converted.to_hash["a"].first.is_a?(Hashie::Mash).should be_false converted.to_hash["a"].first.is_a?(Hash).should be_true converted.to_hash["a"].first["c"].first.is_a?(Hashie::Mash).should be_false + converted.to_hash({:symbolize_keys => true}).keys[0].should == :a end end - + describe "#fetch" do let(:hash) { {:one => 1} } let(:mash) { Hashie::Mash.new(hash) } - + context "when key exists" do it "returns the value" do mash.fetch(:one).should eql(1) end - + context "when key has other than original but acceptable type" do it "returns the value" do mash.fetch('one').should eql(1) end end end - + context "when key does not exist" do it "should raise KeyError" do error = RUBY_VERSION =~ /1.8/ ? IndexError : KeyError expect { mash.fetch(:two) }.to raise_error(error) end - + context "with default value given" do it "returns default value" do mash.fetch(:two, 8).should eql(8) end end - + context "with block given" do it "returns default value" do mash.fetch(:two) {|key| |