Class: CSVDecision2::Matchers::Proc Private

Inherits:
Array
  • Object
show all
Defined in:
lib/csv_decision2/matchers.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Composite object for a data cell proc. Note that we do not need it to be comparable. Implemented as an immutable array of 2 or 3 entries for memory compactness and speed.

Instance Method Summary collapse

Constructor Details

#initialize(type:, function:, symbols: nil) ⇒ Proc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Proc.

Parameters:

  • type (Symbol)

    Type of the function value - e.g., :constant or :guard.

  • function (Object)

    Either a lambda function, or some kind of constant such as an Integer.

  • symbols (nil, Symbol, Array<Symbol>) (defaults to: nil)

    The symbol or list of symbols that the function uses to reference input hash keys (which are always symbolized).



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/csv_decision2/matchers.rb', line 20

def initialize(type:, function:, symbols: nil)
  super()

  self << type

  # Function values should always be frozen
  self << function.freeze

  # Some function values, such as constants or 0-arity functions, do not reference symbols.
  self << symbols if symbols

  freeze
end

Instance Method Details

#call(hash:, value: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Value returned from function call.

Parameters:

  • hash (Hash)

    Input hash to function call.

  • value (Object) (defaults to: nil)

    Input value to function call.

Returns:

  • (Object)

    Value returned from function call.



37
38
39
40
41
42
43
44
# File 'lib/csv_decision2/matchers.rb', line 37

def call(hash:, value: nil)
  func = fetch(1)

  return func.call(hash) if fetch(0) == :guard

  # All other procs can take one or two args
  func.arity == 1 ? func.call(value) : func.call(value, hash)
end

#functionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Either a lambda function, or some kind of constant such as an Integer.

Returns:

  • (Object)

    Either a lambda function, or some kind of constant such as an Integer.



52
53
54
# File 'lib/csv_decision2/matchers.rb', line 52

def function
  fetch(1)
end

#symbolsnil, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The symbol or list of symbols that the function uses to reference input hash keys (which are always symbolized).

Returns:

  • (nil, Symbol, Array<Symbol>)

    The symbol or list of symbols that the function uses to reference input hash keys (which are always symbolized).



58
59
60
# File 'lib/csv_decision2/matchers.rb', line 58

def symbols
  fetch(2, nil)
end

#typeSymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Type of the function value - e.g., :constant or :guard.

Returns:

  • (Symbol)

    Type of the function value - e.g., :constant or :guard.



47
48
49
# File 'lib/csv_decision2/matchers.rb', line 47

def type
  fetch(0)
end