module UmbrellioUtils::Constants

Public Instance Methods

get_class(*name_parts) click to toggle source
# File lib/umbrellio_utils/constants.rb, line 7
def get_class(*name_parts)
  safe_constantize(name_parts.join("/").underscore.camelize)
end
get_class!(*args) click to toggle source
# File lib/umbrellio_utils/constants.rb, line 11
def get_class!(*args)
  get_class(*args) or raise "Failed to get class for #{args.inspect}"
end
match_by_class!(**kwargs) click to toggle source
# File lib/umbrellio_utils/constants.rb, line 20
def match_by_class!(**kwargs)
  name, instance = kwargs.shift
  result = kwargs.find { |klass, _| instance.is_a?(klass) }&.last
  raise "Unsupported #{name} type: #{instance.inspect}" if result.nil?

  result.is_a?(Proc) ? result.call : result
end
safe_constantize(constant_name) click to toggle source
# File lib/umbrellio_utils/constants.rb, line 15
def safe_constantize(constant_name)
  constant = suppress(NameError) { Object.const_get(constant_name, false) }
  constant if constant && constant.name == constant_name
end