Class: Lycra::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/lycra/import.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*documents) ⇒ Import

Returns a new instance of Import.



33
34
35
36
# File 'lib/lycra/import.rb', line 33

def initialize(*documents)
  @documents = documents
  @documents = Lycra::Document::Registry.all if @documents.empty?
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



3
4
5
# File 'lib/lycra/import.rb', line 3

def documents
  @documents
end

Class Method Details

.create(*args, &block) ⇒ Object



5
6
7
# File 'lib/lycra/import.rb', line 5

def self.create(*args, &block)
  new(*args).create(&block)
end

.destroy(*args, &block) ⇒ Object



13
14
15
# File 'lib/lycra/import.rb', line 13

def self.destroy(*args, &block)
  new(*args).destroy(&block)
end

.import(*args, **opts, &block) ⇒ Object



17
18
19
# File 'lib/lycra/import.rb', line 17

def self.import(*args, **opts, &block)
  new(*args).import(**opts, &block)
end

.recreate(*args, &block) ⇒ Object



9
10
11
# File 'lib/lycra/import.rb', line 9

def self.recreate(*args, &block)
  new(*args).recreate(&block)
end

.reindex(*args, **opts, &block) ⇒ Object



25
26
27
# File 'lib/lycra/import.rb', line 25

def self.reindex(*args, **opts, &block)
  new(*args).reindex(**opts, &block)
end

.rotate(*args, **opts, &block) ⇒ Object



21
22
23
# File 'lib/lycra/import.rb', line 21

def self.rotate(*args, **opts, &block)
  new(*args).rotate(**opts, &block)
end

.truncate(*args, **opts, &block) ⇒ Object



29
30
31
# File 'lib/lycra/import.rb', line 29

def self.truncate(*args, **opts, &block)
  new(*args).truncate(**opts, &block)
end

Instance Method Details

#create(&block) ⇒ Object



54
55
56
57
58
59
# File 'lib/lycra/import.rb', line 54

def create(&block)
  documents.each do |document|
    document.create_index! unless document.index_exists?
    yield(document) if block_given?
  end
end

#destroy(&block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/lycra/import.rb', line 70

def destroy(&block)
  documents.each do |document|
    document.delete_alias! if document.alias_exists?
    document.delete_index! if document.index_exists?
    yield(document) if block_given?
  end
end

#import(batch_size: 200, scope: nil, query: nil, &block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/lycra/import.rb', line 78

def import(batch_size: 200, scope: nil, query: nil, &block)
  recreate

  documents.each do |document|
    document.import! batch_size: batch_size, scope: scope, query: query, &block
  end
end

#recreate(&block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/lycra/import.rb', line 61

def recreate(&block)
  documents.each do |document|
    document.delete_alias! if document.alias_exists?
    document.delete_index! if document.index_exists?
    document.create_index!
    yield(document) if block_given?
  end
end

#reindex(batch_size: 200, scope: nil, query: nil, &block) ⇒ Object



104
105
106
107
108
# File 'lib/lycra/import.rb', line 104

def reindex(batch_size: 200, scope: nil, query: nil, &block)
  documents.each do |document|
    document.update! batch_size: batch_size, scope: scope, query: query, &block
  end
end

#rotate(batch_size: 200, scope: nil, query: nil, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lycra/import.rb', line 86

def rotate(batch_size: 200, scope: nil, query: nil, &block)
  create

  documents.each do |document|
    document.update! batch_size: batch_size, scope: scope, query: query, &block

    unless document.index_aliased?
      if document.alias_exists?
        old_index = document.aliased_index
        document.delete_alias!
        document.delete_index!(index: old_index)
      end

      document.create_alias!
    end
  end
end

#scopesObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lycra/import.rb', line 38

def scopes
  documents.map do |doc|
    if doc.import_scope.is_a?(Proc)
      doc.subject_type.instance_exec(&doc.import_scope)
    elsif doc.import_scope.is_a?(String) || doc.import_scope.is_a?(Symbol)
      doc.subject_type.send(doc.import_scope)
    else
      doc.subject_type.all
    end
  end
end

#totalObject



50
51
52
# File 'lib/lycra/import.rb', line 50

def total
  scopes.sum { |scope| scope.count }
end

#truncate(batch_size: 200, scope: nil, query: nil, &block) ⇒ Object



110
111
112
113
114
# File 'lib/lycra/import.rb', line 110

def truncate(batch_size: 200, scope: nil, query: nil, &block)
  documents.each do |document|
    document.delete! batch_size: batch_size, scope: scope, query: query, &block
  end
end