Module: IndexTanked::ClassMethods

Defined in:
lib/index-tanked/class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#index_tankedObject (readonly)

Returns the value of attribute index_tanked.



3
4
5
# File 'lib/index-tanked/class_methods.rb', line 3

def index_tanked
  @index_tanked
end

Instance Method Details

#add_to_index_tank(doc_id, data, fallback = true) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/index-tanked/class_methods.rb', line 14

def add_to_index_tank(doc_id, data, fallback=true)
  begin
    raise IndexTanked::IndexingDisabledError unless IndexTanked::Configuration.index_available?
    timeout = if IndexTanked::Configuration.timeout.is_a?(Proc)
      IndexTanked::Configuration.timeout.call
    else
      IndexTanked::Configuration.timeout
    end
    if timeout && fallback
      IndexTanked::Timer.timeout(timeout, TimeoutExceededError) do
        sleep(timeout + 1) if $testing_index_tanked_timeout
        @index_tanked.index.document(doc_id).add(*data)
      end
    else
      @index_tanked.index.document(doc_id).add(*data)
    end
  rescue StandardError => e
    if fallback && IndexTanked::Configuration.add_to_index_fallback
      IndexTanked::Configuration.add_to_index_fallback.call({:class  => self,
                                                             :data   => data,
                                                             :doc_id => doc_id,
                                                             :error  => e})
    else
      raise
    end
  end
end

#add_to_index_tank_without_fallback(doc_id, data) ⇒ Object



42
43
44
# File 'lib/index-tanked/class_methods.rb', line 42

def add_to_index_tank_without_fallback(doc_id, data)
  add_to_index_tank(doc_id, data, false)
end

#delete_from_index_tank(doc_id, fallback = true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/index-tanked/class_methods.rb', line 46

def delete_from_index_tank(doc_id, fallback=true)
  begin
    raise IndexTanked::IndexingDisabledError unless IndexTanked::Configuration.index_available?
    timeout = if IndexTanked::Configuration.timeout.is_a?(Proc)
      IndexTanked::Configuration.timeout.call
    else
      IndexTanked::Configuration.timeout
    end
    if timeout  && fallback
      IndexTanked::Timer.timeout(timeout, TimeoutExceededError) do
        sleep(timeout + 1) if $testing_index_tanked_timeout
        @index_tanked.index.document(doc_id).delete
      end
    else
      @index_tanked.index.document(doc_id).delete
    end
  rescue StandardError => e
    if fallback && IndexTanked::Configuration.delete_from_index_fallback
      IndexTanked::Configuration.delete_from_index_fallback.call({:class  => self,
                                                                  :doc_id => doc_id,
                                                                  :error  => e})
    else
      raise
    end
  end
end

#delete_from_index_tank_without_fallback(doc_id) ⇒ Object



73
74
75
# File 'lib/index-tanked/class_methods.rb', line 73

def delete_from_index_tank_without_fallback(doc_id)
  delete_from_index_tank(doc_id, false)
end

#index_tank(options = {}, &block) ⇒ Object



5
6
7
8
# File 'lib/index-tanked/class_methods.rb', line 5

def index_tank(options={}, &block)
  @index_tanked ||= ClassCompanion.new(options)
  @index_tanked.instance_exec &block
end

#search_index_tank(query, options = {}) ⇒ Object



10
11
12
# File 'lib/index-tanked/class_methods.rb', line 10

def search_index_tank(query, options={})
  SearchResult.new(index_tanked.add_fields_to_query(query, options), index_tanked.index, options)
end