Class: JSONP3::Path::Query
- Inherits:
-
Object
- Object
- JSONP3::Path::Query
- Defined in:
- lib/json_p3/path/query.rb
Overview
A compiled JSONPath expression ready to be applied to JSON-like values.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Return true if this JSONPath expression has no segments.
-
#find(root) ⇒ Array<Node>
(also: #apply)
Apply this JSONPath expression to JSON-like value root.
-
#find_enum(root) ⇒ Enumerable<Node>
Apply this JSONPath expression to JSON-like value root.
-
#first(root) ⇒ Node | nil
Return the first node from applying this JSONPath expression to JSON-like value root.
-
#initialize(env, segments) ⇒ Query
constructor
A new instance of Query.
-
#match(root) ⇒ Node | nil
Return the first node from applying this JSONPath expression to JSON-like value root.
-
#match?(root) ⇒ bool
Return
trueif this query results in at least one node, orfalseotherwise. -
#singular? ⇒ Boolean
Return true if this JSONPath expression is a singular query.
- #to_s ⇒ Object
Constructor Details
#initialize(env, segments) ⇒ Query
Returns a new instance of Query.
9 10 11 12 |
# File 'lib/json_p3/path/query.rb', line 9 def initialize(env, segments) @env = env @segments = segments end |
Instance Method Details
#empty? ⇒ Boolean
Return true if this JSONPath expression has no segments.
69 70 71 |
# File 'lib/json_p3/path/query.rb', line 69 def empty? @segments.empty? end |
#find(root) ⇒ Array<Node> Also known as: apply
Apply this JSONPath expression to JSON-like value root.
21 22 23 24 25 |
# File 'lib/json_p3/path/query.rb', line 21 def find(root) nodes = [Node.new(root, [], root)] @segments.each { |segment| nodes = segment.resolve(nodes) } NodeList.new(nodes) end |
#find_enum(root) ⇒ Enumerable<Node>
Apply this JSONPath expression to JSON-like value root.
32 33 34 35 36 |
# File 'lib/json_p3/path/query.rb', line 32 def find_enum(root) nodes = [Node.new(root, [], root)] # : Enumerable[Node] @segments.each { |segment| nodes = segment.resolve_enum(nodes) } nodes end |
#first(root) ⇒ Node | nil
Return the first node from applying this JSONPath expression to JSON-like value root.
55 56 57 |
# File 'lib/json_p3/path/query.rb', line 55 def first(root) find_enum(root).first end |
#match(root) ⇒ Node | nil
Return the first node from applying this JSONPath expression to JSON-like value root.
41 42 43 |
# File 'lib/json_p3/path/query.rb', line 41 def match(root) find_enum(root).first end |
#match?(root) ⇒ bool
Return true if this query results in at least one node, or false otherwise.
48 49 50 |
# File 'lib/json_p3/path/query.rb', line 48 def match?(root) !find_enum(root).first.nil? end |
#singular? ⇒ Boolean
Return true if this JSONPath expression is a singular query.
60 61 62 63 64 65 66 |
# File 'lib/json_p3/path/query.rb', line 60 def singular? @segments.each do |segment| return false if segment.instance_of? DescendantSegment return false unless segment.selectors.length == 1 && segment.selectors[0].singular? end true end |
#to_s ⇒ Object
14 15 16 |
# File 'lib/json_p3/path/query.rb', line 14 def to_s "$#{@segments.join}" end |