tabular 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -54,6 +54,11 @@ Tests
54
54
  There's basic test coverage. More comprehensive test coverage needs to be extracted from original projects. Run 'rake test'.
55
55
 
56
56
 
57
+ Changes
58
+ -------
59
+ 0.0.5 Parse 'invalid' m/d/yy dates
60
+
61
+
57
62
  Copyright
58
63
  ---------
59
64
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
data/lib/tabular/row.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "date"
2
+
1
3
  module Tabular
2
4
  # Associate list of cells. Each Table has a list of Rows. Access Row cells via symbols. Ex: row[:city]
3
5
  class Row
@@ -78,7 +80,16 @@ module Tabular
78
80
  if @array[index].is_a?(Date) || @array[index].is_a?(DateTime) || @array[index].is_a?(Time)
79
81
  @hash[column.key] = @array[index]
80
82
  else
81
- @hash[column.key] = Date.parse(@array[index], true)
83
+ begin
84
+ @hash[column.key] = Date.parse(@array[index], true)
85
+ rescue ArgumentError => e
86
+ date = parse_invalid_date(@array[index])
87
+ if date
88
+ @hash[column.key] = date
89
+ else
90
+ raise ArgumentError, "'#{@array[index]}' is not a valid date"
91
+ end
92
+ end
82
93
  end
83
94
  else
84
95
  @hash[column.key] = @array[index]
@@ -88,5 +99,33 @@ module Tabular
88
99
  end
89
100
  @hash
90
101
  end
102
+
103
+
104
+ private
105
+
106
+ # Handle common m/d/yy case that Date.parse dislikes
107
+ def parse_invalid_date(value)
108
+ return unless value
109
+
110
+ parts = value.split("/")
111
+ return unless parts.size == 3
112
+
113
+ month = parts[0].to_i
114
+ day = parts[1].to_i
115
+ year = parts[2].to_i
116
+ return unless month >=1 && month <= 12 && day >= 1 && day <= 31
117
+
118
+ if year == 0
119
+ year = 2000
120
+ elsif year > 0 && year < 69
121
+ year = 2000 + year
122
+ elsif year > 69 && year < 100
123
+ year = 1900 + year
124
+ elsif year < 1900 || year > 2050
125
+ return nil
126
+ end
127
+
128
+ Date.new(year, month, day)
129
+ end
91
130
  end
92
131
  end
data/tabular.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tabular}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Scott Willson"]
12
- s.date = %q{2010-06-09}
12
+ s.date = %q{2010-10-16}
13
13
  s.description = %q{Tabular is a Ruby library for reading, writing, and manipulating CSV, tab-delimited and Excel data.}
14
14
  s.email = %q{scott.willson@gmail.cpm}
15
15
  s.extra_rdoc_files = [
data/test/row_test.rb CHANGED
@@ -44,7 +44,8 @@ module Tabular
44
44
  def test_inspect
45
45
  table = Table.new([[ "planet", "star" ]])
46
46
  row = Row.new(table, [ "Mars", "Sun" ])
47
- assert_equal "{:planet=>\"Mars\", :star=>\"Sun\"}", row.inspect, "inspect"
47
+ assert_match %r{:planet=>"Mars"}, row.inspect, "inspect"
48
+ assert_match %r{:star=>"Sun"}, row.inspect, "inspect"
48
49
  end
49
50
 
50
51
  def test_to_s
@@ -60,6 +61,21 @@ module Tabular
60
61
  assert_equal nil, table.rows.first.previous, "previous of first Row"
61
62
  assert_equal "Mars", table.rows.last.previous[:planet], "previous"
62
63
  end
64
+
65
+ def test_invalid_date_raises_exception
66
+ table = Table.new([[ "launched" ]], :columns => { :launched => { :column_type => :date } })
67
+ row = Row.new(table, [ "99/z/99" ])
68
+ assert_raise ArgumentError do
69
+ row[:launched]
70
+ end
71
+ end
72
+
73
+ def test_parse_compact_american_dates
74
+ table = Table.new([[ "launched" ]], :columns => { :launched => { :column_type => :date } })
75
+ assert_equal Date.new(1999, 1, 1), Row.new(table, [ "1/1/99" ])[:launched], "1/1/99"
76
+ assert_equal Date.new(2000, 8, 28), Row.new(table, [ "8/28/00" ])[:launched], "8/28/00"
77
+ assert_equal Date.new(2008, 12, 31), Row.new(table, [ "12/31/08" ])[:launched], "12/31/08"
78
+ end
63
79
  end
64
80
  end
65
81
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Scott Willson
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-09 00:00:00 -07:00
17
+ date: 2010-10-16 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20