Class: Chef::Expander::Flattener
- Inherits:
-
Object
- Object
- Chef::Expander::Flattener
- Defined in:
- lib/chef/expander/flattener.rb
Overview
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.
Constant Summary collapse
- UNDERSCORE =
'_'
- X =
'X'
- X_CHEF_id_CHEF_X =
'X_CHEF_id_CHEF_X'
- X_CHEF_database_CHEF_X =
'X_CHEF_database_CHEF_X'
- X_CHEF_type_CHEF_X =
'X_CHEF_type_CHEF_X'
Instance Method Summary collapse
- #add_field_value(keys, value) ⇒ Object
- #flatten_and_expand ⇒ Object
- #flatten_each(keys, values) ⇒ Object
- #flattened_item ⇒ Object
-
#initialize(item) ⇒ Flattener
constructor
A new instance of Flattener.
Constructor Details
#initialize(item) ⇒ Flattener
Returns a new instance of Flattener.
39 40 41 |
# File 'lib/chef/expander/flattener.rb', line 39 def initialize(item) @item = item end |
Instance Method Details
#add_field_value(keys, value) ⇒ Object
72 73 74 75 76 |
# 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 ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/chef/expander/flattener.rb', line 47 def @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) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# 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 ⇒ Object
43 44 45 |
# File 'lib/chef/expander/flattener.rb', line 43 def flattened_item @flattened_item || end |