Module: Nsync::CoreExtensions

Defined in:
lib/nsync/core_extensions.rb

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object

Upper case camelize



5
6
7
# File 'lib/nsync/core_extensions.rb', line 5

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

.constantize(string) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nsync/core_extensions.rb', line 17

def self.constantize(string)
  names = string.split('::')
  names.shift if names.size == 0 || names.first.size == 0

  constant = Object
  #Ruby 1.9 awesomeness
  if Module.method(:const_get).arity == 1
    names.each do |name|
      constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
    end
  else
    names.each do |name|
      constant = constant.const_get(name, false) || constant.const_missing(name)
    end
  end
  constant
end

.underscore(camel_cased_word) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/nsync/core_extensions.rb', line 9

def self.underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end