Parent

Chef::Expander::Flattener

Flattens and expands nested Hashes representing Chef objects (e.g, Nodes, Roles, DataBagItems, etc.) into flat Hashes so the objects are suitable to be saved into Solr. This code is more or less copy-pasted from chef/solr/index which may or may not be a great idea, though that does minimize the dependencies and hopefully minimize the memory use of chef-expander.

Public Class Methods

new(item) click to toggle source
# File lib/chef/expander/flattener.rb, line 39
def initialize(item)
  @item = item
end

Public Instance Methods

add_field_value(keys, value) click to toggle source
# File lib/chef/expander/flattener.rb, line 72
def add_field_value(keys, value)
  value = value.to_s
  @flattened_item[keys.join(UNDERSCORE)] << value
  @flattened_item[keys.last] << value
end
flatten_and_expand() click to toggle source
# File lib/chef/expander/flattener.rb, line 47
def flatten_and_expand
  @flattened_item = Hash.new {|hash, key| hash[key] = []}

  @item.each do |key, value|
    flatten_each([key.to_s], value)
  end

  @flattened_item.each_value { |values| values.uniq! }
  @flattened_item
end
flatten_each(keys, values) click to toggle source
# File lib/chef/expander/flattener.rb, line 58
def flatten_each(keys, values)
  case values
  when Hash
    values.each do |child_key, child_value|
      add_field_value(keys, child_key)
      flatten_each(keys + [child_key.to_s], child_value)
    end
  when Array
    values.each { |child_value| flatten_each(keys, child_value) }
  else
    add_field_value(keys, values)
  end
end
flattened_item() click to toggle source
# File lib/chef/expander/flattener.rb, line 43
def flattened_item
  @flattened_item || flatten_and_expand
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.