summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/hashie/hash_extensions.rb12
-rw-r--r--lib/hashie/mash.rb61
2 files changed, 16 insertions, 57 deletions
diff --git a/lib/hashie/hash_extensions.rb b/lib/hashie/hash_extensions.rb
index 145bb06..8fb5657 100644
--- a/lib/hashie/hash_extensions.rb
+++ b/lib/hashie/hash_extensions.rb
@@ -7,7 +7,7 @@ module Hashie
base.send :alias_method, hashie_method, "hashie_#{hashie_method}" unless base.instance_methods.include?(hashie_method)
end
end
-
+
# Destructively convert all of the keys of a Hash
# to their string representations.
def hashie_stringify_keys!
@@ -16,25 +16,25 @@ module Hashie
end
self
end
-
+
# Convert all of the keys of a Hash
# to their string representations.
def hashie_stringify_keys
self.dup.stringify_keys!
end
-
+
# Convert this hash into a Mash
def to_mash
Hashie::Mash.new(self)
end
end
-
+
module PrettyInspect
def self.included(base)
base.send :alias_method, :hash_inspect, :inspect
base.send :alias_method, :inspect, :hashie_inspect
end
-
+
def hashie_inspect
ret = "<##{self.class.to_s}"
keys.sort.each do |key|
@@ -44,4 +44,4 @@ module Hashie
ret
end
end
-end \ No newline at end of file
+end
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index b32afdb..3add1ae 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -50,30 +50,13 @@ module Hashie
# them as well.
def initialize(source_hash = nil, default = nil, &blk)
deep_update(source_hash) if source_hash
- super default if default
- super &blk if blk
+ default ? super(default) : super(&blk)
end
class << self; alias [] new; end
def id #:nodoc:
- self["id"] ? self["id"] : super
- end
-
- # Borrowed from Merb's Mash object.
- #
- # ==== Parameters
- # key<Object>:: The default value for the mash. Defaults to nil.
- #
- # ==== Alternatives
- # If key is a Symbol and it is a key in the mash, then the default value will
- # be set to the value matching the key.
- def default(key = nil)
- if key.is_a?(Symbol) && key?(key.to_s)
- self[key]
- else
- key ? super : super()
- end
+ key?("id") ? self["id"] : super
end
alias_method :regular_reader, :[]
@@ -82,22 +65,20 @@ module Hashie
# Retrieves an attribute set in the Mash. Will convert
# any key passed in to a string before retrieving.
def [](key)
- key = convert_key(key)
- regular_reader(key)
+ regular_reader(convert_key(key))
end
# Sets an attribute in the Mash. Key will be converted to
# a string before it is set, and Hashes will be converted
# into Mashes for nesting purposes.
def []=(key,value) #:nodoc:
- key = convert_key(key)
- regular_writer(key, convert_value(value))
+ regular_writer(convert_key(key), convert_value(value))
end
# This is the bang method reader, it will return a new Mash
# if there isn't a value already assigned to the key requested.
def initializing_reader(key)
- self[key] = Hashie::Mash.new unless key?(key)
+ self[key] ||= Hashie::Mash.new
self[key]
end
@@ -107,9 +88,8 @@ module Hashie
Mash.new(self, self.default)
end
- alias_method :picky_key?, :key?
def key?(key)
- picky_key?(convert_key(key))
+ super(convert_key(key))
end
# Performs a deep_update on a duplicate of the
@@ -121,18 +101,14 @@ module Hashie
# Recursively merges this mash with the passed
# in hash, merging each hash in the hierarchy.
def deep_update(other_hash)
- other_hash = Hashie::Hash[other_hash].stringify_keys!
-
other_hash.each_pair do |k,v|
- k = convert_key(k)
- self[k] = Hashie::Mash.new(self[k]).to_mash if self[k].is_a?(Hash) unless self[k].is_a?(Hashie::Mash)
- if self[k].is_a?(Hashie::Mash) && other_hash[k].is_a?(Hash)
- self[k] = self[k].deep_merge(other_hash[k])
+ ck = convert_key(k)
+ if Mash === v && Hash === other_hash
+ regular_writer(ck, v.deep_merge(other_hash[k]))
else
- self[k] = convert_value(other_hash[k],true)
+ regular_writer(ck, convert_value(other_hash[k], true))
end
end
-
self
end
alias_method :deep_merge!, :deep_update
@@ -176,23 +152,6 @@ module Hashie
end
end
-
-# def method_missing(method_name, *args) #:nodoc:
-# if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
-# self[match[1]] = args.first
-# elsif (match = method_name.to_s.match(/(.*)\?$/)) && args.size == 0
-# key?(match[1])
-# elsif (match = method_name.to_s.match(/(.*)!$/)) && args.size == 0
-# initializing_reader(match[1])
-# elsif key?(method_name)
-# self[method_name]
-# elsif match = method_name.to_s.match(/^([a-z][a-z0-9A-Z_]+)$/)
-# default(method_name)
-# else
-# super
-# end
-# end
-
protected
def convert_key(key) #:nodoc: