Class: Brine::Selecting::Traversal

Inherits:
Object
  • Object
show all
Defined in:
lib/brine/selecting.rb

Overview

Capture a path for which the value will be retrieved from a root.

Instance Method Summary collapse

Constructor Details

#initialize(path, is_plural) ⇒ Traversal

Return a traversal for the provided path.

Parameters:

  • path (String)

    Define the JSONPath for the location of the value(s) to retrieve.

  • is_plural (Boolean)

    Specify whether a collection should be returned for possibly multiple values, false if only a single value should be expected/returned.


163
164
165
166
# File 'lib/brine/selecting.rb', line 163

def initialize(path, is_plural)
  @path = path
  @message = is_plural ? :on : :first
end

Instance Method Details

#visit(root) ⇒ Object

Return values out of the provided root based on the traversal definition.

Parameters:

  • root (String)

    Provide the JSON structure from which the value will be retrieved.

Returns:

  • (Object)

    Return the value or values (if is_plural) at the defined path or nil if none found.


174
175
176
# File 'lib/brine/selecting.rb', line 174

def visit(root)
  !@path ? root : JsonPath.new("$.#{@path}").send(@message, root)
end