Class: ChewyKiqqer::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy_kiqqer/index.rb

Instance Method Summary collapse

Constructor Details

#initialize(backref: :self, index: nil, queue: ChewyKiqqer.default_queue) ⇒ Index

Returns a new instance of Index.



5
6
7
8
9
# File 'lib/chewy_kiqqer/index.rb', line 5

def initialize(backref: :self, index: nil, queue: ChewyKiqqer.default_queue)
  @index_name = index
  @backref_method = backref
  @queue = queue
end

Instance Method Details

#backref(object) ⇒ Object



15
16
17
18
19
# File 'lib/chewy_kiqqer/index.rb', line 15

def backref(object)
  return @backref_method.call(object) if @backref_method.respond_to?(:call)
  return object if @backref_method.to_s == 'self'
  object.send(@backref_method)
end

#backref_ids(object) ⇒ Object



21
22
23
# File 'lib/chewy_kiqqer/index.rb', line 21

def backref_ids(object)
  Array.wrap(backref(object)).map { |object| object.respond_to?(:id) ? object.id : object.to_i }
end

#enqueue(object) ⇒ Object



11
12
13
# File 'lib/chewy_kiqqer/index.rb', line 11

def enqueue(object)
  Sidekiq::Client.push('queue' => @queue, 'class' => ChewyKiqqer::Worker, 'args' => [@index_name, backref_ids(object)])
end