Class: Affilinet::Middleware::Mash

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/affilinet/middleware/mash.rb

Class Method Summary collapse

Class Method Details

.camelize(term) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/affilinet/middleware/mash.rb', line 18

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

.camelize_keys(hash) ⇒ Object



11
12
13
14
15
16
# File 'lib/affilinet/middleware/mash.rb', line 11

def self.camelize_keys(hash)
  hash.keys.each do |key|
    hash[self.camelize(key)] = hash.delete key
  end
  hash
end

.join_arrays(hash) ⇒ Object



4
5
6
7
8
9
# File 'lib/affilinet/middleware/mash.rb', line 4

def self.join_arrays(hash)
  hash.each_pair do |key,value|
    hash[key] = value.join(',') if value.is_a? Array
  end
  hash
end

.underscore(camel_cased_word) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/affilinet/middleware/mash.rb', line 26

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