Module: Tanker::ClassMethods

Defined in:
lib/tanker.rb

Overview

these are the class methods added when Tanker is included

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#index_nameObject (readonly)

Returns the value of attribute index_name.



36
37
38
# File 'lib/tanker.rb', line 36

def index_name
  @index_name
end

#tanker_indexesObject (readonly)

Returns the value of attribute tanker_indexes.



36
37
38
# File 'lib/tanker.rb', line 36

def tanker_indexes
  @tanker_indexes
end

Instance Method Details

#apiObject



51
52
53
# File 'lib/tanker.rb', line 51

def api
  @api ||= IndexTank::ApiClient.new(Tanker.configuration[:url])
end

#indexObject



55
56
57
# File 'lib/tanker.rb', line 55

def index
  @index ||= api.get_index(self.index_name)
end

#indexes(field) ⇒ Object



47
48
49
# File 'lib/tanker.rb', line 47

def indexes(field)
  @tanker_indexes << field
end

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tanker.rb', line 59

def search_tank(query, options = {})
  page     = options.delete(:page) || 1
  per_page = options.delete(:per_page) || self.per_page

  # transform fields in query
  if options.has_key? :conditions
    options[:conditions].each do |field,value|
      query += " #{field}:(#{value})"
    end
  end

  query = "__any:(#{query.to_s}) __type:#{self.name}"
  options = { :start => page - 1, :len => per_page }.merge(options)

  results = index.search(query, options)

  unless results[:results].empty?
    ids = results[:results].map{|res| res[:docid].split(" ", 2)}
  else
    return nil
  end


  @entries = WillPaginate::Collection.create(page, per_page) do |pager|
    result = self.find(ids)
    # inject the result array into the paginated collection:
    pager.replace(result)

    unless pager.total_entries
      # the pager didn't manage to guess the total count, do it manually
      pager.total_entries = results[:matches]
    end
  end
end

#tankit(name, &block) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/tanker.rb', line 38

def tankit(name, &block)
  if block_given?
    @index_name = name
    self.instance_exec(&block)
  else
    raise(NoBlockGiven, 'Please provide a block')
  end
end