Module: Afterburn::Helpers
- Included in:
- TrelloObjectWrapper, TrelloObjectWrapper
- Defined in:
- lib/afterburn/helpers.rb
Instance Method Summary collapse
-
#constantize(camel_cased_word) ⇒ Object
Tries to find a constant with the name specified in the argument string:.
- #redis ⇒ Object
- #titleize(word) ⇒ Object
Instance Method Details
#constantize(camel_cased_word) ⇒ Object
Tries to find a constant with the name specified in the argument string:
constantize(“Module”) # => Module constantize(“Test::Unit”) # => Test::Unit
The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account:
C = ‘outside’ module M
C = 'inside'
C # => 'inside'
constantize("C") # => 'outside', same as ::C
end
NameError is raised when the constant is unknown.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/afterburn/helpers.rb', line 25 def constantize(camel_cased_word) camel_cased_word = camel_cased_word.to_s if camel_cased_word.include?('-') camel_cased_word = classify(camel_cased_word) end names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| args = Module.method(:const_get).arity != 1 ? [false] : [] if constant.const_defined?(name, *args) constant = constant.const_get(name) else constant = constant.const_missing(name) end end constant end |
#titleize(word) ⇒ Object
49 50 51 |
# File 'lib/afterburn/helpers.rb', line 49 def titleize(word) word.gsub(%r{\b('?[a-z])}) { $1.capitalize } end |