Class: Oplogjam::Operators::IndexAssignment

Inherits:
Assignment
  • Object
show all
Defined in:
lib/oplogjam/operators/index_assignment.rb

Instance Attribute Summary

Attributes inherited from Assignment

#path, #value

Instance Method Summary collapse

Methods inherited from Assignment

#initialize

Constructor Details

This class inherits a constructor from Oplogjam::Operators::Assignment

Instance Method Details

#indexObject



30
31
32
# File 'lib/oplogjam/operators/index_assignment.rb', line 30

def index
  Integer(path.last, 10)
end

#parent_pathObject



34
35
36
# File 'lib/oplogjam/operators/index_assignment.rb', line 34

def parent_path
  path[0...-1]
end

#update(column) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/oplogjam/operators/index_assignment.rb', line 6

def update(column)
  # Now for a not-so-fun bit!
  #
  # As this is a numeric index, it might either be an index into an existing array or a numeric field name on an
  # object.
  #
  # If it is an index into an array then we need to ensure that all prior indexes down to 0 are either set or null.
  filled_array_column = (0...index).inject(column) { |subject, i|
    prior_path = parent_path + [i.to_s]

    subject.set(prior_path, Sequel.function(:coalesce, column[prior_path], NULL))
  }

  populated_column = Sequel.pg_jsonb_op(
    Sequel.case(
      { ARRAY_TYPE => filled_array_column },
      column,
      column[parent_path].typeof
    )
  )

  populated_column.set(path, value.to_json)
end