Class: Hash
- Defined in:
- lib/hoodie/core_ext/blank.rb,
lib/hoodie/core_ext/hash.rb
Overview
Add #blank? method to Hash class.
Defined Under Namespace
Classes: UndefinedPathError
Instance Method Summary collapse
-
#argumentize(args_field = nil) ⇒ Object
Turn a hash into a method arguments.
-
#capitalize_keys ⇒ Hash
Returns a new hash with all keys converted to strings and the first letter capitalized.
-
#compact ⇒ Hash
Returns a compacted copy (contains no key/value pairs having nil? values).
-
#except(*rejected) ⇒ Hash
Create a hash with all key/value pairs in receiver except
rejected. -
#normalize_keys ⇒ Hash
Returns a new hash with all keys downcased and converted to symbols.
-
#object_state(data = nil) ⇒ Object
Get or set state of object.
-
#only(*allowed) ⇒ Hash
Create a hash with only key/value pairs in receiver and
allowed. -
#recursive_fetch(*args, &block) ⇒ Hash, ...
Recursively searchs a nested datastructure for a key and returns the value.
- #recursive_merge(other) ⇒ Object
-
#recursively_capitalize_key ⇒ Hash
Returns a new Hash, recursively converting all keys to strings and the first letter capitalized.
-
#recursively_normalize_keys ⇒ Hash
Returns a new Hash, recursively downcasing and converting all keys to symbols.
-
#recursively_stringify_key ⇒ Hash
Returns a new Hash, recursively converting all keys to strings.
-
#recursively_symbolize_keys ⇒ Hash
Returns a new Hash, recursively converting all keys to symbols.
-
#recursively_transform_keys(&block) ⇒ Hash
Returns a new hash, recursively converting all keys by the block operation.
-
#stringify_keys ⇒ Hash
Returns a new hash with all keys converted to strings.
-
#symbolize_keys ⇒ Hash
Returns a new hash with all keys converted to symbols.
-
#to_struct(struct_name) ⇒ Object
A method to convert a Hash into a Struct.
-
#transform_keys ⇒ Hash
Returns a new hash with all keys converted using the block operation.
-
#zip(col1, col2) ⇒ Object
Creates a new hash from two separate arrays, a
keysarray and avaluesarray.
Instance Method Details
#argumentize(args_field = nil) ⇒ Object
Turn a hash into a method arguments.
h = { :list => [1,2], :base => "HI" }
Without an argument field.
h.argumentize #=> [ { :list => [1,2], :base => "HI" } ]
With an argument field.
h.argumentize(:list) #=> [ 1, 2, { :base => "HI" } ]
h.argumentize(:base) #=> [ "HI", { :list => [1,2] } ]
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/hoodie/core_ext/hash.rb', line 36 def argumentize(args_field = nil) config = dup if args_field args = [config.delete(args_field)].flatten.compact else args = [] end args << config return args end |
#capitalize_keys ⇒ Hash
Returns a new hash with all keys converted to strings and the first letter capitalized.
170 171 172 |
# File 'lib/hoodie/core_ext/hash.rb', line 170 def capitalize_keys transform_keys { |key| key.to_s.capitalize rescue key } end |
#compact ⇒ Hash
Returns a compacted copy (contains no key/value pairs having nil? values)
83 84 85 |
# File 'lib/hoodie/core_ext/hash.rb', line 83 def compact select { |_, value| !value.nil? } end |
#except(*rejected) ⇒ Hash
Create a hash with all key/value pairs in receiver except rejected
{ :one => 1, :two => 2, :three => 3 }.except(:one)
# => { :two => 2, :three => 3 }
219 220 221 222 223 |
# File 'lib/hoodie/core_ext/hash.rb', line 219 def except(*rejected) hash = self.dup rejected.each {|k| hash.delete(k) } hash end |
#normalize_keys ⇒ Hash
Returns a new hash with all keys downcased and converted to symbols.
120 121 122 |
# File 'lib/hoodie/core_ext/hash.rb', line 120 def normalize_keys transform_keys { |key| key.downcase.to_sym rescue key } end |
#object_state(data = nil) ⇒ Object
Get or set state of object. You can think of #object_state as an in-code form of marshalling.
59 60 61 |
# File 'lib/hoodie/core_ext/hash.rb', line 59 def object_state(data=nil) data ? replace(data) : dup end |
#only(*allowed) ⇒ Hash
Create a hash with only key/value pairs in receiver and allowed
{ :one => 1, :two => 2, :three => 3 }.only(:one) # => { :one => 1 }
203 204 205 206 207 |
# File 'lib/hoodie/core_ext/hash.rb', line 203 def only(*allowed) hash = {} allowed.each {|k| hash[k] = self[k] if self.has_key?(k) } hash end |
#recursive_fetch(*args, &block) ⇒ Hash, ...
Recursively searchs a nested datastructure for a key and returns the value. If a block is provided its value will be returned if the key does not exist
239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/hoodie/core_ext/hash.rb', line 239 def recursive_fetch(*args, &block) args.reduce(self) do |obj, arg| begin arg = Integer(arg) if obj.is_a? Array obj.fetch(arg) rescue ArgumentError, IndexError, NoMethodError => e break block.call(arg) if block raise UndefinedPathError, "Could not fetch path (#{args.join(' > ')}) at #{arg}", e.backtrace end end end |
#recursive_merge(other) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/hoodie/core_ext/hash.rb', line 252 def recursive_merge(other) hash = self.dup other.each do |key, value| myval = self[key] if value.is_a?(Hash) && myval.is_a?(Hash) hash[key] = myval.recursive_merge(value) else hash[key] = value end end hash end |
#recursively_capitalize_key ⇒ Hash
Returns a new Hash, recursively converting all keys to strings and the first letter capitalized.
179 180 181 |
# File 'lib/hoodie/core_ext/hash.rb', line 179 def recursively_capitalize_key recursively_transform_keys { |key| key.to_s.capitalize rescue key } end |
#recursively_normalize_keys ⇒ Hash
Returns a new Hash, recursively downcasing and converting all keys to symbols.
129 130 131 |
# File 'lib/hoodie/core_ext/hash.rb', line 129 def recursively_normalize_keys recursively_transform_keys { |key| key.downcase.to_sym rescue key } end |
#recursively_stringify_key ⇒ Hash
Returns a new Hash, recursively converting all keys to strings.
161 162 163 |
# File 'lib/hoodie/core_ext/hash.rb', line 161 def recursively_stringify_key recursively_transform_keys { |key| key.to_s rescue key } end |
#recursively_symbolize_keys ⇒ Hash
Returns a new Hash, recursively converting all keys to symbols.
145 146 147 |
# File 'lib/hoodie/core_ext/hash.rb', line 145 def recursively_symbolize_keys recursively_transform_keys { |key| key.to_sym rescue key } end |
#recursively_transform_keys(&block) ⇒ Hash
Returns a new hash, recursively converting all keys by the block operation.
111 112 113 |
# File 'lib/hoodie/core_ext/hash.rb', line 111 def recursively_transform_keys(&block) _recursively_transform_keys_in_object(self, &block) end |
#stringify_keys ⇒ Hash
Returns a new hash with all keys converted to strings.
153 154 155 |
# File 'lib/hoodie/core_ext/hash.rb', line 153 def stringify_keys transform_keys { |key| key.to_s rescue key } end |
#symbolize_keys ⇒ Hash
Returns a new hash with all keys converted to symbols.
137 138 139 |
# File 'lib/hoodie/core_ext/hash.rb', line 137 def symbolize_keys transform_keys { |key| key.to_sym rescue key } end |
#to_struct(struct_name) ⇒ Object
A method to convert a Hash into a Struct.
h = { :name => "Earl", "age" => 20, "sex" => "lots", "worries" => "none" }
s = h.to_struct("Foo")
52 53 54 |
# File 'lib/hoodie/core_ext/hash.rb', line 52 def to_struct(struct_name) Struct.new(struct_name,*keys).new(*values) end |
#transform_keys ⇒ Hash
Returns a new hash with all keys converted using the block operation.
97 98 99 100 101 102 103 104 |
# File 'lib/hoodie/core_ext/hash.rb', line 97 def transform_keys enum_for(:transform_keys) unless block_given? result = self.class.new each_key do |key| result[yield(key)] = self[key] end result end |
#zip(col1, col2) ⇒ Object
Creates a new hash from two separate arrays, a keys array and a values array.
190 191 192 |
# File 'lib/hoodie/core_ext/hash.rb', line 190 def zip(col1, col2) col1.zip(col2).inject({}) { |r, i| r[i[0]] = i[1]; r } end |