class Chewy::Index::Adapter::Base

Basic adapter class. Contains interface, need to implement to add any classes support

Constants

BATCH_SIZE

Attributes

options[R]
target[R]

Public Class Methods

accepts?(_target) click to toggle source

Returns `true` if this adapter is applicable for the given target.

# File lib/chewy/index/adapter/base.rb, line 12
def self.accepts?(_target)
  true
end

Public Instance Methods

identify(_collection) click to toggle source

Returns shortest identifies for further postponed importing. For ORM/ODM it will be an array of ids for simple objects - just objects themselves

# File lib/chewy/index/adapter/base.rb, line 34
def identify(_collection)
  raise NotImplementedError
end
import(_batch, &_block) click to toggle source

Splits passed objects to groups according to `:batch_size` options. For every group creates hash with action keys. Example:

{ delete: [object_or_id1, object_or_id2], index: [object3, object4, object5] }

@yieldparam _batch [Array<Object>] each batch of objects @return [true, false] returns true if all the block call returns true and false otherwise

# File lib/chewy/index/adapter/base.rb, line 45
def import(_batch, &_block)
  raise NotImplementedError
end
import_fields(_fields, _batch_size, &_block) click to toggle source

Unlike {#import} fetches only ids (references) to the imported objects, using the same procedures as {#import}.

@param _fields [Array<Symbol>] additional fields to fetch @param _batch_size [Integer] batch size, defaults to 1000 @yieldparam batch [Array<Object>] each batch of objects

# File lib/chewy/index/adapter/base.rb, line 55
def import_fields(_fields, _batch_size, &_block)
  raise NotImplementedError
end
import_references(_batch_size, &_block) click to toggle source

Uses the same strategy as import for the passed arguments, and returns an array of references to the passed objects. Returns ids if possible. Otherwise - and array of objects themselves.

@param _batch_size [Integer] batch size, defaults to 1000 @yieldparam batch [Array<Object>] each batch of objects

# File lib/chewy/index/adapter/base.rb, line 65
def import_references(_batch_size, &_block)
  raise NotImplementedError
end
load(_ids, **_options) click to toggle source

Returns array of loaded objects for passed ids array. If some object was not loaded, it returns `nil` in the place of this object

load([1, 2, 3]) #=>
  # [<Product id: 1>, nil, <Product id: 3>], assuming, #2 was not found
# File lib/chewy/index/adapter/base.rb, line 75
def load(_ids, **_options)
  raise NotImplementedError
end
name() click to toggle source

Camelcased name.

# File lib/chewy/index/adapter/base.rb, line 18
def name
  raise NotImplementedError
end
type_name() click to toggle source

Underscored type name, user for elasticsearch type creation and for type class access with ProductsIndex.type_hash hash or method. `ProductsIndex.type_hash` or `ProductsIndex.product`

# File lib/chewy/index/adapter/base.rb, line 26
def type_name
  @type_name ||= name.underscore
end

Private Instance Methods

delete_from_index?(object) click to toggle source
# File lib/chewy/index/adapter/base.rb, line 87
def delete_from_index?(object)
  delete_if = options[:delete_if]
  delete ||= case delete_if
  when Symbol, String
    object.send delete_if
  when Proc
    delete_if.arity == 1 ? delete_if.call(object) : object.instance_exec(&delete_if)
  end

  !!delete
end
grouped_objects(objects) click to toggle source
# File lib/chewy/index/adapter/base.rb, line 81
def grouped_objects(objects)
  objects.to_a.group_by do |object|
    delete_from_index?(object) ? :delete : :index
  end
end