Class: Rjq::AST::Field

Inherits:
Node
  • Object
show all
Defined in:
lib/rjq/ast.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#source_span

Instance Method Summary collapse

Methods inherited from Node

#assign_value, #take, #with_source_span

Constructor Details

#initialize(base, name, optional: false) ⇒ Field

Returns a new instance of Field.



395
396
397
398
399
# File 'lib/rjq/ast.rb', line 395

def initialize(base, name, optional: false)
  @base = base
  @name = name
  @optional = optional
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



393
394
395
# File 'lib/rjq/ast.rb', line 393

def base
  @base
end

#nameObject (readonly)

Returns the value of attribute name.



393
394
395
# File 'lib/rjq/ast.rb', line 393

def name
  @name
end

Instance Method Details

#eval(input, context) ⇒ Object



401
402
403
404
405
406
407
# File 'lib/rjq/ast.rb', line 401

def eval(input, context)
  @base.eval(input, context).flat_map do |value|
    [read(value)]
  rescue Rjq::RuntimeError
    @optional ? [] : raise
  end
end

#invalid_path_message(result, _input, _context) ⇒ Object



413
414
415
416
# File 'lib/rjq/ast.rb', line 413

def invalid_path_message(result, _input, _context)
  "Invalid path expression near attempt to access element #{@name.inspect} of #{JSON::Dumper.dump(result,
                                                                                                  indent: nil)}"
end

#paths(input, context) ⇒ Object



409
410
411
# File 'lib/rjq/ast.rb', line 409

def paths(input, context)
  @base.paths(input, context).map { |path| path + [@name] }
end