Class: Hash
- Defined in:
- lib/lab42/core/open_object.rb,
lib/lab42/core/hash.rb
Overview
class OpenObject
Instance Method Summary collapse
- #fetch!(key, *defaults, &defblk) ⇒ Object
- #only(*args) ⇒ Object
- #reject_values(*behavior, &beh) ⇒ Object
- #select_values(*behavior, &beh) ⇒ Object
- #to_open_object ⇒ Object
- #with_present(key, default: nil) ⇒ Object
- #without(*keys) ⇒ Object
Instance Method Details
#fetch!(key, *defaults, &defblk) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/lab42/core/hash.rb', line 42 def fetch! key, *defaults, &defblk default_present = !(defaults.empty? && defblk.nil?) return fetch key unless default_present fetch key do self[ key ] = defblk ? defblk.() : defaults.first end end |
#only(*args) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/lab42/core/hash.rb', line 4 def only *args args.inject Hash.new do | r, k | if has_key? k r.merge k => self[k] else r end end end |
#reject_values(*behavior, &beh) ⇒ Object
14 15 16 17 |
# File 'lib/lab42/core/hash.rb', line 14 def reject_values *behavior, &beh beh = Lab42::Meta::Behavior *behavior, &beh reject{ |_, v| beh.(v) } end |
#select_values(*behavior, &beh) ⇒ Object
19 20 21 22 |
# File 'lib/lab42/core/hash.rb', line 19 def select_values *behavior, &beh beh = Lab42::Meta::Behavior *behavior, &beh select{ |_, v| beh.(v) } end |
#to_open_object ⇒ Object
49 50 51 |
# File 'lib/lab42/core/open_object.rb', line 49 def to_open_object OpenObject.new **self end |
#with_present(key, default: nil) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/lab42/core/hash.rb', line 24 def with_present key, default: nil if has_key? key yield fetch(key), self else default end end |