Class: JSONP3::JSONPath
- Inherits:
-
Object
- Object
- JSONP3::JSONPath
- Defined in:
- lib/json_p3/path.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<JSONPathNode>
(also: #apply)
Apply this JSONPath expression to JSON-like value root.
-
#find_enum(root) ⇒ Enumerable<JSONPathNode>
Apply this JSONPath expression to JSON-like value root.
-
#first(root) ⇒ JSONPathNode | nil
Return the first node from applying this JSONPath expression to JSON-like value root.
-
#initialize(env, segments) ⇒ JSONPath
constructor
A new instance of JSONPath.
-
#match(root) ⇒ JSONPathNode | nil
Return the first node from applying this JSONPath expression to JSON-like value root.
-
#match?(root) ⇒ bool
Return
true
if this query results in at least one node, orfalse
otherwise. -
#singular? ⇒ Boolean
Return true if this JSONPath expression is a singular query.
- #to_s ⇒ Object
Constructor Details
#initialize(env, segments) ⇒ JSONPath
Returns a new instance of JSONPath.
8 9 10 11 |
# File 'lib/json_p3/path.rb', line 8 def initialize(env, segments) @env = env @segments = segments end |
Instance Method Details
#empty? ⇒ Boolean
Return true if this JSONPath expression has no segments.
68 69 70 |
# File 'lib/json_p3/path.rb', line 68 def empty? @segments.empty? end |
#find(root) ⇒ Array<JSONPathNode> Also known as: apply
Apply this JSONPath expression to JSON-like value root.
20 21 22 23 24 |
# File 'lib/json_p3/path.rb', line 20 def find(root) nodes = [JSONPathNode.new(root, [], root)] @segments.each { |segment| nodes = segment.resolve(nodes) } JSONPathNodeList.new(nodes) # TODO: use JSONPathNodeList internally? end |
#find_enum(root) ⇒ Enumerable<JSONPathNode>
Apply this JSONPath expression to JSON-like value root.
31 32 33 34 35 |
# File 'lib/json_p3/path.rb', line 31 def find_enum(root) nodes = [JSONPathNode.new(root, [], root)] # : Enumerable[JSONPathNode] @segments.each { |segment| nodes = segment.resolve_enum(nodes) } nodes end |
#first(root) ⇒ JSONPathNode | nil
Return the first node from applying this JSONPath expression to JSON-like value root.
54 55 56 |
# File 'lib/json_p3/path.rb', line 54 def first(root) find_enum(root).first end |
#match(root) ⇒ JSONPathNode | nil
Return the first node from applying this JSONPath expression to JSON-like value root.
40 41 42 |
# File 'lib/json_p3/path.rb', line 40 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.
47 48 49 |
# File 'lib/json_p3/path.rb', line 47 def match?(root) !find_enum(root).first.nil? end |
#singular? ⇒ Boolean
Return true if this JSONPath expression is a singular query.
59 60 61 62 63 64 65 |
# File 'lib/json_p3/path.rb', line 59 def singular? @segments.each do |segment| return false if segment.instance_of? RecursiveDescentSegment return false unless segment.selectors.length == 1 && segment.selectors[0].singular? end true end |
#to_s ⇒ Object
13 14 15 |
# File 'lib/json_p3/path.rb', line 13 def to_s "$#{@segments.map(&:to_s).join}" end |