Module: Utils::Hash
- Defined in:
- lib/utils/hash.rb
Instance Method Summary collapse
- #content_if(key, string = default_value_for_key(key)) ⇒ Object
- #exists?(key) ⇒ Boolean
- #included(parent) ⇒ Object
- #key_if(key, string = default_value_for_key(key)) ⇒ Object
- #use_hash(content) ⇒ Object
- #value_for(key) ⇒ Object
- #with_hash(content) {|proc| ... } ⇒ Object
Instance Method Details
#content_if(key, string = default_value_for_key(key)) ⇒ Object
20 21 22 |
# File 'lib/utils/hash.rb', line 20 def content_if(key, string = default_value_for_key(key)) "#{string} #{value_for(key)}" if exists?(key) end |
#exists?(key) ⇒ Boolean
28 29 30 31 32 33 34 |
# File 'lib/utils/hash.rb', line 28 def exists?(key) case value_for(key) when nil, '' then false else true end end |
#included(parent) ⇒ Object
4 5 6 |
# File 'lib/utils/hash.rb', line 4 def included(parent) parent.extend(self) end |
#key_if(key, string = default_value_for_key(key)) ⇒ Object
24 25 26 |
# File 'lib/utils/hash.rb', line 24 def key_if(key, string = default_value_for_key(key)) "#{string}" if exists?(key) end |
#use_hash(content) ⇒ Object
16 17 18 |
# File 'lib/utils/hash.rb', line 16 def use_hash(content) @__content = content # use a double underscore to make sure it doesn't interfere end |
#value_for(key) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/utils/hash.rb', line 36 def value_for(key) keys = key.split('.') keys.inject(@__content) do |location, key| location && location[key] ? location[key] : nil end end |
#with_hash(content) {|proc| ... } ⇒ Object
9 10 11 12 13 14 |
# File 'lib/utils/hash.rb', line 9 def with_hash(content, &proc) old_content = @__content @__content = content # use a double underscore to make sure it doesn't interfere yield proc @__content = old_content end |