Class: Heliosphere::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/heliosphere/indexer.rb

Constant Summary collapse

SEARCH_WITHOUT_LIFECYCLE =
{ :auto_index => false, :auto_remove => false }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockObject

Returns the value of attribute block.



11
12
13
# File 'lib/heliosphere/indexer.rb', line 11

def block
  @block
end

Instance Method Details

#applyObject



23
24
25
26
27
28
29
30
# File 'lib/heliosphere/indexer.rb', line 23

def apply
  options = SEARCH_WITHOUT_LIFECYCLE
  model_definitions.each do |model, block|
    model.instance_eval do
      searchable(options, &block)
    end
  end
end

#define(&block) ⇒ Object



13
14
15
16
17
# File 'lib/heliosphere/indexer.rb', line 13

def define(&block)
  instance_eval(&block)
  extend_active_record
  apply
end

#extend_active_recordObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/heliosphere/indexer.rb', line 32

def extend_active_record
  ActiveRecord::Base.class_eval %Q{
    def index_on_save?
      @index_on_save = true unless instance_variable_defined?(:@index_on_save)
      @index_on_save
    end

    def save_without_index
      @index_on_save = false
      save
      @index_on_save = true
    end

    def save_without_index!
      @index_on_save = false
      save!
      @index_on_save = true
    end
  }
end

#index(model, &block) ⇒ Object



53
54
55
# File 'lib/heliosphere/indexer.rb', line 53

def index(model, &block)
  model_definitions[model] = block
end

#model_definitionsObject



61
62
63
# File 'lib/heliosphere/indexer.rb', line 61

def model_definitions
  @model_definitions ||= {}
end

#model_namesObject



19
20
21
# File 'lib/heliosphere/indexer.rb', line 19

def model_names
  models.map { |model| model.name.underscore.to_sym }
end

#modelsObject



57
58
59
# File 'lib/heliosphere/indexer.rb', line 57

def models
  model_definitions.keys
end