Class: Janeway::Query
- Inherits:
-
Object
- Object
- Janeway::Query
- Defined in:
- lib/janeway/query.rb
Overview
Query holds the abstract syntax tree created by parsing the query. This can be frozen and passed to multiple threads or ractors for simultaneous use. No instance members are modified during the interpretation stage.
Instance Attribute Summary collapse
-
#jsonpath ⇒ String
readonly
The original jsonpath query, for use in error messages.
- #root ⇒ AST::Root readonly
Instance Method Summary collapse
-
#==(other) ⇒ Object
Queries are considered equal if their ASTs evaluate to the same JSONPath string.
-
#enum_for(input) ⇒ Janeway::Enumerator
Combine this query with input to make an Enumerator.
-
#initialize(root_node, jsonpath) ⇒ Query
constructor
A new instance of Query.
-
#node_list ⇒ Array<Expression>
Return a list of the nodes in the AST.
- #to_s ⇒ Object
-
#tree ⇒ Object
Print AST in tree format Every AST class prints a 1-line representation of self, with children on separate lines.
Constructor Details
#initialize(root_node, jsonpath) ⇒ Query
Returns a new instance of Query.
17 18 19 20 21 22 23 |
# File 'lib/janeway/query.rb', line 17 def initialize(root_node, jsonpath) raise ArgumentError, "expect root identifier, got #{root_node.inspect}" unless root_node.is_a?(AST::RootNode) raise ArgumentError, "expect query string, got #{jsonpath.inspect}" unless jsonpath.is_a?(String) @root = root_node @jsonpath = jsonpath end |
Instance Attribute Details
#jsonpath ⇒ String (readonly)
The original jsonpath query, for use in error messages
10 11 12 |
# File 'lib/janeway/query.rb', line 10 def jsonpath @jsonpath end |
#root ⇒ AST::Root (readonly)
13 14 15 |
# File 'lib/janeway/query.rb', line 13 def root @root end |
Instance Method Details
#==(other) ⇒ Object
Queries are considered equal if their ASTs evaluate to the same JSONPath string.
The string output is generated from the AST and should be considered a “normalized” form of the query. It may have different whitespace and parentheses than the original input but will be semantically equivalent.
58 59 60 |
# File 'lib/janeway/query.rb', line 58 def ==(other) to_s == other.to_s end |
#enum_for(input) ⇒ Janeway::Enumerator
Combine this query with input to make an Enumerator. This can be used to iterate over results with #each, #map, etc.
29 30 31 |
# File 'lib/janeway/query.rb', line 29 def enum_for(input) Janeway::Enumerator.new(self, input) end |
#node_list ⇒ Array<Expression>
Return a list of the nodes in the AST. The AST of a jsonpath query is a straight line, so this is expressible as an array. The only part of the AST with branches is inside a filter selector, but that doesn’t show up here.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/janeway/query.rb', line 41 def node_list nodes = [] node = @root loop do nodes << node break unless node.next node = node.next end nodes end |
#to_s ⇒ Object
33 34 35 |
# File 'lib/janeway/query.rb', line 33 def to_s @root.to_s end |
#tree ⇒ Object
Print AST in tree format Every AST class prints a 1-line representation of self, with children on separate lines
64 65 66 67 68 |
# File 'lib/janeway/query.rb', line 64 def tree result = @root.tree(0) result.flatten.join("\n") end |