Module: CanSelfDoIt::Helper

Defined in:
lib/can_self_do_it/helper.rb

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object



20
21
22
# File 'lib/can_self_do_it/helper.rb', line 20

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

.class_2_method_sub_str(a_class) ⇒ Object



4
5
6
7
# File 'lib/can_self_do_it/helper.rb', line 4

def self.class_2_method_sub_str(a_class)
  str = a_class.to_s
  str.respond_to?(:underscore) ?  str.underscore.sub('/','__') : self.underscore(str).sub('/','__')
end

.underscore(camel_cased_word) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/can_self_do_it/helper.rb', line 9

def self.underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/(?:([A-Za-z\d])|^)(#{/(?=a)b/})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end