Module: Toolhound::Util

Included in:
Base, Client
Defined in:
lib/toolhound-ruby/util.rb

Instance Method Summary collapse

Instance Method Details

#acronym_regexObject



8
9
10
# File 'lib/toolhound-ruby/util.rb', line 8

def acronym_regex
  /#{acronyms.values.join("|")}/
end

#acronymsObject

class << self



5
6
7
# File 'lib/toolhound-ruby/util.rb', line 5

def acronyms
  {"rest" => "REST", "html" =>"HTML", "id" => "ID", "url" => "URL"}
end

#camelize(term, uppercase_first_letter = true) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/toolhound-ruby/util.rb', line 32

def camelize(term, uppercase_first_letter = true)
  string = term.to_s
  if uppercase_first_letter
    string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
  else
    string = string.sub(/^(?:#{acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
  end
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{acronyms[$2] || $2.capitalize}" }
  string.gsub!('/'.freeze, '::'.freeze)
  string
end

#demodulize(path) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/toolhound-ruby/util.rb', line 12

def demodulize(path)
  path = path.to_s
  if i = path.rindex('::')
    path[(i+2)..-1]
  else
    path
  end
end

#underscore(camel_cased_word) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/toolhound-ruby/util.rb', line 21

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