Foreman::Helpers

Public Instance Methods

classify(dashed_word) click to toggle source

Given a word with dashes, returns a camel cased version of it.

classify(‘job-name’) # => ‘JobName’

# File lib/foreman/helpers.rb, line 7
def classify(dashed_word)
  dashed_word.split('-').each { |part| part[0] = part[0].chr.upcase }.join
end
constantize(camel_cased_word) click to toggle source

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.

# File lib/foreman/helpers.rb, line 27
def constantize(camel_cased_word)
  camel_cased_word = camel_cased_word.to_s

  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

[Validate]

Generated with the Darkfish Rdoc Generator 2.