Class: JSONP3::Path::Query

Inherits:
Object
  • Object
show all
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

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.

Returns:

  • (Boolean)


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.

Parameters:

  • root (Array, Hash, String, Integer, nil)

    the root JSON-like value to apply this query to.

Returns:

  • (Array<Node>)

    the sequence of nodes found while applying this query to 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.

Parameters:

  • root (Array, Hash, String, Integer, nil)

    the root JSON-like value to apply this query to.

Returns:

  • (Enumerable<Node>)

    the sequence of nodes found while applying this query to 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.

Parameters:

  • root (Array, Hash, String, Integer, nil)

    the root JSON-like value to apply this query to.

Returns:

  • (Node | nil)

    the first available node or nil if there were no matches.



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.

Parameters:

  • root (Array, Hash, String, Integer, nil)

    the root JSON-like value to apply this query to.

Returns:

  • (Node | nil)

    the first available node or nil if there were no matches.



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.

Parameters:

  • root (Array, Hash, String, Integer, nil)

    the root JSON-like value to apply this query to.

Returns:

  • (bool)

    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.

Returns:

  • (Boolean)


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_sObject



14
15
16
# File 'lib/json_p3/path/query.rb', line 14

def to_s
  "$#{@segments.join}"
end