diff options
author | Daniel Neighman <has.sox@gmail.com> | 2010-06-15 14:16:25 +0800 |
---|---|---|
committer | Michael Bleigh <michael@intridea.com> | 2010-06-22 22:00:39 +0800 |
commit | 0c85e9482f0ca0232d7cf7cf6577a359ea07ffed (patch) | |
tree | 34a2dd820c3e78721b3931a87b8a91b28f1091eb /lib/hashie | |
parent | a026498fb732eeb8ad9bdb11bf28d21d8c0e3467 (diff) | |
download | hashie-0c85e9482f0ca0232d7cf7cf6577a359ea07ffed.tar.gz |
Adds a fix for json generation
Diffstat (limited to 'lib/hashie')
-rw-r--r-- | lib/hashie/hash.rb | 16 | ||||
-rw-r--r-- | lib/hashie/mash.rb | 8 |
2 files changed, 17 insertions, 7 deletions
diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb index d13a58b..906563c 100644 --- a/lib/hashie/hash.rb +++ b/lib/hashie/hash.rb @@ -4,5 +4,19 @@ module Hashie # not be available in all libraries. class Hash < Hash include Hashie::HashExtensions + + # Converts a mash back to a hash (with stringified keys) + def to_hash + out = {} + keys.each do |k| + out[k] = Hashie::Hash === self[k] ? self[k].to_hash : self[k] + end + out + end + + # The C geneartor for the json gem doesn't like mashies + def to_json + to_hash.to_json + end end -end
\ No newline at end of file +end diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 69df491..4a196d6 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -111,12 +111,8 @@ module Hashie alias_method :update, :deep_update alias_method :merge!, :update - # Converts a mash back to a hash (with stringified keys) - def to_hash - Hash.new(default).merge(self) - end - def method_missing(method_name, *args) + def method_missing(method_name, *args, &blk) return self[method_name] if key?(method_name) match = method_name.to_s.match(/(.*?)([?=!]?)$/) case match[2] @@ -127,7 +123,7 @@ module Hashie when "!" initializing_reader(match[1]) else - default(method_name) + default(method_name, *args, &blk) end end |