Class: Chewy::Type::Adapter::Object

Inherits:
Base show all
Defined in:
lib/chewy/type/adapter/object.rb

Constant Summary

Constants inherited from Base

Base::BATCH_SIZE

Instance Method Summary collapse

Methods inherited from Base

#type_name

Constructor Details

#initialize(*args) ⇒ Object

Returns a new instance of Object.



7
8
9
10
# File 'lib/chewy/type/adapter/object.rb', line 7

def initialize *args
  @options = args.extract_options!
  @target = args.first
end

Instance Method Details

#import(*args, &block) ⇒ Object

Imports passed data with options

Import data types:

* Array ob objects

Import options:

<tt>:batch_size</tt> - import batch size, 1000 objects by default

If methods delete_from_index? or destroyed? are defined for object and any return true then object will be deleted from index. But to be destroyed objects need to respond to id method as well, so ElasticSearch could know which one to delete.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chewy/type/adapter/object.rb', line 31

def import *args, &block
  import_options = args.extract_options!
  batch_size = import_options.delete(:batch_size) || BATCH_SIZE
  objects = args.flatten.compact

  objects.each_slice(batch_size).map do |group|
    action_groups = group.group_by do |object|
      raise "Object is not a `#{target}`" if class_target? && !object.is_a?(target)
      delete = object.delete_from_index? if object.respond_to?(:delete_from_index?)
      delete ||= object.destroyed? if object.respond_to?(:destroyed?)
      delete ? :delete : :index
    end
    block.call action_groups
  end.all?
end

#load(*args) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/chewy/type/adapter/object.rb', line 47

def load *args
  load_options = args.extract_options!
  objects = args.flatten
  if class_target?
    objects.map { |object| target.wrap(object) }
  else
    objects
  end
end

#nameObject



12
13
14
# File 'lib/chewy/type/adapter/object.rb', line 12

def name
  @name ||= (options[:name] || target).to_s.camelize.demodulize
end