class Enumpath::Resolver::Simple

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

Constants

NUMERIC_INDEX_TEST

Public Class Methods

resolve(variable, enum) click to toggle source

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

@param variable [String] the value to attempt to resolve @param enum [Enumerable] the enumerable to resolve the value against @return the resolved value, or nil if it could not be resolved

# 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

Private Class Methods

rescued_dig(enum, typecast_variable) click to toggle source
# File lib/enumpath/resolver/simple.rb, line 25
def rescued_dig(enum, typecast_variable)
  enum.dig(typecast_variable)
rescue NoMethodError, TypeError
  nil
end