Class: Enumpath::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/enumpath/path.rb,
lib/enumpath/path/normalized_path.rb

Overview

A mechanism for applying path expressions to enumerables and tracking results

Defined Under Namespace

Classes: NormalizedPath

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, result_type: nil) ⇒ Path

Returns a new instance of Path.

Parameters:

  • path (String, Array<String>)

    the path expression to apply to the enumerable

  • result_type (Symbol) (defaults to: nil)

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



16
17
18
19
20
# File 'lib/enumpath/path.rb', line 16

def initialize(path, result_type: nil)
  @path = path
  normalize!
  @results = Enumpath::Results.new(result_type: result_type)
end

Instance Attribute Details

#pathEnumpath::Path::NormalizedPath (readonly)

Returns the normalized path.

Returns:



9
10
11
# File 'lib/enumpath/path.rb', line 9

def path
  @path
end

#resultsEnumpath::Results (readonly)

Returns the current results array.

Returns:



12
13
14
# File 'lib/enumpath/path.rb', line 12

def results
  @results
end

Instance Method Details

#apply(enum) ⇒ Enumpath::Results

Note:

Calling this method resets the previous results array

Apply the path expression against an enumerable

Parameters:

  • enum (Enumerable)

    the enumerable to apply the path to

Returns:



28
29
30
31
32
# File 'lib/enumpath/path.rb', line 28

def apply(enum)
  results.clear
  trace(@path.dup, enum)
  results
end