Class: Hanami::Utils::Class
- Inherits:
-
Object
- Object
- Hanami::Utils::Class
- Defined in:
- lib/hanami/utils/class.rb
Overview
Class utilities
Constant Summary collapse
- TOKENIZE_REGEXP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regexp for .tokenize
/\((.*)\)/.freeze
- TOKENIZE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Separator for .tokenize
"|"
Class Method Summary collapse
-
.load(name, namespace = Object) ⇒ Class, ...
Loads a class for the given name, only if it’s defined.
-
.load!(name, namespace = Object) ⇒ Class, Module
Loads a class for the given name.
-
.load_from_pattern!(pattern, namespace = Object) ⇒ Class, Module
Loads a class from the given pattern name and namespace.
- .tokenize(pattern) ⇒ Object
Class Method Details
.load(name, namespace = Object) ⇒ Class, ...
Loads a class for the given name, only if it’s defined.
73 74 75 |
# File 'lib/hanami/utils/class.rb', line 73 def self.load(name, namespace = Object) load!(name, namespace) if namespace.const_defined?(name.to_s, false) end |
.load!(name, namespace = Object) ⇒ Class, Module
Loads a class for the given name.
42 43 44 |
# File 'lib/hanami/utils/class.rb', line 42 def self.load!(name, namespace = Object) namespace.const_get(name.to_s, false) end |
.load_from_pattern!(pattern, namespace = Object) ⇒ Class, Module
Loads a class from the given pattern name and namespace
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/hanami/utils/class.rb', line 114 def self.load_from_pattern!(pattern, namespace = Object) Deprecation.new("Hanami::Utils::Class.load_from_pattern! is deprecated, please use Hanami::Utils::Class.load! instead") # rubocop:disable Layout/LineLength tokenize(pattern) do |token| begin return namespace.const_get(token, false) rescue NameError # rubocop:disable Lint/SuppressedException end end full_name = [(namespace == Object ? nil : namespace), pattern].compact.join("::") raise NameError.new("uninitialized constant #{full_name}") end |
.tokenize(pattern) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/hanami/utils/class.rb', line 128 def self.tokenize(pattern) if match = TOKENIZE_REGEXP.match(pattern) pre = match.pre_match post = match.post_match tokens = match[1].split(TOKENIZE_SEPARATOR) tokens.each do |token| yield("#{pre}#{token}#{post}") end else yield(pattern) end nil end |