Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/rid/core_ext/hash.rb
Constant Summary collapse
- DEFAULT_SEPERATOR =
'/'
Instance Method Summary collapse
- #at(path, seperator = DEFAULT_SEPERATOR) ⇒ Object
- #flatten(seperator = DEFAULT_SEPERATOR, prefix = nil) ⇒ Object
- #flatten!(*args) ⇒ Object
- #update_at(path, value, seperator = DEFAULT_SEPERATOR) ⇒ Object
Instance Method Details
#at(path, seperator = DEFAULT_SEPERATOR) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rid/core_ext/hash.rb', line 23 def at(path, seperator = DEFAULT_SEPERATOR) current = self parts = path.split(DEFAULT_SEPERATOR) key = parts.pop return unless key parts.each do |part| current = current[part] return unless current.is_a?(self.class) end current[key] end |
#flatten(seperator = DEFAULT_SEPERATOR, prefix = nil) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rid/core_ext/hash.rb', line 4 def flatten(seperator = DEFAULT_SEPERATOR, prefix = nil) new_hash = {} each do |key, value| new_key = [prefix, key].compact.join(seperator) if value.is_a?(self.class) new_hash.update value.flatten(seperator, new_key) else new_hash.update new_key => value end end new_hash end |
#flatten!(*args) ⇒ Object
19 20 21 |
# File 'lib/rid/core_ext/hash.rb', line 19 def flatten!(*args) replace flatten(*args) end |
#update_at(path, value, seperator = DEFAULT_SEPERATOR) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rid/core_ext/hash.rb', line 40 def update_at(path, value, seperator = DEFAULT_SEPERATOR) current = self parts = path.split(DEFAULT_SEPERATOR) key = parts.pop return self unless key parts.each do |part| current[part] ||= {} current = current[part] raise 'updating value at %s failed!' % inspect unless current.is_a?(self.class) end current[key] = value end |