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)



351
352
353
354
355
# File 'lib/tanker.rb', line 351

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



325
326
327
# File 'lib/tanker.rb', line 325

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



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

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

#tanker_configObject



305
306
307
# File 'lib/tanker.rb', line 305

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

#tanker_index_dataObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/tanker.rb', line 329

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

  data
end

#tanker_index_optionsObject



357
358
359
360
361
362
363
364
365
366
367
# File 'lib/tanker.rb', line 357

def tanker_index_options
  options = {}

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

  options
end

#tanker_indexesObject



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

def tanker_indexes
  tanker_config.indexes
end

#tanker_variablesObject



313
314
315
# File 'lib/tanker.rb', line 313

def tanker_variables
  tanker_config.variables
end

#type_nameObject



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

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

#update_tank_indexesObject

update a create instance from index tank



318
319
320
321
322
# File 'lib/tanker.rb', line 318

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