Class: Enumpath::Resolver::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/enumpath/resolver/simple.rb

Overview

A utility for resolving a string as an index, key, or member of an enumerable

Constant Summary collapse

NUMERIC_INDEX_TEST =
/\A\-?(?:0|[1-9][0-9]*)\z/

Class Method Summary collapse

Class Method Details

.resolve(variable, enum) ⇒ Object

Attempts to resolve a string as an index, key, or member of an enumerable

Parameters:

  • variable (String)

    the value to attempt to resolve

  • enum (Enumerable)

    the enumerable to resolve the value against

Returns:

  • the resolved value, or nil if it could not be resolved



15
16
17
18
19
20
21
# File 'lib/enumpath/resolver/simple.rb', line 15

def resolve(variable, enum)
  variable = variable.to_s
  value = rescued_dig(enum, variable.to_i) if variable =~ NUMERIC_INDEX_TEST
  value = rescued_dig(enum, variable) if value.nil?
  value = rescued_dig(enum, variable.to_sym) if value.nil?
  value
end