Class: IndexTanked::ClassCompanion

Inherits:
Object
  • Object
show all
Defined in:
lib/index-tanked/class_companion.rb

Direct Known Subclasses

ActiveRecordDefaults::ClassCompanion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ClassCompanion

Returns a new instance of ClassCompanion.



5
6
7
8
9
10
11
12
13
# File 'lib/index-tanked/class_companion.rb', line 5

def initialize(options={})
  @fields         = []
  @variables      = []
  @texts          = []
  @index_name     = options[:index] || IndexTanked::Configuration.index
  @index_tank_url = options[:url] || IndexTanked::Configuration.url
  raise IndexTanked::URLNotProvidedError if @index_tank_url.nil? && (IndexTanked::Configuration.index_available? || IndexTanked::Configuration.search_available?)
  raise IndexTanked::IndexNameNotProvidedError if @index_name.nil? && (IndexTanked::Configuration.index_available? || IndexTanked::Configuration.search_available?)
end

Instance Attribute Details

#doc_id_valueObject (readonly)

Returns the value of attribute doc_id_value.



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

def doc_id_value
  @doc_id_value
end

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#index_nameObject (readonly)

Returns the value of attribute index_name.



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

def index_name
  @index_name
end

#index_tank_urlObject (readonly)

Returns the value of attribute index_tank_url.



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

def index_tank_url
  @index_tank_url
end

#textsObject (readonly)

Returns the value of attribute texts.



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

def texts
  @texts
end

#variablesObject (readonly)

Returns the value of attribute variables.



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

def variables
  @variables
end

Instance Method Details

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



53
54
55
56
# File 'lib/index-tanked/class_companion.rb', line 53

def add_fields_to_query(query, options={})
  return nil if query.blank?
  [query, options[:fields] && options[:fields].to_a.map {|pair| pair.join(':')}.join(' ')].compact.join(" ").strip
end

#api_clientObject



37
38
39
40
# File 'lib/index-tanked/class_companion.rb', line 37

def api_client
  return nil if @index_tank_url.nil?
  @api_client ||= (IndexTanked::IndexTank::Client.new @index_tank_url)
end

#doc_id(method) ⇒ Object



15
16
17
# File 'lib/index-tanked/class_companion.rb', line 15

def doc_id(method)
  @doc_id_value = method
end

#field(field_name, method = field_name, options = {}) ⇒ Object



19
20
21
22
# File 'lib/index-tanked/class_companion.rb', line 19

def field(field_name, method=field_name, options={})
  method, options = field_name, method if method.is_a? Hash
  @fields <<  [field_name, method, options]
end

#get_value_from(instance, method) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/index-tanked/class_companion.rb', line 42

def get_value_from(instance, method)
  case method
  when Symbol
    instance.send method
  when Proc
    method.call(instance)
  else
    method
  end
end

#indexObject



32
33
34
35
# File 'lib/index-tanked/class_companion.rb', line 32

def index
  return nil if @index_name.nil? || api_client.nil?
  api_client.indexes @index_name
end

#text(method) ⇒ Object



24
25
26
# File 'lib/index-tanked/class_companion.rb', line 24

def text(method)
  @texts << method
end

#var(variable, method) ⇒ Object



28
29
30
# File 'lib/index-tanked/class_companion.rb', line 28

def var(variable, method)
  @variables << [variable, method]
end