Module: Ebay::Inflections

Constant Summary collapse

UPCASE_TOKENS =
/(Id$|Cs|Xsl|Url|Rtq|Aaq)/
DOWNCASE_TOKENS =
/^(eBx|EBx)/
NAME_MAPPINGS =
{
  'ebay' => 'eBay',
  'paypal' => 'PayPal',
  'vero' => 'VeRO'
}

Instance Method Summary collapse

Instance Method Details

#downcase_first_character(string) ⇒ Object



17
18
19
# File 'lib/ebay/inflections.rb', line 17

def downcase_first_character(string)
	string.sub(/^(.)/) { $1.downcase }
end

#ebay_camelize(lower_case_and_underscored_word) ⇒ Object



29
30
31
32
33
34
# File 'lib/ebay/inflections.rb', line 29

def ebay_camelize(lower_case_and_underscored_word)
  lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.
    gsub(/(^|_)(.)/) { $2.upcase }.
    gsub(/(#{NAME_MAPPINGS.keys.collect{|k| k.capitalize}.join('|')})/){ NAME_MAPPINGS[$1.downcase] }.
    gsub(UPCASE_TOKENS) { $1.upcase }
end

#ebay_underscore(camel_cased_word) ⇒ Object



36
37
38
39
40
41
# File 'lib/ebay/inflections.rb', line 36

def ebay_underscore(camel_cased_word)
  underscore(camel_cased_word.to_s.gsub(/(#{NAME_MAPPINGS.values.join('|')})/i){ upcase_first_character($1.downcase) }.
    gsub(DOWNCASE_TOKENS){ $1.downcase }.
    gsub(/(ids$)/i) { $1.upcase }
  )
end

#underscore(camel_cased_word) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ebay/inflections.rb', line 21

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

#upcase_first_character(string) ⇒ Object



13
14
15
# File 'lib/ebay/inflections.rb', line 13

def upcase_first_character(string)
	string.sub(/^(.)/) { $1.upcase }
end