Class: Janeway::AST::Expression
- Inherits:
-
Object
- Object
- Janeway::AST::Expression
- Defined in:
- lib/janeway/ast/expression.rb
Overview
Base class for jsonpath expressions.
Every AST node is a subclass of this. This includes selectors, root and child identifiers, descendant segments, and the nodes that occur within a filter selector such as the current node identifier, operators and literals.
Direct Known Subclasses
BinaryOperator, Boolean, ChildSegment, CurrentNode, Function, Null, Number, RootNode, Selector, StringType, UnaryOperator
Instance Attribute Summary collapse
-
#next ⇒ Object
Next expression in the AST, if any.
-
#value ⇒ Object
Value provided by subclass constructor.
Class Method Summary collapse
-
.type_name ⇒ String
Cached camelcase→underscore transform of the class name.
Instance Method Summary collapse
-
#chain_of?(*allowed_classes) ⇒ Boolean
True if every selector in the @next chain is one of the allowed classes.
-
#indented(level, msg) ⇒ String
Return the given message, indented.
-
#initialize(val = nil) ⇒ Expression
constructor
A new instance of Expression.
-
#literal? ⇒ Boolean
Return true if this is a literal expression.
-
#singular_query? ⇒ Boolean
True if this is the root of a singular-query.
- #tree(level) ⇒ Array
- #type ⇒ String
Constructor Details
#initialize(val = nil) ⇒ Expression
Returns a new instance of Expression.
28 29 30 31 32 |
# File 'lib/janeway/ast/expression.rb', line 28 def initialize(val = nil) # don't set the instance variable if unused, because it makes the # "#inspect" output cleaner in rspec test failures @value = val unless val.nil? # literal false must be stored though! end |
Instance Attribute Details
#next ⇒ Object
Next expression in the AST, if any. Writable so the parser can link nodes as it constructs the chain, and so Query#dup / #pop can rewire the top-level chain.
26 27 28 |
# File 'lib/janeway/ast/expression.rb', line 26 def next @next end |
#value ⇒ Object
Value provided by subclass constructor.
21 22 23 |
# File 'lib/janeway/ast/expression.rb', line 21 def value @value end |
Class Method Details
.type_name ⇒ String
Cached camelcase→underscore transform of the class name. Same value on every instance of the class — compute once per class.
43 44 45 46 |
# File 'lib/janeway/ast/expression.rb', line 43 def self.type_name @type_name ||= Helpers.camelcase_to_underscore(name.split('::').last).freeze end |
Instance Method Details
#chain_of?(*allowed_classes) ⇒ Boolean
True if every selector in the @next chain is one of the allowed classes. An empty chain (no @next) returns true. Extracted from the identical implementations that used to live in CurrentNode and RootNode.
83 84 85 86 87 88 89 90 91 |
# File 'lib/janeway/ast/expression.rb', line 83 def chain_of?(*allowed_classes) selector = @next while selector return false unless allowed_classes.include?(selector.class) selector = selector.next end true end |
#indented(level, msg) ⇒ String
Return the given message, indented
53 54 55 |
# File 'lib/janeway/ast/expression.rb', line 53 def indented(level, msg) (INDENTS[level] || (INDENT * level)) + msg end |
#literal? ⇒ Boolean
Return true if this is a literal expression
65 66 67 |
# File 'lib/janeway/ast/expression.rb', line 65 def literal? false end |
#singular_query? ⇒ Boolean
True if this is the root of a singular-query.
73 74 75 |
# File 'lib/janeway/ast/expression.rb', line 73 def singular_query? false end |
#tree(level) ⇒ Array
59 60 61 |
# File 'lib/janeway/ast/expression.rb', line 59 def tree(level) [indented(level, to_s)] end |
#type ⇒ String
35 36 37 |
# File 'lib/janeway/ast/expression.rb', line 35 def type self.class.type_name end |