Class: ActiveKit::Position::Harmonize

Inherits:
Object
  • Object
show all
Defined in:
lib/active_kit/position/harmonize.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_class:, name:, scope:) ⇒ Harmonize

Returns a new instance of Harmonize.



4
5
6
7
8
9
10
11
# File 'lib/active_kit/position/harmonize.rb', line 4

def initialize(current_class:, name:, scope:)
  @current_class = current_class
  @name = name

  @scoped_class = @current_class.where(scope)
  @positioning = Positioning.new
  @batch_size = 1000
end

Instance Method Details

#run!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_kit/position/harmonize.rb', line 13

def run!
  @current_class.transaction do
    chair_at_params, scoped_class_with_order, chair_method, offset_operator = control
    currvalue = @positioning.chair_at(**chair_at_params, increase_spot_length_by: 1).first

    first_run = true
    where_offset = nil
    loop do
      records = scoped_class_with_order.where(where_offset).limit(@batch_size)
      break if records.empty?

      records.lock.each do |record|
        value, reharmonize = first_run ? [currvalue, false] : @positioning.public_send(chair_method, currvalue: currvalue)
        raise message_for_reharmonize if reharmonize

        record.send("#{@name}=", value)
        record.save!

        currvalue = record.public_send("#{@name}")
        first_run = false
      end

      where_offset = ["#{@name} #{offset_operator} ?", currvalue]
    end
  end

  Rails.logger.info "ActiveKit::Position | Harmonize for :#{@name}: completed."
end