Class: ChefUtils::Mash
- Inherits:
-
Hash
- Object
- Hash
- ChefUtils::Mash
- Defined in:
- lib/chef-utils/mash.rb
Class Method Summary collapse
-
.from_hash(hash) ⇒ Mash
The input Hash’s default value is maintained.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #default(key = nil) ⇒ Object
- #delete(key) ⇒ Object
-
#except(*keys) ⇒ Mash
A new mash without the selected keys.
-
#fetch(key, *extras) ⇒ Object
The value at key or the default value.
-
#initialize(constructor = {}) ⇒ Mash
constructor
A new instance of Mash.
-
#initialize_copy(orig) ⇒ Object
A new copied Mash.
-
#internal_get(key) ⇒ Object
private
internal API for use by Chef’s deep merge cache.
-
#internal_set(key, value) ⇒ Object
private
internal API for use by Chef’s deep merge cache.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
True if the key exists in the mash.
-
#merge(hash) ⇒ Mash
A new mash with the hash values merged in.
- #regular_delete ⇒ Object
- #regular_reader ⇒ Object
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
-
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
-
#symbolize_keys ⇒ Hash
The mash as a Hash with symbolized keys.
-
#to_hash ⇒ Hash
The mash as a Hash with string keys.
-
#update(other_hash) ⇒ Mash
(also: #merge!)
The updated mash.
-
#values_at(*indices) ⇒ Array
The values at each of the provided keys.
Constructor Details
#initialize(constructor = {}) ⇒ Mash
Returns a new instance of Mash.
61 62 63 64 65 66 67 68 |
# File 'lib/chef-utils/mash.rb', line 61 def initialize(constructor = {}) if constructor.is_a?(Hash) super() update(constructor) else super(constructor) end end |
Class Method Details
Instance Method Details
#[](key) ⇒ Object
118 119 120 |
# File 'lib/chef-utils/mash.rb', line 118 def [](key) regular_reader(key) end |
#[]=(key, value) ⇒ Object
128 129 130 |
# File 'lib/chef-utils/mash.rb', line 128 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#default(key = nil) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/chef-utils/mash.rb', line 89 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end |
#delete(key) ⇒ Object
193 194 195 |
# File 'lib/chef-utils/mash.rb', line 193 def delete(key) super(convert_key(key)) end |
#except(*keys) ⇒ Mash
Returns A new mash without the selected keys.
204 205 206 |
# File 'lib/chef-utils/mash.rb', line 204 def except(*keys) super(*keys.map { |k| convert_key(k) }) end |
#fetch(key, *extras) ⇒ Object
Returns The value at key or the default value.
172 173 174 |
# File 'lib/chef-utils/mash.rb', line 172 def fetch(key, *extras) super(convert_key(key), *extras) end |
#initialize_copy(orig) ⇒ Object
Returns A new copied Mash.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chef-utils/mash.rb', line 73 def initialize_copy(orig) super # Handle nested values each do |k, v| if v.is_a?(Mash) || v.is_a?(Array) self[k] = v.dup end end self end |
#internal_get(key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
internal API for use by Chef’s deep merge cache
134 135 136 |
# File 'lib/chef-utils/mash.rb', line 134 def internal_get(key) regular_reader(key) end |
#internal_set(key, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
internal API for use by Chef’s deep merge cache
140 141 142 |
# File 'lib/chef-utils/mash.rb', line 140 def internal_set(key, value) regular_writer(key, convert_value(value)) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
Returns True if the key exists in the mash.
159 160 161 |
# File 'lib/chef-utils/mash.rb', line 159 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Mash
Returns A new mash with the hash values merged in.
187 188 189 |
# File 'lib/chef-utils/mash.rb', line 187 def merge(hash) dup.update(hash) end |
#regular_delete ⇒ Object
114 |
# File 'lib/chef-utils/mash.rb', line 114 alias_method :regular_delete, :delete |
#regular_reader ⇒ Object
98 |
# File 'lib/chef-utils/mash.rb', line 98 alias_method :regular_reader, :[] |
#regular_update ⇒ Object
106 |
# File 'lib/chef-utils/mash.rb', line 106 alias_method :regular_update, :update |
#regular_writer ⇒ Object
102 |
# File 'lib/chef-utils/mash.rb', line 102 alias_method :regular_writer, :[]= |
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
211 |
# File 'lib/chef-utils/mash.rb', line 211 def stringify_keys!; self end |
#symbolize_keys ⇒ Hash
Returns The mash as a Hash with symbolized keys.
214 215 216 217 218 |
# File 'lib/chef-utils/mash.rb', line 214 def symbolize_keys h = Hash.new(default) each { |key, val| h[key.to_sym] = val } h end |
#to_hash ⇒ Hash
Returns The mash as a Hash with string keys.
221 222 223 |
# File 'lib/chef-utils/mash.rb', line 221 def to_hash Hash.new(default).merge(self) end |
#update(other_hash) ⇒ Mash Also known as: merge!
Returns The updated mash.
149 150 151 152 |
# File 'lib/chef-utils/mash.rb', line 149 def update(other_hash) other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } self end |
#values_at(*indices) ⇒ Array
Returns The values at each of the provided keys.
180 181 182 |
# File 'lib/chef-utils/mash.rb', line 180 def values_at(*indices) indices.collect { |key| self[convert_key(key)] } end |