Class: Enumpath::Results

Inherits:
Array
  • Object
show all
Defined in:
lib/enumpath/results.rb

Overview

An Array-like structure with syntactical sugar for storing the results of evaluating a path expression against an enumerable.

Constant Summary collapse

RESULT_TYPE_PATH =
:path
RESULT_TYPE_VALUE =
:value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result_type: RESULT_TYPE_VALUE) ⇒ Results

Returns a new instance of Results.

Parameters:

  • result_type (Symbol) (defaults to: RESULT_TYPE_VALUE)

    the type of result to store, :value (default) or :path



18
19
20
21
# File 'lib/enumpath/results.rb', line 18

def initialize(result_type: RESULT_TYPE_VALUE)
  @result_type = result_type
  super()
end

Instance Attribute Details

#result_typeSymbol (readonly)

Returns the current result type.

Returns:

  • (Symbol)

    the current result type



15
16
17
# File 'lib/enumpath/results.rb', line 15

def result_type
  @result_type
end

Instance Method Details

#apply(path, options = {}) ⇒ Enumpath::Results

Resolve a new path expression against the results of the last path expression

Parameters:

  • path (String, Array<String>)

    the path expression to apply to the enumerable

  • options (optional, Hash) (defaults to: {})

Returns:



28
29
30
# File 'lib/enumpath/results.rb', line 28

def apply(path, options = {})
  Enumpath.apply(path, self, options)
end

#store(resolved_path, value) ⇒ self

Adds a new result to the collection, the format of which is determined by the value of @result_type

Parameters:

  • resolved_path (Array)

    the path segments leading to the resolved value

  • value

    the resolved value

Returns:

  • (self)


37
38
39
40
41
42
43
44
45
# File 'lib/enumpath/results.rb', line 37

def store(resolved_path, value)
  result = if result_type == RESULT_TYPE_PATH
             as_path(resolved_path)
           else
             value
           end
  Enumpath.log('New Result') { { result: result } }
  push(result)
end