data_tools 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -183,6 +183,7 @@ module DataTools::Hash
183
183
  def cleanse(options = {})
184
184
  each_with_object({}) do |(k,v), out|
185
185
  out[k] = DataTools.scour(v, options)
186
+
186
187
  if dateformat = options[:datefields][k]
187
188
  begin
188
189
  out[k] = v && DateTime.strptime(v, dateformat).to_date
@@ -191,6 +192,15 @@ module DataTools::Hash
191
192
  out[k] = nil
192
193
  end
193
194
  end
195
+
196
+ if timeformat = options[:timefields][k]
197
+ begin
198
+ out[k] = v && DateTime.strptime(v, timeformat).to_time.utc
199
+ rescue ArgumentError
200
+ warn "expected '#{timeformat}' in #{k} = '#{v}' at [#{options[:line]}]: #{self}"
201
+ out[k] = nil
202
+ end
203
+ end
194
204
  end
195
205
  end
196
206
 
data/lib/data_tools/io.rb CHANGED
@@ -32,7 +32,8 @@ module DataTools::IO
32
32
  def import_options
33
33
  @import_options ||= {
34
34
  junkwords: [],
35
- datefields: {}
35
+ datefields: {},
36
+ timefields: {}
36
37
  }
37
38
  end
38
39
 
@@ -1,3 +1,3 @@
1
1
  module DataTools
2
- VERSION = "0.6.4"
2
+ VERSION = "0.6.5"
3
3
  end
data/spec/import_spec.rb CHANGED
@@ -17,6 +17,25 @@ describe "File Import" do
17
17
  f = File.open(File.dirname(__FILE__) + "/../tmp/visits.txt")
18
18
  recs = f.import(:format => :wsv, :datefields => {'admit_arrive_date' => '%Y-%m-%d'}).to_a
19
19
  recs.count.should == 99
20
- puts recs.sample
20
+ recs.shuffle.take(5).each {|rec| rec.keys.count.should == 3}
21
+ end
22
+
23
+ it "recognizes dates and times" do
24
+ f = File.open(File.dirname(__FILE__) + "/../tmp/timetest.txt")
25
+ config = {
26
+ :format => :tsv,
27
+ :datefields => {'EDIT_DATE' => '%Y-%m-%d'},
28
+ :timefields => {'EDIT_TIME' => '%Y-%m-%d %H:%M:%S'}
29
+ }
30
+ recs = f.import(config).to_a
31
+ recs.count.should == 99
32
+ dt = recs.first['EDIT_DATE']
33
+ tm = recs.first['EDIT_TIME']
34
+ dt.year.should == 2012
35
+ dt.month.should == 11
36
+ dt.day.should == 28
37
+ tm.hour.should == 0
38
+ tm.min.should == 40
39
+ tm.sec.should == 32
21
40
  end
22
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-05 00:00:00.000000000 Z
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print