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

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

Overview

Reading inside a JSON document. Every adapter can do it and no two spell it alike: PostgreSQL walks an array of steps, SQLite has the operators with a $ path, and MySQL has the functions -- which is what this uses for that family, since MariaDB answers to the same adapter and has no -> at all.

The path is turned into a string either way, so a key with a space or a quote in it travels as itself rather than having to be refused.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JsonSteps

#check_steps, #dollar_path, #escape_step, #steps_array

Methods included from Arithmetics

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

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, as_json: false) ⇒ JsonPath

Returns a new instance of JsonPath.



509
510
511
512
513
# File 'lib/active_record/refined/ast.rb', line 509

def initialize(operand, path, as_json: false)
  @operand = operand
  @path = check_steps(path, 'dig')
  @as_json = as_json
end

Instance Attribute Details

#as_jsonObject (readonly)

Returns the value of attribute as_json.



507
508
509
# File 'lib/active_record/refined/ast.rb', line 507

def as_json
  @as_json
end

#operandObject (readonly)

Returns the value of attribute operand.



507
508
509
# File 'lib/active_record/refined/ast.rb', line 507

def operand
  @operand
end

#pathObject (readonly)

Returns the value of attribute path.



507
508
509
# File 'lib/active_record/refined/ast.rb', line 507

def path
  @path
end

Instance Method Details

#to_arel(table, model) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/active_record/refined/ast.rb', line 515

def to_arel(table, model)
  document = to_arel_operand(operand, table, model)
  case AST.adapter_family(model)
  when :postgresql
    Arel::Nodes::InfixOperation.new(
      as_json ? :"#>" : :"#>>", document, Arel::Nodes.build_quoted(steps_array))
  when :mysql
    extracted = Arel::Nodes::NamedFunction.new(
      'JSON_EXTRACT', [document, Arel::Nodes.build_quoted(dollar_path)])
    as_json ? extracted : Arel::Nodes::NamedFunction.new('JSON_UNQUOTE', [extracted])
  else
    extracted = Arel::Nodes::InfixOperation.new(
      as_json ? :"->" : :"->>", 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(:n) == '5'` means the same
    # thing everywhere, and a number wants a cast everywhere too.
    as_json ? extracted : Arel::Nodes::NamedFunction.new(
      'CAST', [Arel::Nodes::As.new(extracted, Arel::Nodes::SqlLiteral.new('text'))])
  end
end