Module: SheetsDB::Support

Defined in:
lib/sheets_db/support.rb

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object


4
5
6
7
# File 'lib/sheets_db/support.rb', line 4

def camelize(string)
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  string = string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
end

.constantize(string) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sheets_db/support.rb', line 9

def constantize(string)
  name_parts = camelize(string).split('::')
  name_parts.shift if name_parts.first.empty?
  constant = Object

  name_parts.each do |name_part|
    constant_defined = constant.const_defined?(name_part, false)
    constant = constant_defined ? constant.const_get(name_part) : constant.const_missing(name_part)
  end
  constant
end