Class: Settingable::Hash
- Inherits:
-
Hash
- Object
- Hash
- Settingable::Hash
- Defined in:
- lib/settingable/hash.rb
Overview
A hash that raises an error on an access that fails.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Accesses the hash.
-
#[]=(key, value) ⇒ void
Used to set a key to a value.
-
#fetch(key, value = BLANK_OBJECT) {|key| ... } ⇒ Object
Performs a fetch.
-
#initialize(body = {}) ⇒ Hash
constructor
Initialize the hash.
-
#key?(key) ⇒ Boolean
Checks to determine if the given key is set in this hash.
- #old_access ⇒ Object
- #old_fetch ⇒ Object
- #old_key? ⇒ Object
- #old_set ⇒ Object
-
#to_h ⇒ ::Hash
Converts this hash into a normal hash.
Constructor Details
#initialize(body = {}) ⇒ Hash
Initialize the hash. If a value is passed, the values are set on this hash. Does not modify the value.
11 12 13 14 15 |
# File 'lib/settingable/hash.rb', line 11 def initialize(body = {}) body.each do |key, value| self[key] = value end end |
Instance Method Details
#[](key) ⇒ Object
Accesses the hash. It raises an error if it can't find the key. It attempts to convert the key into a Symbol.
39 40 41 42 43 44 45 |
# File 'lib/settingable/hash.rb', line 39 def [](key) case key when String then fetch(key.intern) when Symbol then fetch(key) else fetch(key.to_s.intern) end end |
#[]=(key, value) ⇒ void
This method returns an undefined value.
Used to set a key to a value. The key is first converted to a
Symbol, and the value is coerced into a Settingable::Hash if it is a
::Hash.
56 57 58 59 60 61 62 |
# File 'lib/settingable/hash.rb', line 56 def []=(key, value) case key when String then super(key.intern, convert(value)) when Symbol then super(key, convert(value)) else super(key.to_s.intern, convert(value)) end end |
#fetch(key, value = BLANK_OBJECT) {|key| ... } ⇒ Object
Performs a fetch. If the key is in the hash, it returns its value. If the key is not in the hash, and a value was passed, the value is returned. If the key is not in the hash, and a block was passed, it yields. Otherwise, it raises KeyError.
82 83 84 85 86 87 88 89 90 |
# File 'lib/settingable/hash.rb', line 82 def fetch(key, value = BLANK_OBJECT) case when old_key?(key) then old_access(key) when block_given? then yield(key) when value != BLANK_OBJECT then value else fail KeyError, "Key not found: #{key.inspect}" end end |
#key?(key) ⇒ Boolean
Checks to determine if the given key is set in this hash. It first converts the key to a Symbol, then performs the check.
24 25 26 27 28 29 30 |
# File 'lib/settingable/hash.rb', line 24 def key?(key) case key when String then super(key.intern) when Symbol then super(key) else super(key.to_s.intern) end end |
#old_access ⇒ Object
32 |
# File 'lib/settingable/hash.rb', line 32 alias_method :old_access, :[] |
#old_fetch ⇒ Object
70 |
# File 'lib/settingable/hash.rb', line 70 alias_method :old_fetch, :fetch |
#old_key? ⇒ Object
17 |
# File 'lib/settingable/hash.rb', line 17 alias_method :old_key?, :key? |
#old_set ⇒ Object
47 |
# File 'lib/settingable/hash.rb', line 47 alias_method :old_set, :[]= |
#to_h ⇒ ::Hash
Converts this hash into a normal hash. Since we recursively converted every hash value into a Settingable::Hash, we have to undo that for the new hash.
97 98 99 100 101 102 103 104 |
# File 'lib/settingable/hash.rb', line 97 def to_h out = {} each do |key, value| out[key] = convert(value, true) end out end |