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, #escape_step, #steps_array

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_json, #distinct_from?, #end_with?, #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.



547
548
549
550
551
# File 'lib/active_record/refined/ast.rb', line 547

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.



545
546
547
# File 'lib/active_record/refined/ast.rb', line 545

def operand
  @operand
end

#pathObject (readonly)

Returns the value of attribute path.



545
546
547
# File 'lib/active_record/refined/ast.rb', line 545

def path
  @path
end

#valueObject (readonly)

Returns the value of attribute value.



545
546
547
# File 'lib/active_record/refined/ast.rb', line 545

def value
  @value
end

Instance Method Details

#to_arel(table, model) ⇒ Object



553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/active_record/refined/ast.rb', line 553

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