Class: ActiveRecord::Refined::AST::JsonExcept

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

Overview

Keys taken out of a JSON document. PostgreSQL subtracts them, the other two remove a path apiece.

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?, #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, keys) ⇒ JsonExcept

Returns a new instance of JsonExcept.



741
742
743
744
# File 'lib/active_record/refined/ast.rb', line 741

def initialize(operand, keys)
  @operand = operand
  @keys = check_keys(keys)
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



739
740
741
# File 'lib/active_record/refined/ast.rb', line 739

def keys
  @keys
end

#operandObject (readonly)

Returns the value of attribute operand.



739
740
741
# File 'lib/active_record/refined/ast.rb', line 739

def operand
  @operand
end

Instance Method Details

#as_jsonObject



746
747
748
# File 'lib/active_record/refined/ast.rb', line 746

def as_json
  true
end

#to_arel(table, model) ⇒ Object



750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/active_record/refined/ast.rb', line 750

def to_arel(table, model)
  document = to_arel_operand(operand, table, model)
  if AST.adapter_family(model) == :postgresql
    # Grouped because - binds tighter than #>: dug out of a document,
    # the subtraction would otherwise take the path literal first.
    return Arel::Nodes::InfixOperation.new(
      :-, Arel::Nodes::Grouping.new(document), key_array)
  end

  Arel::Nodes::NamedFunction.new(
    'JSON_REMOVE',
    [document, *keys.map {|key| Arel::Nodes.build_quoted("$#{dollar_step(key)}") }])
end