class Chewy::Index::Adapter::Object
This adapter provides an ability to import documents from any source. You can actually use any class or even a symbol as a target.
In case if a class is used - some of the additional features are available: it is possible to provide the default import data (used on reset) and source objects loading logic.
Public Class Methods
The signature of the index scope definition.
@example
index_scope :geoname index_scope Geoname index_scope -> { Geoname.all_the_places }, name: 'geoname'
@param target [Class, Symbol, String, Proc] a source of data and everything @option options [String, Symbol] :name redefines the inferred name if necessary @option options [String, Symbol] :import_all_method redefines import method name @option options [String, Symbol] :load_all_method redefines batch load method name @option options [String, Symbol] :load_one_method redefines per-object load method name
# File lib/chewy/index/adapter/object.rb, line 29 def initialize(target, **options) @target = target @options = options end
Public Instance Methods
While for ORM adapters it returns an array of ids for the passed collection, for the object adapter it returns the collection itself.
@param collection [Array<Object>, Object] a collection or an object @return [Array<Object>]
# File lib/chewy/index/adapter/object.rb, line 52 def identify(collection) Array.wrap(collection) end
# File lib/chewy/index/adapter/object.rb, line 88 def import(*args, &block) collection, options = import_args(*args) import_objects(collection, options, &block) end
# File lib/chewy/index/adapter/object.rb, line 116 def import_fields(*args, &block) return enum_for(:import_fields, *args) unless block_given? options = args.extract_options! options[:batch_size] ||= BATCH_SIZE if args.empty? && @target.respond_to?(pluck_method) @target.send(pluck_method, :id, *options[:fields]).each_slice(options[:batch_size], &block) elsif options[:fields].blank? import_references(*args, options) do |batch| yield batch.map { |object| object_field(object, :id) || object } end else import_references(*args, options) do |batch| batch = batch.map do |object| options[:fields].map { |field| object_field(object, field) } .unshift(object_field(object, :id) || object) end yield batch end end end
# File lib/chewy/index/adapter/object.rb, line 142 def import_references(*args, &block) return enum_for(:import_references, *args) unless block_given? collection, options = import_args(*args) collection.each_slice(options[:batch_size], &block) end
This method is used internally by the request DSL when the collection of ORM/ODM objects is requested.
Options usage is implemented by `load_all_method` and `load_one_method`.
If none of the `load_all_method` or `load_one_method` is implemented for the target - the method will return nil. This means that the loader will return an array `Chewy::Index` objects that actually was passed.
To use loading for objects it is obviously required to provide some meaningful ids for ES documents.
@example
class Geoname def self.load_all(wrappers, options) if options[:additional_data] wrappers.map do |wrapper| FancyGeoAPI.point_by_name(wrapper.name) end else wrappers end end end MyIndex.load(additional_data: true).objects
@param ids [Array<Hash>] an array of ids from ES hits @param options [Hash] any options passed here with the request DSL `load` method. @return [Array<Object>, nil]
# File lib/chewy/index/adapter/object.rb, line 179 def load(ids, **options) if target.respond_to?(load_all_method) if target.method(load_all_method).arity == 1 target.send(load_all_method, ids) else target.send(load_all_method, ids, options) end elsif target.respond_to?(load_one_method) if target.method(load_one_method).arity == 1 ids.map { |hit| target.send(load_one_method, hit) } else ids.map { |hit| target.send(load_one_method, hit, options) } end end end
Inferred from the target by default if possible.
@example
# defines name = Geoname index_scope :geoname # still defines name = Geoname index_scope -> { Geoname.all_the_places }, name: 'geoname'
@return [String]
# File lib/chewy/index/adapter/object.rb, line 43 def name @name ||= (options[:name] || @target).to_s.camelize.demodulize end
Private Instance Methods
Chewy::Index::Adapter::Base#delete_from_index?
# File lib/chewy/index/adapter/object.rb, line 203 def delete_from_index?(object) delete = super delete ||= object.destroyed? if object.respond_to?(:destroyed?) delete ||= object[:_destroyed] || object['_destroyed'] if object.is_a?(Hash) !!delete end
# File lib/chewy/index/adapter/object.rb, line 218 def import_all_method @import_all_method ||= options[:import_all_method] || :call end
# File lib/chewy/index/adapter/object.rb, line 234 def import_args(*args) options = args.extract_options! options[:batch_size] ||= BATCH_SIZE collection = if args.empty? && @target.respond_to?(import_all_method) @target.send(import_all_method) else args.flatten(1).compact end [collection, options] end
# File lib/chewy/index/adapter/object.rb, line 197 def import_objects(objects, options) objects.each_slice(options[:batch_size]).map do |group| yield grouped_objects(group) end.all? end
# File lib/chewy/index/adapter/object.rb, line 226 def load_all_method @load_all_method ||= options[:load_all_method] || :load_all end
# File lib/chewy/index/adapter/object.rb, line 230 def load_one_method @load_one_method ||= options[:load_one_method] || :load_one end
# File lib/chewy/index/adapter/object.rb, line 210 def object_field(object, name) if object.respond_to?(name) object.send(name) elsif object.is_a?(Hash) object[name.to_sym] || object[name.to_s] end end
# File lib/chewy/index/adapter/object.rb, line 222 def pluck_method @pluck_method ||= options[:pluck_method] || :pluck end