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

Inherits:
Node
  • Object
show all
Includes:
JsonComparable, 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 JsonComparable

#between?, #in?, #not_between?, #not_in?, #~

Methods included from JsonSteps

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

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_text, #distinct_from?, #end_with?, #except, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #keys, #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.



890
891
892
893
894
# File 'lib/active_record/refined/ast.rb', line 890

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.



888
889
890
# File 'lib/active_record/refined/ast.rb', line 888

def operand
  @operand
end

#pathObject (readonly)

Returns the value of attribute path.



888
889
890
# File 'lib/active_record/refined/ast.rb', line 888

def path
  @path
end

#valueObject (readonly)

Returns the value of attribute value.



888
889
890
# File 'lib/active_record/refined/ast.rb', line 888

def value
  @value
end

Instance Method Details

#json_value?Boolean

Always JSON, which is what the comparison guard asks.

Returns:

  • (Boolean)


897
898
899
# File 'lib/active_record/refined/ast.rb', line 897

def json_value?
  true
end

#to_arel(table, model) ⇒ Object



901
902
903
904
905
906
907
908
909
910
911
912
# File 'lib/active_record/refined/ast.rb', line 901

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