toon 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/toon.rb +41 -15
  3. data/toon.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 407ce9c69d6811c19ca1e44d7438f1057b8865c136afdd28a5339d701e627424
4
- data.tar.gz: 5868197f08fc85775d5089c87224b839013ce0e83c1727a012ab99b343fbb3c2
3
+ metadata.gz: 5f54590efdfb51223a8d93c3c7e060c7a17fa3825e437771dbf2227dd78b6eab
4
+ data.tar.gz: 15697b4e11e26b0c55abada9af9ab4eadca89f1466c55940e92aee2cca1e84e0
5
5
  SHA512:
6
- metadata.gz: 1e01d3e4d0acafeffb61417c545d2b26e94ddeee795478129b8e03b1ca3dbb6d460b5c246b12cc1088f226c765752696c724d12123a71c22053018526785ef8b
7
- data.tar.gz: e382956bbb6f6e629d3bed01cfb56cabc514222fa1fe7576b2a51989f85587d306a78b37c001ee42dce19d04b7108d5c796d8aa2da3138723c54b22bbd6a2f04
6
+ metadata.gz: c14737eb69fe4ae05b0769b7e4d258af682258cf37013ee46478637f3f798c98e22d2a9656ada4a26d8d541686465bc9a5ed249a3b839f38d5de1d01f66bd968
7
+ data.tar.gz: 5762f00e404a5c9a70b7d072eb47b0aaefd06f90ebc1a71c5598bceab7e0e3894c31e3d56b24fc7d8d7ceca2d478414e8019a528f620bc935ce2968bb6cea2b9
@@ -1,17 +1,10 @@
1
+ require 'zones'
2
+
1
3
  class Object
2
4
  def blank?
3
5
  respond_to?(:empty?) or return !self
4
6
  empty? or respond_to?(:strip) && strip.empty?
5
- end
6
-
7
- def present?
8
- !blank?
9
- end
10
-
11
- def present(default=nil)
12
- blank? ? default : self
13
- end
14
- alias :if_blank :present
7
+ end unless defined? blank?
15
8
  end
16
9
 
17
10
  $STATE_MAP ||= <<~end.split(/(?:\n| +)/).inject({}) {|h, e| h.store(*e.split(' ', 2)); h}
@@ -46,6 +39,24 @@ def toon(str, func=nil, *args, **opts, &code)
46
39
  return if str.nil? #!# TOO CRAZY?
47
40
  case func
48
41
  when nil then str
42
+ when 'age' #!# FIXME: what about timezone shifts, etc?
43
+ dob = Date.new(*Time.parse_str(str)[0])
44
+ ref = args[0].respond_to?(:to_time) ? args[0].to_date : Date.today
45
+ yrs = ref.year - dob.year
46
+ yrs -= 1 if (ref.month < dob.month) || ((ref.month == dob.month) && (ref.day < dob.day))
47
+ yrs
48
+ when 'date'
49
+ str.to_tz.strftime("%m/%d/%Y") rescue ""
50
+ when 'Time'
51
+ str.to_tz
52
+ when 'timestamp'
53
+ str.to_tz.strftime("%m/%d/%Y %H:%M:%S") rescue ""
54
+ when 'hispanic'
55
+ str =~ /hispanic|latin/i ? "Y" : "N"
56
+ when 'sex'
57
+ str =~ /\A(m|male|f|female|o|other)\z/i ? $1[0].upcase : '' # M/F/O
58
+ when 'state'
59
+ $STATE_ABBREV[str.upcase] || ''
49
60
  when 'to_decimal'
50
61
  prec = 2
51
62
  if str[/\A\s*\$?\s*([-+])?\s*\$?\s*([-+])?\s*(\d[,\d]*)?(\.\d*)?\s*\z/]
@@ -56,7 +67,17 @@ def toon(str, func=nil, *args, **opts, &code)
56
67
  else
57
68
  ""
58
69
  end
59
- when 'to_phone'
70
+ when 'to_map'
71
+ map = args[0]; map.is_a?(Hash) or raise "to_map unable to map using #{map.inspect}"
72
+ map.key?(str) ? map[str] : begin
73
+ case val = map[:else]
74
+ when :pass then str
75
+ when Proc then str.instance_eval(&val)
76
+ when Symbol then str.send(val)
77
+ else val
78
+ end
79
+ end
80
+ when 'to_phone', 'phone'
60
81
  return "" if str.blank?
61
82
  num = str.to_s.squeeze(' ').strip
62
83
  num, ext = num.split(/\s*(?:ext?\.?|x|#|:|,)\s*/i, 2)
@@ -69,6 +90,8 @@ def toon(str, func=nil, *args, **opts, &code)
69
90
  num = ext = nil
70
91
  end
71
92
  num
93
+ when 'to_yyyymmdd_hmZ'
94
+ str.to_tz.utc.to_s[0...-4]
72
95
  when 'to_yyyymmdd'
73
96
  case str
74
97
  when /^((?:19|20)\d{2})(\d{2})(\d{2})$/ then "%s%s%s" % [$1, $2, $3 ] # YYYYMMDD
@@ -83,14 +106,16 @@ def toon(str, func=nil, *args, **opts, &code)
83
106
  end
84
107
  when 'to_yyyymmdd_ymd'
85
108
  toon(str, 'to_yyyymmdd') =~ /^(\d{4})(\d{2})(\d{2})$/ ? "#{$2}/#{$3}/#{$1}" : str
109
+ when 'to_yyyymmdd_ymd_iso'
110
+ str.to_tz.utc.to_s[0...-4]
86
111
  when 'tune'
87
112
  o = {}; opts.each {|e| o[e]=true}
88
113
  s = str
89
114
  s = s.downcase.gsub(/\s\s+/, ' ').strip.gsub(/(?<=^| |[\d[:punct:]])([[[:alpha:]]])/i) { $1.upcase } # general case
90
115
  s.gsub!(/\b([a-z])\. ?([bcdfghjklmnpqrstvwxyz])\.?(?=\W|$)/i) { "#$1#$2".upcase } # initials (should this be :name only?)
91
116
  s.gsub!(/\b([a-z](?:[a-z&&[^aeiouy]]{1,4}))\b/i) { $1.upcase } # uppercase apparent acronyms
92
- s.gsub!(/\b([djs]r|us|acct|[ai]nn?|apps|ed|erb|esq|grp|in[cj]|of[cf]|st|up)\.?(?=\W|$)/i) { $1.capitalize } # force camel-case
93
- s.gsub!(/(^|(?<=\d ))?\b(and|at|as|of|the|in|on|or|for|to|by|de l[ao]s?|del?|(el-)|el|las)($)?\b/i) { ($1 || $3 || $4) ? $2.downcase.capitalize : $2.downcase } # prepositions
117
+ 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 } # force camel-case
118
+ 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 } # prepositions
94
119
  s.gsub!(/\b(mc|mac(?=d[ao][a-k,m-z][a-z]|[fgmpw])|[dol]')([a-z])/i) { $1.capitalize + $2.capitalize } # mixed case (Irish)
95
120
  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 } # if o[:name] # capitalize
96
121
  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 } # if o[:address] # road features
@@ -104,8 +129,9 @@ def toon(str, func=nil, *args, **opts, &code)
104
129
  s
105
130
  when 'zip', 'to_zip'
106
131
  str =~ /^(\d{5})-?\d{4}?$/ ? $1 : '' # only allow 5-digit zip codes
107
- when 'state'
108
- $STATE_ABBREV[str.upcase] || ''
132
+ when 'yn'
133
+ # {"Y"=>"Y","N"=>"N"}[str.to_s[0].upcase] || ""
134
+ str =~ /\A(y|yes|n|no)\z/i ? $1[0].upcase : '' # yes/no
109
135
  else
110
136
  if str.respond_to?(func)
111
137
  str.send(func, *args)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "toon"
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
  s.author = "Steve Shreeve"
7
7
  s.email = "steve.shreeve@gmail.com"
8
8
  s.summary = "A Ruby gem that makes it easy to cleanup and format data"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-26 00:00:00.000000000 Z
11
+ date: 2020-11-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem is helpful for ETL or other general data cleaning.
14
14
  email: steve.shreeve@gmail.com