Class: ActiveRecord::Refined::AST::JsonSet

Inherits:
Node
  • Object
show all
Includes:
JsonSteps, Predications
Defined in:
lib/active_record/refined/ast.rb

Overview

Setting a value inside a JSON document, which is what bury does to what dig reads. The document comes back changed rather than being written anywhere; update_all is what writes it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JsonSteps

#check_steps, #dollar_path, #dollar_step, #escape_step, #steps_array

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_json, #distinct_from?, #end_with?, #except, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?, #when

Methods inherited from Node

#as, #asc, #desc

Constructor Details

#initialize(operand, path, value) ⇒ JsonSet

Returns a new instance of JsonSet.



636
637
638
639
640
# File 'lib/active_record/refined/ast.rb', line 636

def initialize(operand, path, value)
  @operand = operand
  @path = check_steps(path, 'bury')
  @value = value
end

Instance Attribute Details

#operandObject (readonly)

Returns the value of attribute operand.



634
635
636
# File 'lib/active_record/refined/ast.rb', line 634

def operand
  @operand
end

#pathObject (readonly)

Returns the value of attribute path.



634
635
636
# File 'lib/active_record/refined/ast.rb', line 634

def path
  @path
end

#valueObject (readonly)

Returns the value of attribute value.



634
635
636
# File 'lib/active_record/refined/ast.rb', line 634

def value
  @value
end

Instance Method Details

#to_arel(table, model) ⇒ Object



642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/active_record/refined/ast.rb', line 642

def to_arel(table, model)
  document = to_arel_operand(operand, table, model)
  if AST.adapter_family(model) == :postgresql
    Arel::Nodes::NamedFunction.new(
      'jsonb_set',
      [document, Arel::Nodes.build_quoted(steps_array), postgresql_value(table, model)])
  else
    Arel::Nodes::NamedFunction.new(
      'JSON_SET',
      [document, Arel::Nodes.build_quoted(dollar_path), other_value(table, model)])
  end
end