Class: ActiveRecord::Refined::AST::JsonPath

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

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 Arithmetics

#&, #*, #+, #-, #/, #<<, #>>, #^, #|, #~

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, json_value: true) ⇒ JsonPath

Returns a new instance of JsonPath.



843
844
845
846
847
# File 'lib/active_record/refined/ast.rb', line 843

def initialize(operand, path, json_value: true)
  @operand = operand
  @path = check_steps(path, "dig")
  @json_value = json_value
end

Instance Attribute Details

#operandObject (readonly)

Returns the value of attribute operand.



841
842
843
# File 'lib/active_record/refined/ast.rb', line 841

def operand
  @operand
end

#pathObject (readonly)

Returns the value of attribute path.



841
842
843
# File 'lib/active_record/refined/ast.rb', line 841

def path
  @path
end

Instance Method Details

#json_value?Boolean

Returns:

  • (Boolean)


849
850
851
# File 'lib/active_record/refined/ast.rb', line 849

def json_value?
  @json_value
end

#to_arel(table, model) ⇒ Object



853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/active_record/refined/ast.rb', line 853

def to_arel(table, model)
  document = to_arel_operand(operand, table, model)
  case AST.adapter_family(model)
  when :postgresql
    Arel::Nodes::InfixOperation.new(
      json_value? ? :"#>" : :"#>>", document, Arel::Nodes.build_quoted(steps_array))
  when :mysql
    extracted = Arel::Nodes::NamedFunction.new(
      "JSON_EXTRACT", [document, Arel::Nodes.build_quoted(dollar_path)])
    json_value? ? extracted : Arel::Nodes::NamedFunction.new("JSON_UNQUOTE", [extracted])
  else
    extracted = Arel::Nodes::InfixOperation.new(
      json_value? ? :"->" : :"->>", document, Arel::Nodes.build_quoted(dollar_path))
    # SQLite's ->> gives back the value with its type, where the other
    # two give text.  Cast so that `dig_text(:n) == '5'` means the
    # same thing everywhere, and a number wants a cast everywhere too.
    json_value? ? extracted : Arel::Nodes::NamedFunction.new(
      "CAST", [Arel::Nodes::As.new(extracted, Arel::Nodes::SqlLiteral.new("text"))])
  end
end