Module: Tanker::InstanceMethods

Defined in:
lib/tanker.rb

Overview

these are the instance methods included

Instance Method Summary collapse

Instance Method Details

#create_snippet_attribute(key, value) ⇒ Object

dynamically create a snippet read attribute (method)



380
381
382
383
384
# File 'lib/tanker.rb', line 380

def create_snippet_attribute(key, value)
  # method name should something_snippet not snippet_something as the api returns it
  method_name = "#{key.match(/snippet_(\w+)/)[1]}_snippet"
  (class << self; self end).send(:define_method, method_name) { value }
end

#delete_tank_indexesObject

delete instance from index tank



354
355
356
# File 'lib/tanker.rb', line 354

def delete_tank_indexes
  tanker_config.index.delete_document(it_doc_id)
end

#it_doc_idObject

create a unique index based on the model name and unique id



408
409
410
# File 'lib/tanker.rb', line 408

def it_doc_id
  type_name + ' ' + self.id.to_s
end

#tanker_categoriesObject



338
339
340
# File 'lib/tanker.rb', line 338

def tanker_categories
  tanker_config.categories
end

#tanker_configObject



330
331
332
# File 'lib/tanker.rb', line 330

def tanker_config
  self.class.tanker_config || raise(NotConfigured, "Please configure Tanker for #{self.class.inspect} with the 'tankit' block")
end

#tanker_index_dataObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/tanker.rb', line 358

def tanker_index_data
  data = {}

  # attempt to autodetect timestamp
  if respond_to?(:created_at)
    data[:timestamp] = created_at.to_i
  end
  
  tanker_indexes.each do |field, block|
    val = block ? instance_exec(&block) : send(field)
    val = val.join(' ') if Array === val
    data[field.to_sym] = val.to_s unless val.nil?
  end

  data[:__any] = data.values.sort_by{|v| v.to_s}.join " . "
  data[:__type] = type_name
  data[:__id] = self.id.is_a?(Fixnum) ? self.id : self.id.to_s

  data
end

#tanker_index_optionsObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/tanker.rb', line 386

def tanker_index_options
  options = {}

  unless tanker_variables.empty?
    options[:variables] = tanker_variables.inject({}) do |hash, variables|
      hash.merge(instance_exec(&variables))
    end
  end

  unless tanker_categories.empty?
    options[:categories] = {}
    tanker_categories.each do |field, block|
      val = block ? instance_exec(&block) : send(field)
      val = val.join(' ') if Array === val
      options[:categories][field] = val.to_s unless val.nil?
    end
  end 
  
  options
end

#tanker_indexesObject



334
335
336
# File 'lib/tanker.rb', line 334

def tanker_indexes
  tanker_config.indexes
end

#tanker_variablesObject



342
343
344
# File 'lib/tanker.rb', line 342

def tanker_variables
  tanker_config.variables
end

#type_nameObject



412
413
414
# File 'lib/tanker.rb', line 412

def type_name
  tanker_config.options[:as] || self.class.name
end

#update_tank_indexesObject

update a create instance from index tank



347
348
349
350
351
# File 'lib/tanker.rb', line 347

def update_tank_indexes
  tanker_config.index.add_document(
    it_doc_id, tanker_index_data, tanker_index_options
  )
end