Class: Radar::Support::Inflector

Inherits:
Object
  • Object
show all
Defined in:
lib/radar/support.rb

Overview

Inflector methods:

  • #camelize - Convert a string or symbol to UpperCamelCase.
  • #constantize - Convert a string to a constant.

Both of these inflector methods are taken directly from ActiveSupport in Rails 3.

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object



34
35
36
# File 'lib/radar/support.rb', line 34

def self.camelize(string)
  string.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

.constantize(camel_cased_word) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/radar/support.rb', line 38

def self.constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?
  names.inject(Object) do |acc, name|
    acc.const_defined?(name) ? acc.const_get(name) : acc.const_missing(name)
  end
end