Class: ActiveRecord::Refined::AST::JsonPath
- 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
-
#as_json ⇒ Object
readonly
Returns the value of attribute as_json.
-
#operand ⇒ Object
readonly
Returns the value of attribute operand.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(operand, path, as_json: false) ⇒ JsonPath
constructor
A new instance of JsonPath.
- #to_arel(table, model) ⇒ Object
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
Constructor Details
#initialize(operand, path, as_json: false) ⇒ JsonPath
Returns a new instance of JsonPath.
513 514 515 516 517 |
# File 'lib/active_record/refined/ast.rb', line 513 def initialize(operand, path, as_json: false) @operand = operand @path = check_steps(path, 'dig') @as_json = as_json end |
Instance Attribute Details
#as_json ⇒ Object (readonly)
Returns the value of attribute as_json.
511 512 513 |
# File 'lib/active_record/refined/ast.rb', line 511 def as_json @as_json end |
#operand ⇒ Object (readonly)
Returns the value of attribute operand.
511 512 513 |
# File 'lib/active_record/refined/ast.rb', line 511 def operand @operand end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
511 512 513 |
# File 'lib/active_record/refined/ast.rb', line 511 def path @path end |
Instance Method Details
#to_arel(table, model) ⇒ Object
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/active_record/refined/ast.rb', line 519 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 |