Module: Tanker::ClassMethods

Defined in:
lib/tanker.rb

Overview

these are the class methods added when Tanker is included They’re kept to a minimum to prevent namespace pollution

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tanker_configObject

Returns the value of attribute tanker_config.



252
253
254
# File 'lib/tanker.rb', line 252

def tanker_config
  @tanker_config
end

Instance Method Details

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



278
279
280
# File 'lib/tanker.rb', line 278

def search_tank(query, options = {})
  Tanker.search([self], query, options)
end

#tanker_indexObject



282
283
284
# File 'lib/tanker.rb', line 282

def tanker_index
  tanker_config.index
end

#tanker_parse_doc_id(result) ⇒ Object



308
309
310
# File 'lib/tanker.rb', line 308

def tanker_parse_doc_id(result)
  result['docid'].split(' ').last
end

#tanker_reindex(options = {}) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/tanker.rb', line 286

def tanker_reindex(options = {})
  puts "Indexing #{self} model"

  batches = []
  options[:batch_size] ||= 200
  records = options[:scope] ? send(options[:scope]).all : all
  record_size = 0

  records.each_with_index do |model_instance, idx|
    batch_num = idx / options[:batch_size]
    (batches[batch_num] ||= []) << model_instance
    record_size += 1
  end

  timer = Time.now
  batches.each_with_index do |batch, idx|
    Tanker.batch_update(batch)
    puts "Indexed #{batch.size} records   #{(idx * options[:batch_size]) + batch.size}/#{record_size}"
  end
  puts "Indexed #{record_size} #{self} records in #{Time.now - timer} seconds"
end

#tankit(name = nil, options = {}, &block) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/tanker.rb', line 254

def tankit(name = nil, options = {}, &block)
  if block_given?
    raise(NoIndexName, 'Please provide an index name') if name.nil? && self.tanker_config.nil?

    self.tanker_config ||= ModelConfig.new(name, options, Proc.new)
    name ||= self.tanker_config.index_name

    self.tanker_config.index_name = name

    config = ModelConfig.new(name, {}, block)
    config.indexes.each do |key, value|
      self.tanker_config.indexes << [key, value]
    end

    unless config.variables.empty?
      self.tanker_config.variables do
        instance_exec &config.variables.first
      end
    end
  else
    raise(NoBlockGiven, 'Please provide a block')
  end
end