Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/makeloc/core_extensions.rb
Instance Method Summary collapse
- #at(path, opts = {}) ⇒ Object
- #delete_at(path) ⇒ Object
- #flatten_keys(prefix = "") ⇒ Object
- #parse(only_leaves = false, &block) ⇒ Object
- #parse_leaves(&block) ⇒ Object
- #update_leaves!(&block) ⇒ Object
Instance Method Details
#at(path, opts = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/makeloc/core_extensions.rb', line 36 def at(path,opts={}) array_path = path.to_s.split('.') current_key = array_path.shift # raise ArgumentError, "wrong key " unless self.keys.include?(current_key) raise ArgumentError, "wrong opts" unless (opts.empty? or opts[:set]) if array_path.empty? case opts[:set] when :delete self.delete(current_key) when nil self[current_key] else self[current_key] = opts[:set] end else self[current_key] and self[current_key].at(array_path.join('.'),opts) end end |
#delete_at(path) ⇒ Object
55 56 57 |
# File 'lib/makeloc/core_extensions.rb', line 55 def delete_at(path) at(path, :set => :delete) end |
#flatten_keys(prefix = "") ⇒ Object
2 3 4 5 6 7 8 9 |
# File 'lib/makeloc/core_extensions.rb', line 2 def flatten_keys(prefix="") keys = [] self.keys.each do |key| keys << "#{prefix}#{key}" (keys << self[key].flatten_keys(prefix + "#{key}.")) if self[key].is_a? Hash end prefix == "" ? keys.flatten : keys end |
#parse(only_leaves = false, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/makeloc/core_extensions.rb', line 11 def parse(only_leaves = false, &block) each do |k,v| if v.is_a? Hash yield(k,v) unless only_leaves v.parse(only_leaves,&block) else yield(k,v) end end end |
#parse_leaves(&block) ⇒ Object
22 23 24 |
# File 'lib/makeloc/core_extensions.rb', line 22 def parse_leaves(&block) parse(only_leaves = true, &block) end |
#update_leaves!(&block) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/makeloc/core_extensions.rb', line 26 def update_leaves!(&block) each do |k,v| if v.is_a? Hash v.update_leaves!(&block) else self[k] = yield(k,v) end end end |