Class: Chef::Node::ImmutableMash

Inherits:
Mash
  • Object
show all
Includes:
CommonAPI, Immutablize
Defined in:
lib/chef/node/immutable_collections.rb

Overview

== ImmutableMash ImmutableMash implements Hash/Dict behavior for reading values from node attributes.

ImmutableMash acts like a Mash (Hash that is indifferent to String or Symbol keys), with some important exceptions:

  • Methods that mutate state are overridden to raise an error instead.
  • Methods that read from the collection are overriden so that they check if the Chef::Node::Attribute has been modified since an instance of this class was generated. An error is raised if the object detects that it is stale.
  • Values can be accessed in attr_reader-like fashion via method_missing.

Instance Method Summary collapse

Methods included from CommonAPI

#exist?, #read, #read!, #unlink, #unlink!, #write, #write!

Methods included from Immutablize

#immutablize

Methods inherited from Mash

#[]=, #default, #delete, #except, #fetch, from_hash, #initialize_copy, #key?, #merge, #regular_update, #regular_writer, #stringify_keys!, #symbolize_keys, #update, #values_at

Constructor Details

#initialize(mash_data = {}) ⇒ ImmutableMash

Returns a new instance of ImmutableMash



121
122
123
124
125
# File 'lib/chef/node/immutable_collections.rb', line 121

def initialize(mash_data = {})
  mash_data.each do |key, value|
    internal_set(key, immutablize(value))
  end
end

Instance Method Details

#convert_value(value) ⇒ Object

Mash uses #convert_value to mashify values on input. Since we're handling this ourselves, override it to be a no-op

FIXME? this seems wrong to do and i think is responsible for

dup needing to be more complicated than Mash.new(self)?



139
140
141
# File 'lib/chef/node/immutable_collections.rb', line 139

def convert_value(value)
  value
end

#dupObject

NOTE: #default and #default= are likely to be pretty confusing. For a regular ruby Hash, they control what value is returned for, e.g., hash[:no_such_key] #=> hash.default Of course, 'default' has a specific meaning in Chef-land



148
149
150
151
152
153
154
# File 'lib/chef/node/immutable_collections.rb', line 148

def dup
  h = Mash.new
  each_pair do |k, v|
    h[k] = safe_dup(v)
  end
  h
end

#public_method_that_only_deep_merge_should_use(key, value) ⇒ Object



127
128
129
# File 'lib/chef/node/immutable_collections.rb', line 127

def public_method_that_only_deep_merge_should_use(key, value)
  internal_set(key, immutablize(value))
end

#safe_dup(e) ⇒ Object

For elements like Fixnums, true, nil...



175
176
177
178
179
# File 'lib/chef/node/immutable_collections.rb', line 175

def safe_dup(e)
  e.dup
rescue TypeError
  e
end

#to_hObject Also known as: to_hash



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/chef/node/immutable_collections.rb', line 156

def to_h
  h = Hash.new
  each_pair do |k, v|
    h[k] =
      case v
      when ImmutableMash
        v.to_h
      when ImmutableArray
        v.to_a
      else
        safe_dup(v)
      end
  end
  h
end