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)



415
416
417
418
419
# File 'lib/tanker.rb', line 415

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



389
390
391
# File 'lib/tanker.rb', line 389

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



443
444
445
# File 'lib/tanker.rb', line 443

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

#tanker_categoriesObject



373
374
375
# File 'lib/tanker.rb', line 373

def tanker_categories
  tanker_config.categories
end

#tanker_configObject



365
366
367
# File 'lib/tanker.rb', line 365

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

#tanker_index_dataObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/tanker.rb', line 393

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



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/tanker.rb', line 421

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



369
370
371
# File 'lib/tanker.rb', line 369

def tanker_indexes
  tanker_config.indexes
end

#tanker_variablesObject



377
378
379
# File 'lib/tanker.rb', line 377

def tanker_variables
  tanker_config.variables
end

#type_nameObject



447
448
449
# File 'lib/tanker.rb', line 447

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

#update_tank_indexesObject

update a create instance from index tank



382
383
384
385
386
# File 'lib/tanker.rb', line 382

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