Module: Helpers

Defined in:
lib/violet/helpers.rb

Class Method Summary collapse

Class Method Details

.constantize(camel_cased_word) ⇒ Object

==Credits Copyright (c) 2005 David Heinemeier Hansson taken from active_support/inflector.rb (MIT licence) see http://rubyforge.org/projects/activesupport

==Summary Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

==Examples Helpers.constantize("Module") #=> Module Helpers.constantize("Class") #=> Class



41
42
43
44
45
46
47
# File 'lib/violet/helpers.rb', line 41

def Helpers.constantize(camel_cased_word)
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
    raise NameError.new("#{camel_cased_word.inspect} is not a valid constant name!")
  end

  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end