Module: Ibanity::Util

Defined in:
lib/ibanity/util.rb

Class Method Summary collapse

Class Method Details

.camelize(string, uppercase_first_letter = true) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/ibanity/util.rb', line 26

def self.camelize(string, uppercase_first_letter = true)
  string = if uppercase_first_letter
             string.sub(/^[a-z\d]*/) { $&.capitalize }
           else
             string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase }
           end
  string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }.gsub("/", "::")
end

.underscore(camel_cased_word) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/ibanity/util.rb', line 17

def self.underscore(camel_cased_word)
  return camel_cased_word unless camel_cased_word =~ /[A-Z-]/
  word = camel_cased_word.to_s.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

.underscorize(obj) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/ibanity/util.rb', line 3

def self.underscorize(obj)
  case obj
  when Array
    obj.map { |e| underscorize(e) }
  when Hash
    obj.keys.reduce({}) do |result, key|
      result[underscore(key)] = underscorize(obj[key])
      result
    end
  else
    obj
  end
end