Class: Sash
Overview
Hash with indifferent access
Adapted from extlib/lib/mash.rb
Constant Summary
Constants inherited from Hash
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #default(key = nil) ⇒ Object
- #delete(key) ⇒ Object
-
#fetch(key, *extras) ⇒ Object
The value at key or the default value.
-
#initialize(constructor = {}) ⇒ Sash
constructor
A new instance of Sash.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
True if the key exists in the mash.
-
#merge(hash, &block) ⇒ Mash
A new mash with the hash values merged in.
- #merge! ⇒ Object
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
-
#stringify_keys ⇒ Hash
The sash as a Hash with stringified keys.
-
#symbolize_keys! ⇒ Sash
Used to provide the same interface as Hash.
-
#to_hash ⇒ Hash
The mash as a Hash with string keys.
-
#update(other_hash, &block) ⇒ Mash
The updated mash.
-
#values_at(*indices) ⇒ Array
The values at each of the provided keys.
Methods inherited from Hash
#compact, #compact!, #deep_delete, #deep_get, #deep_merge, #deep_merge!, #deep_set, #to_sash
Constructor Details
Instance Method Details
#[]=(key, value) ⇒ Object
34 35 36 |
# File 'lib/configliere/core_ext/sash.rb', line 34 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#default(key = nil) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/configliere/core_ext/sash.rb', line 91 def default(key = nil) if key.is_a?(String) && include?(key = key.to_sym) self[key] else super(key) end end |
#delete(key) ⇒ Object
77 78 79 |
# File 'lib/configliere/core_ext/sash.rb', line 77 def delete(key) super(convert_key(key)) end |
#fetch(key, *extras) ⇒ Object
Returns The value at key or the default value.
56 57 58 |
# File 'lib/configliere/core_ext/sash.rb', line 56 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
Returns True if the key exists in the mash.
43 44 45 |
# File 'lib/configliere/core_ext/sash.rb', line 43 def key?(key) super(convert_key(key)) end |
#merge(hash, &block) ⇒ Mash
Returns A new mash with the hash values merged in.
71 72 73 |
# File 'lib/configliere/core_ext/sash.rb', line 71 def merge(hash, &block) self.dup.update(hash, &block) end |
#merge! ⇒ Object
38 |
# File 'lib/configliere/core_ext/sash.rb', line 38 alias_method :merge!, :update |
#regular_update ⇒ Object
26 |
# File 'lib/configliere/core_ext/sash.rb', line 26 alias_method :regular_update, :update |
#regular_writer ⇒ Object
25 |
# File 'lib/configliere/core_ext/sash.rb', line 25 alias_method :regular_writer, :[]= |
#stringify_keys ⇒ Hash
Returns The sash as a Hash with stringified keys.
119 120 121 122 123 |
# File 'lib/configliere/core_ext/sash.rb', line 119 def stringify_keys h = Hash.new(default) each { |key, val| h[key.to_sym] = val } h end |
#symbolize_keys! ⇒ Sash
Used to provide the same interface as Hash.
116 |
# File 'lib/configliere/core_ext/sash.rb', line 116 def symbolize_keys!; self end |
#to_hash ⇒ Hash
Returns The mash as a Hash with string keys.
82 83 84 |
# File 'lib/configliere/core_ext/sash.rb', line 82 def to_hash Hash.new(default).merge(self) end |
#update(other_hash, &block) ⇒ Mash
Returns The updated mash.
104 105 106 107 108 109 110 111 |
# File 'lib/configliere/core_ext/sash.rb', line 104 def update(other_hash, &block) sash = self.class.new other_hash.each_pair do |key, value| val = convert_value(value) sash[convert_key(key)] = val end regular_update(sash, &block) end |
#values_at(*indices) ⇒ Array
Returns The values at each of the provided keys.
64 65 66 |
# File 'lib/configliere/core_ext/sash.rb', line 64 def values_at(*indices) indices.collect{|key| self[convert_key(key)]} end |