Class: Chef::NodeMap

Inherits:
Object show all
Defined in:
lib/chef/node_map.rb

Constant Summary collapse

VALID_OPTS =
[
  :on_platform,
  :on_platforms,
  :platform,
  :os,
  :platform_family,
]
DEPRECATED_OPTS =
[
  :on_platform,
  :on_platforms,
]

Instance Method Summary collapse

Constructor Details

#initializeNodeMap

Create a new NodeMap



37
38
39
# File 'lib/chef/node_map.rb', line 37

def initialize
  @map = {}
end

Instance Method Details

#get(node, key) ⇒ Object

Get a value from the NodeMap via applying the node to the filters that were set on the key.

Parameters:

  • node (Chef::Node)

    The Chef::Node object for the run

  • key (Object)

    Key to look up

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/node_map.rb', line 68

def get(node, key)
  # FIXME: real exception
  raise "first argument must be a Chef::Node" unless node.is_a?(Chef::Node)
  return nil unless @map.has_key?(key)
  @map[key].each do |matcher|
    if filters_match?(node, matcher[:filters]) &&
      block_matches?(node, matcher[:block])
      return matcher[:value]
    end
  end
  nil
end

#set(key, value, filters = {}) {|node| ... } ⇒ NodeMap

Set a key/value pair on the map with a filter. The filter must be true when applied to the node in order to retrieve the value.

Parameters:

  • key (Object)

    Key to store

  • value (Object)

    Value associated with the key

  • filters (Hash) (defaults to: {})

    Node filter options to apply to key retrieval

Yields:

  • (node)

    Arbitrary node filter as a block which takes a node argument

Returns:

  • (NodeMap)

    Returns self for possible chaining



50
51
52
53
54
55
56
57
58
59
# File 'lib/chef/node_map.rb', line 50

def set(key, value, filters = {}, &block)
  validate_filter!(filters)
  deprecate_filter!(filters)
  @map[key] ||= []
  # we match on the first value we find, so we want to unshift so that the
  # last setter wins
  # FIXME: need a test for this behavior
  @map[key].unshift({ filters: filters, block: block, value: value })
  self
end