Module: Spectifly::Support

Defined in:
lib/spectifly/support.rb

Class Method Summary collapse

Class Method Details

.camelize(string, lower = false) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/spectifly/support.rb', line 5

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

.get_module(constant) ⇒ Object



26
27
28
29
30
# File 'lib/spectifly/support.rb', line 26

def get_module(constant)
  tokens = constant.to_s.split('::')
  module_name = tokens[0, tokens.length - 1].join('::')
  module_name == '' ? nil : module_name
end

.lower_camelize(string) ⇒ Object



14
15
16
# File 'lib/spectifly/support.rb', line 14

def lower_camelize(string)
  camelize(string, true)
end

.tokenize(string) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/spectifly/support.rb', line 18

def tokenize(string)
  return nil if string.nil?
  string = string.gsub(/&/, ' and ').
    gsub(/[ \/]+/, '_').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    downcase
end