class ChefZero::Solr::SolrDoc

This does what expander does, flattening the json doc into keys and values so that solr can search them.

Public Class Methods

new(json, id) click to toggle source
# File lib/chef_zero/solr/solr_doc.rb, line 6
def initialize(json, id)
  @json = json
  @id = id
end

Public Instance Methods

[](key) click to toggle source
# File lib/chef_zero/solr/solr_doc.rb, line 11
def [](key)
  matching_values { |match_key| match_key == key }
end
matching_values(&block) click to toggle source
# File lib/chef_zero/solr/solr_doc.rb, line 15
def matching_values(&block)
  result = []
  key_values(nil, @json) do |key, value|
    if block.call(key)
      result << value.to_s
    end
  end
  # Handle manufactured value(s)
  if block.call('X_CHEF_id_CHEF_X')
    result << @id.to_s
  end

  result.uniq
end

Private Instance Methods

key_values(key_so_far, value, &block) click to toggle source
# File lib/chef_zero/solr/solr_doc.rb, line 32
def key_values(key_so_far, value, &block)
  if value.is_a?(Hash)
    value.each_pair do |child_key, child_value|
      block.call(child_key, child_value.to_s)
      if key_so_far
        new_key = "#{key_so_far}_#{child_key}"
        key_values(new_key, child_value, &block)
      else
        key_values(child_key, child_value, &block) if child_value.is_a?(Hash) || child_value.is_a?(Array)
      end
    end
  elsif value.is_a?(Array)
    value.each do |child_value|
      key_values(key_so_far, child_value, &block)
    end
  else
    block.call(key_so_far || 'text', value.to_s)
  end
end