Module: Waxx::Util

Extended by:
Util
Included in:
Util
Defined in:
waxx/util.rb

Overview

Waxx Copyright © 2016 ePark labs Inc. & Daniel J. Fitzpatrick <dan@eparklabs.com> All rights reserved. Released under the Apache Version 2 License. See LICENSE.txt.

Instance Method Summary collapse

Instance Method Details

#app_from_class(cls) ⇒ Object



35
36
37
# File 'waxx/util.rb', line 35

def app_from_class(cls)
  cls.to_s.split("::")[1]
end

#camel_case(str) ⇒ Object Also known as: camelize



7
8
9
# File 'waxx/util.rb', line 7

def camel_case(str)
  title_case(str.to_s.gsub(/[\._-]/,' ')).gsub(' ','')
end

#class_from_table(tbl) ⇒ Object



43
44
45
# File 'waxx/util.rb', line 43

def class_from_table(tbl)
  tbl.to_s.split("_").map{|i| i.capitalize}.join
end

#get_const(base_class, *names) ⇒ Object



47
48
49
50
51
# File 'waxx/util.rb', line 47

def get_const(base_class, *names)
  names.inject(base_class){|c, n|
    c.const_get(n.to_s.split("_").map{|i| i.capitalize}.join)
  }
end

#humanize(str) ⇒ Object



17
18
19
# File 'waxx/util.rb', line 17

def humanize(str)
  underscore(str.to_s).gsub('_',' ').capitalize
end

#label(str) ⇒ Object



13
14
15
# File 'waxx/util.rb', line 13

def label(str)
  title_case(humanize(str.to_s))
end

#qs(str) ⇒ Object



53
54
55
# File 'waxx/util.rb', line 53

def qs(str)
  Waxx::Http.qs(str)
end

#table_from_class(cls) ⇒ Object



39
40
41
# File 'waxx/util.rb', line 39

def table_from_class(cls)
  underscore(app_from_class(cls))
end

#title_case(str) ⇒ Object Also known as: titleize



21
22
23
# File 'waxx/util.rb', line 21

def title_case(str)
  str.to_s.split(' ').map{|t| t.capitalize}.join(' ')
end

#underscore(str) ⇒ Object



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

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