35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/toon.rb', line 35
def toon(str, func=nil, *args, **opts, &code)
if block_given?
yield str
else
return if str.nil? case func
when nil then str
when 'age' dob = Date.new(*Time.parse_str(str)[0])
ref = args[0].respond_to?(:to_time) ? args[0].to_date : Date.today
yrs = ref.year - dob.year
yrs -= 1 if (ref.month < dob.month) || ((ref.month == dob.month) && (ref.day < dob.day))
yrs
when 'date_on' then str.to_tz.to_date rescue nil when 'time_at' then str.to_tz.getutc rescue nil when 'isodate' then str.to_tz. strftime("%F" ) rescue "" when 'isotime' then str.to_tz.getutc.strftime("%F %T") rescue ""
when 'date'
str.to_tz.strftime("%m/%d/%Y") rescue ""
when 'Time'
str.to_tz
when 'timestamp'
str.to_tz.strftime("%m/%d/%Y %H:%M:%S") rescue ""
when 'hispanic'
str =~ /hispanic|latin/i ? "Y" : "N"
when 'sex'
str =~ /\A(m|male|f|female|o|other)\z/i ? $1[0].upcase : '' when 'state'
$STATE_ABBREV[str.upcase] || ''
when 'to_decimal'
prec = 2
if str[/\A\s*\$?\s*([-+])?\s*\$?\s*([-+])?\s*(\d[,\d]*)?(\.\d*)?\s*\z/]
sign = "#{$1}#{$2}".squeeze.include?("-") ? "-" : ""
left = $3.blank? ? "0" : $3.delete(",")
decs = $4.blank? ? nil : $4
"%.*f" % [prec, "#{sign}#{left}#{decs}".to_f]
else
""
end
when 'to_map'
map = args[0]; map.is_a?(Hash) or raise "to_map unable to map using #{map.inspect}"
map.key?(str) ? map[str] : begin
case val = map[:else]
when :pass then str
when Proc then str.instance_eval(&val)
when Symbol then str.send(val)
else val
end
end
when 'to_phone', 'phone'
return "" if str.blank?
num = str.to_s.squeeze(' ').strip
num, ext = num.split(/\s*(?:ext?\.?|x|#|:|,)\s*/i, 2)
ext.gsub!(/\D+/,'') if ext
num = num.sub(/\A[^2-9]*/, '').gsub(/\D+/, '')
if num =~ /\A([2-9][0-8][0-9])([2-9]\d\d)(\d{4})\z/
num = "(#{$1}) #{$2}-#{$3}"
num << ", ext. #{ext}" if num && ext
else
num = ext = nil
end
num
when 'to_yyyymmdd_hmZ'
str.to_tz.utc.to_s[0...-4]
when 'to_yyyymmdd'
case str
when /^((?:19|20)\d{2})(\d{2})(\d{2})$/ then "%s%s%s" % [$1, $2, $3 ] when /^(\d{2})(\d{2})((?:19|20)\d{2})$/ then "%s%s%s" % [$3, $1, $2 ] when /^(\d{1,2})([-\/.])(\d{1,2})\2(\d{4})$/ then "%s%02d%02d" % [$4, $1.to_i, $3.to_i] when /^(\d{4})([-\/.])(\d{1,2})\2(\d{1,2})$/ then "%s%02d%02d" % [$1, $3.to_i, $4.to_i] when /^(\d{1,2})([-\/.])(\d{1,2})\2(\d{2})$/
year = $4.to_i
year += year < (Time.now.year % 100 + 5) ? 2000 : 1900
"%04d%02d%02d" % [year, $1.to_i, $3.to_i] else ""
end
when 'to_yyyymmdd_ymd'
toon(str, 'to_yyyymmdd') =~ /^(\d{4})(\d{2})(\d{2})$/ ? "#{$2}/#{$3}/#{$1}" : str
when 'to_yyyymmdd_ymd_iso'
str.to_tz.utc.to_s[0...-4]
when 'address'
toon(str, 'tune', opts: [:address])
when 'tune'
o = {}; opts.each {|e| o[e]=true}
s = str
s = s.downcase.gsub(/\s\s+/, ' ').strip.gsub(/(?<=^| |[\d[:punct:]])([[[:alpha:]]])/i) { $1.upcase } s.gsub!(/\b([a-z])\. ?([bcdfghjklmnpqrstvwxyz])\.?(?=\W|$)/i) { "#$1#$2".upcase } s.gsub!(/\b([a-z](?:[a-z&&[^aeiouy]]{1,4}))\b/i) { $1.upcase } s.gsub!(/\b([djs]r|us|acct|[ai]nn?|all|apps|ed|erb|esq|grp|in[cj]|of[cf]|st|up)\.?(?=\W|$)/i) { $1.capitalize } s.gsub!(/(^|(?<=\d ))?\b(and|at|as|of|the|in|not|on|or|for|to|by|de l[ao]s?|del?|(el-)|el|las)($)?\b/i) { ($1 || $3 || $4) ? $2.downcase.capitalize : $2.downcase } s.gsub!(/\b(mc|mac(?=d[ao][a-k,m-z][a-z]|[fgmpw])|[dol]')([a-z])/i) { $1.capitalize + $2.capitalize } s.gsub!(/\b(ahn|an[gh]|al|art[sz]?|ash|e[dnv]|echt|elms|emms|eng|epps|essl|i[mp]|mrs?|ms|ng|ock|o[hm]|ong|orr|orth|ost|ott|oz|sng|tsz|u[br]|ung)\b/i) { $1.capitalize } s.gsub!(/(?<=^| |[[:punct:]])(apt?s?|arch|ave?|bldg|blvd|cr?t|co?mn|drv?|elm|end|f[lt]|hts?|ln|old|pkw?y|plc?|prk|pt|r[dm]|spc|s[qt]r?|srt|street|[nesw])\.?(?=\W|$)/i) { $1.capitalize } s.gsub!(/(1st|2nd|3rd|[\d]th|de l[ao]s)\b/i) { $1.downcase } s.gsub!(/(?<=^|\d |\b[nesw] |\b[ns][ew] )(d?el|las?|los)\b/i) { $1.capitalize } s.gsub!(/\b(ca|dba|fbo|ihop|mri|ucla|usa|vru|[ns][ew]|i{1,3}v?)\b/i) { $1.upcase } s.gsub!(/\b([-@.\w]+\.(?:com|net|io|org))\b/i) { $1.downcase } s.gsub!(/# /, '#') s.sub!(/[.,#]+$/, '') s.sub!(/\bP\.? ?O\.? ?Box/i, 'PO Box') s
when 'zip', 'to_zip'
str =~ /^(\d{5})-?\d{4}?$/ ? $1 : '' when 'yn'
str =~ /\A(y|yes|n|no)\z/i ? $1[0].upcase : '' else
if str.respond_to?(func)
str.send(func, *args)
else
warn "dude... you gave me the unknown func #{func.inspect}"
nil
end
end
end
end
|