Class: Chewy::Type::Adapter::Sequel

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

Constant Summary

Constants inherited from Base

Base::BATCH_SIZE

Instance Attribute Summary collapse

Attributes inherited from Base

#options, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#type_name

Constructor Details

#initialize(*args) ⇒ Sequel

Returns a new instance of Sequel.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chewy/type/adapter/sequel.rb', line 15

def initialize(*args)
  @options = args.extract_options!

  if dataset? args.first
    dataset = args.first
    @target = dataset.model
    @default_dataset = dataset.unordered.unlimited
  else
    model = args.first
    @target = model
    @default_dataset = model.where(nil)
  end
end

Instance Attribute Details

#default_datasetObject (readonly)

Returns the value of attribute default_dataset.



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

def default_dataset
  @default_dataset
end

Class Method Details

.accepts?(target) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/chewy/type/adapter/sequel.rb', line 10

def self.accepts?(target)
  defined?(::Sequel::Model) && (
    target.is_a?(Class) && target < ::Sequel::Model || target.is_a?(::Sequel::Dataset))
end

Instance Method Details

#identify(obj) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/chewy/type/adapter/sequel.rb', line 33

def identify(obj)
  if dataset? obj
    obj.select_map(target_pk)
  else
    Array.wrap(obj).map do |item|
      model?(item) ? item.pk : item
    end
  end
end

#import(*args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chewy/type/adapter/sequel.rb', line 43

def import(*args, &block)
  import_options = args.extract_options!
  batch_size = import_options[:batch_size] || BATCH_SIZE

  if args.empty?
    import_dataset(default_dataset, batch_size, &block)
  elsif args.one? && dataset?(args.first)
    import_dataset(args.first, batch_size, &block)
  else
    import_models(args.flatten.compact, batch_size, &block)
  end
end

#load(*args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chewy/type/adapter/sequel.rb', line 56

def load(*args)
  load_options = args.extract_options!
  index_ids = args.flatten.map(&:id)  # args contains index instances

  type_name = load_options[:_type].type_name.to_sym
  additional_scope = load_options[type_name].try(:[], :scope) || load_options[:scope]

  dataset = select_by_ids(target, index_ids)

  if additional_scope.is_a?(Proc)
    index_ids.map!(&:to_s)
    dataset.instance_exec(&additional_scope).to_a.select do |model|
      index_ids.include? model.pk.to_s
    end
  else
    dataset.to_a
  end
end

#nameObject



29
30
31
# File 'lib/chewy/type/adapter/sequel.rb', line 29

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