Module: Tc4r::Helpers

Defined in:
lib/tc4r/helpers.rb

Class Method Summary collapse

Class Method Details

.sym_score(obj) ⇒ Object



17
18
19
20
# File 'lib/tc4r/helpers.rb', line 17

def self.sym_score(obj)
  return obj.inject({}){|memo,(k,v)| memo[k.underscore.to_sym] =  symbolize(v); memo} if obj.is_a? Hash
  return obj
end

.symbolize(obj) ⇒ Object



3
4
5
6
7
# File 'lib/tc4r/helpers.rb', line 3

def self.symbolize(obj)
  return obj.inject({}){|memo,(k,v)| memo[k.to_sym] =  symbolize(v); memo} if obj.is_a? Hash
  return obj.inject([]){|memo,v    | memo           << symbolize(v); memo} if obj.is_a? Array
  return obj
end

.underscore(obj) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/tc4r/helpers.rb', line 9

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