Class: Chef::SearchIndex

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearchIndex

Returns a new instance of SearchIndex.



26
27
28
29
30
31
# File 'lib/chef/search_index.rb', line 26

def initialize
  @index = Ferret::Index::Index.new(
    :path => Chef::Config[:search_index_path],
    :key => [ :id ]
  )
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



24
25
26
# File 'lib/chef/search_index.rb', line 24

def index
  @index
end

Instance Method Details

#add(new_object) ⇒ Object



33
34
35
36
37
# File 'lib/chef/search_index.rb', line 33

def add(new_object)
  index_hash = create_index_object(new_object)
  Chef::Log.debug("Indexing #{index_hash[:index_name]} with #{index_hash.inspect}")
  @index.add_document(index_hash)
end

#commitObject



72
73
74
# File 'lib/chef/search_index.rb', line 72

def commit
  @index.commit
end

#create_index_object(new_object) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/search_index.rb', line 39

def create_index_object(new_object)
  index_hash = nil
  
  if new_object.respond_to?(:to_index)
    index_hash = new_object.to_index
  elsif new_object.kind_of?(Hash)
    index_hash = new_object
  else
    raise Chef::Exceptions::SearchIndex, "Cannot transform argument to a Hash!" 
  end
  
  unless index_hash.has_key?(:index_name) || index_hash.has_key?("index_name")
    raise Chef::Exceptions::SearchIndex, "Cannot index without an index_name key: #{index_hash.inspect}"
  end
  
  unless index_hash.has_key?(:id) || index_hash.has_key?("id")
    raise Chef::Exceptions::SearchIndex, "Cannot index without an id key: #{index_hash.inspect}"
  end
 
  sanitized_hash = Hash.new
  index_hash.each do |k,v|
    sanitized_hash[k.to_sym] = v
  end

  sanitized_hash
end

#delete(index_obj) ⇒ Object



66
67
68
69
70
# File 'lib/chef/search_index.rb', line 66

def delete(index_obj)
  to_delete = create_index_object(index_obj)
  Chef::Log.debug("Removing #{to_delete.inspect} from the #{to_delete[:index_name]} index")
  @index.delete(to_delete[:id])
end