Class: Reamaze::Opinionated::PrefHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/opinionated/hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner, preferential) ⇒ PrefHash

Returns a new instance of PrefHash.



4
5
6
7
# File 'lib/opinionated/hash.rb', line 4

def initialize(owner, preferential)
  @owner = owner
  @preferential = preferential
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/opinionated/hash.rb', line 18

def method_missing(id, *args, &block)
  match = /(.+)=$/.match id.to_s
  return self[match[1]] = args[0] if match
  
  match = /(.+)\?$/.match id.to_s
  return !!self[match[1]] if match
  
  self[id]
end

Instance Method Details

#[](key) ⇒ Object



9
10
11
# File 'lib/opinionated/hash.rb', line 9

def [] key
  @owner.get_preferential @preferential, key.to_s, true
end

#[]=(key, value) ⇒ Object



13
14
15
16
# File 'lib/opinionated/hash.rb', line 13

def []= key, value
  @owner.set_preferential @preferential, key.to_s, value, true
  super key.to_s, value
end