Module: UmbrellioUtils::Constants
Instance Method Summary collapse
- #get_class(*name_parts) ⇒ Object
- #get_class!(*args) ⇒ Object
- #match_by_class!(**kwargs) ⇒ Object
- #safe_constantize(constant_name) ⇒ Object
Instance Method Details
#get_class(*name_parts) ⇒ Object
7 8 9 |
# File 'lib/umbrellio_utils/constants.rb', line 7 def get_class(*name_parts) safe_constantize(name_parts.join("/").underscore.camelize) end |
#get_class!(*args) ⇒ Object
11 12 13 |
# 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) ⇒ Object
20 21 22 23 24 25 26 |
# 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) ⇒ Object
15 16 17 18 |
# 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 |