Module: Brine::Selecting

Included in:
Brine
Defined in:
lib/brine/selecting.rb

Overview

Provide assorted means to select particular values out of objects/graphs.

Defined Under Namespace

Classes: AllSelector, AnySelector, Selector, Traversal

Instance Method Summary collapse

Instance Method Details

#select(target, negated = nil) ⇒ Object

Activate a Selector for the provided target.

Parameters:

  • target (Object)

    Provide the value which the Selector should target.

  • negated (Boolean) (defaults to: nil)

    Specify whether the assertions should be expected to fail (DEPRECATED).



108
109
110
# File 'lib/brine/selecting.rb', line 108

def select(target, negated=nil)
  use_selector(Selector.new(target, negated))
end

#select_all(target, negated = nil) ⇒ Object

Activate a Selector for all of the children of the provided target.

Parameters:

  • target (Object)

    Provide the value which the Selector should target.

  • negated (Boolean) (defaults to: nil)

    Specify whether the assertions should be expected to fail (DEPRECATED).



128
129
130
# File 'lib/brine/selecting.rb', line 128

def select_all(target, negated=nil)
  use_selector(AllSelector.new(target, negated))
end

#select_any(target, negated = nil) ⇒ Object

Activate a Selector for any of the children of the provided target.

Parameters:

  • target (Object)

    Provide the value which the Selector should target.

  • negated (Boolean) (defaults to: nil)

    Specify whether the assertions should be expected to fail (DEPRECATED).



118
119
120
# File 'lib/brine/selecting.rb', line 118

def select_any(target, negated=nil)
  use_selector(AnySelector.new(target, negated))
end

#selectorSelector

Return the currently active Selector.

Returns:

  • (Selector)

    Return the Selector as set by #use_selector.



147
148
149
# File 'lib/brine/selecting.rb', line 147

def selector
  @selector
end

#traversal(path, is_plural) ⇒ Traversal

Return a Traversal based on the provided arguments.

This primarily exists as the exported interface to retrieve a Traversal instance.

Parameters:

  • path (String)

    Define the JSONPath to which the traversal should descend.

  • is_plural (Object)

    Specify whether to produce a traversal for a collection of all matches: nil will target only a single match, any non-nil value will target all matches.

Returns:

  • (Traversal)

    Return a Traversal configured based on the provided arguments.



189
190
191
# File 'lib/brine/selecting.rb', line 189

def traversal(path, is_plural)
  Traversal.new(path, !is_plural.nil?)
end

#use_selector(selector) ⇒ Object

Configure selector and make it the active Selector.

Parameters:

  • selector (Selector)

    Provide the selector which will be activated.



137
138
139
140
# File 'lib/brine/selecting.rb', line 137

def use_selector(selector)
  selector.coercer = coercer
  @selector = selector
end