partial-date 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog.textile CHANGED
@@ -1,3 +1,9 @@
1
+ h3. 1.0.0 / 2012-05-27
2
+
3
+ * Refactored to use array backing store for element and computed date
4
+ values for better performance.
5
+ * Implemented Comparable
6
+
1
7
  h3. 0.1.0 / 2012-05-26
2
8
 
3
9
  * Initial release:
data/README.textile CHANGED
@@ -16,7 +16,7 @@ h2. Examples
16
16
 
17
17
  bc.. require 'partial-date'
18
18
 
19
- #Default initializer
19
+ # Default initializer
20
20
  date = PartialDate::Date.new
21
21
  # =>
22
22
  date.value
@@ -50,7 +50,9 @@ h2. Install
50
50
 
51
51
  h2. TODO
52
52
 
53
- Implement @PartialDate::Date.parse@ method for construction from strings.
53
+ # @PartialDate::Date.parse@ method for construction from strings.
54
+ # A Hash or Array backing store for date components for better performance.
55
+ # Include Comparable
54
56
 
55
57
  h2. Copyright
56
58
 
@@ -17,6 +17,7 @@ module PartialDate
17
17
  # date = PartialDate::Date.new {|d| d.year = 2012 }
18
18
  #
19
19
  class Date
20
+ include Comparable
20
21
 
21
22
  # Public: Create a new partial date class from a block of integers
22
23
  # or strings.
@@ -35,10 +36,10 @@ module PartialDate
35
36
  #
36
37
  # Returns a date object.
37
38
  def initialize
39
+ @data = [0,0,0,0]
38
40
  yield self if block_given?
39
41
  end
40
42
 
41
-
42
43
  # Public: Get the integer date value in partial date format.
43
44
  #
44
45
  # Examples
@@ -49,7 +50,7 @@ module PartialDate
49
50
  #
50
51
  # Returns an integer representation of a partial date.
51
52
  def value
52
- @value ||= 0
53
+ get_value(0)
53
54
  end
54
55
 
55
56
  # Public: Set a date value using an interger in partial date format.
@@ -61,13 +62,14 @@ module PartialDate
61
62
  # Returns nothing
62
63
  def value=(value)
63
64
  if value.is_a?(Integer) && (value >= 10000 && value <= 99991231)
64
- @value = value
65
+ set_value(0, value)
65
66
  else
66
67
  raise PartialDateError, "Date value must be an integer betwen 10000 and 99991231"
67
68
  end
68
69
  end
69
70
 
70
- # Public: Loads an 8 digit date value into a date object.
71
+ # Public: Loads an 8 digit date value into a date object. Can be used
72
+ # when rehydrating a date object from a persisted partial date value.
71
73
  #
72
74
  # value - an 8 digit value in partial date format.
73
75
  #
@@ -114,7 +116,7 @@ module PartialDate
114
116
  end
115
117
 
116
118
  if value.is_a?(Integer) && (value <= 9999 && value > 0)
117
- @value = (self.value - self.year) * 10000 + (value * 10000)
119
+ set_value(1, value)
118
120
  else
119
121
  raise PartialDateError, "Year must be an integer between 1 and 9999"
120
122
  end
@@ -122,7 +124,7 @@ module PartialDate
122
124
 
123
125
  # Public: Get the year from a partial date.
124
126
  def year
125
- self.value > 9999 ? (self.value / 10000).abs : 0
127
+ get_value(1)
126
128
  end
127
129
 
128
130
  # Public: Set the month of a partial date.
@@ -141,7 +143,7 @@ module PartialDate
141
143
  end
142
144
 
143
145
  if value.is_a?(Integer) && (value <= 12 && value >= 0)
144
- @value = self.value - (self.month * 100) + (value * 100)
146
+ set_value(2, value)
145
147
  else
146
148
  raise PartialDateError, "Month must an be integer between 1 and 12"
147
149
  end
@@ -149,7 +151,7 @@ module PartialDate
149
151
 
150
152
  # Public: Get the month from a partial date.
151
153
  def month
152
- self.value > 99 ? ((self.value - (self.value / 10000).abs * 10000) / 100).abs : 0
154
+ get_value(2)
153
155
  end
154
156
 
155
157
 
@@ -172,7 +174,8 @@ module PartialDate
172
174
  if value.is_a?(Integer) && (value >= 0 && value <= 31)
173
175
  begin
174
176
  date = ::Date.civil(self.year, self.month, value) if value > 0
175
- @value = (self.value - self.day + value)
177
+ #@value = (self.value - self.day + value)
178
+ set_value(3, value)
176
179
  rescue
177
180
  raise PartialDateError, "Day must be a valid day for the given month"
178
181
  end
@@ -183,7 +186,8 @@ module PartialDate
183
186
 
184
187
  # Public: Get the day from a partial date.
185
188
  def day
186
- self.value > 0 ? self.value - (self.value / 100).abs * 100 : 0
189
+ #self.value > 0 ? self.value - (self.value / 100).abs * 100 : 0
190
+ get_value(3)
187
191
  end
188
192
 
189
193
  # Public: Returns a formatted string representation of the partial date.
@@ -205,5 +209,46 @@ module PartialDate
205
209
  return ""
206
210
  end
207
211
  end
212
+
213
+ # Public: Spaceship operator for date comparisons.
214
+ #
215
+ # Returns -1, 1, or 0
216
+ def <=>(other_date)
217
+ self.value <=> other_date.value
218
+ end
219
+
220
+
221
+ private
222
+
223
+ # Internal: Retreive a value from the array backing store.
224
+ #
225
+ # Returns an integer value for partial date, year, month or day.
226
+ def get_value(element)
227
+ @data[element]
228
+ end
229
+
230
+ # Internal: Set a value in the array backing store - either the
231
+ # complete date value (from load or value accessors), or a year,
232
+ # month, or day value after which the partial date will be recomputed.
233
+ #
234
+ # Returns nothing
235
+ def set_value(element, value)
236
+ case element
237
+ when 0
238
+ @data[1] = (value / 10000).abs
239
+ @data[2] = ((value - (value / 10000).abs * 10000) / 100).abs
240
+ @data[3] = value - (value / 100).abs * 100
241
+ when 1
242
+ @data[0] = @data[0] - (self.year * 10000) + (value * 10000)
243
+ when 2
244
+ @data[0] = @data[0] - (self.month * 100) + (value * 100)
245
+ when 3
246
+ @data[0] = @data[0] - self.day + value
247
+ end
248
+
249
+ # Important - update the old element value _after_ @data[0]
250
+ # has been recalculated above.
251
+ @data[element] = value
252
+ end
208
253
  end
209
254
  end
@@ -1,4 +1,4 @@
1
1
  module PartialDate
2
2
  # partial-date version
3
- VERSION = "0.1.0"
3
+ VERSION = "1.0.0"
4
4
  end
@@ -43,17 +43,17 @@ describe PartialDate::Date do
43
43
  expect {new_date = PartialDate::Date.new {|d| d.value = 100000000}}.to raise_error(PartialDate::PartialDateError, "Date value must be an integer betwen 10000 and 99991231")
44
44
  end
45
45
 
46
- it "should return a string represntation of date in the correct format" do
46
+ it "should return a string representation of date in the correct format" do
47
47
  new_date = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 31}
48
48
  new_date.to_s.should match(/\A\d{4}-\d{2}-\d{2}\z/)
49
49
  end
50
50
 
51
- it "should return a string represntation of a partial date in the correct format" do
51
+ it "should return a string representation of a partial date in the correct format" do
52
52
  new_date = PartialDate::Date.new {|d| d.year = 2012; d.month = 12}
53
53
  new_date.to_s.should match(/\A\d{4}-\d{2}\z/)
54
54
  end
55
55
 
56
- it "should return a string represntation of a partial date in the correct format" do
56
+ it "should return a string representation of a partial date in the correct format" do
57
57
  new_date = PartialDate::Date.new {|d| d.year = 2012}
58
58
  new_date.to_s.should match(/\A\d{4}\z/)
59
59
  end
@@ -164,4 +164,25 @@ describe PartialDate::Date do
164
164
  date.day.should == 10
165
165
  end
166
166
  end
167
+
168
+ describe "Comparisons" do
169
+ it "should determine if one date is greater than another" do
170
+ a = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 31}
171
+ b = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 30}
172
+ a.should be > b
173
+ end
174
+
175
+ it "should determine if one date is less than another" do
176
+ a = PartialDate::Date.new {|d| d.year = 2012; d.month = 0; d.day = 0}
177
+ b = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 30}
178
+ a.should be < b
179
+ end
180
+
181
+ it "should determine if one date is equal to another" do
182
+ a = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 30}
183
+ b = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 30}
184
+ a.should be == b
185
+ end
186
+ end
187
+
167
188
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: partial-date
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
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: 2012-05-26 00:00:00.000000000 Z
12
+ date: 2012-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubygems-tasks
@@ -82,7 +82,7 @@ files:
82
82
  - lib/partial-date/error.rb
83
83
  - lib/partial-date/version.rb
84
84
  - partial-date.gemspec
85
- - spec/partial-date_spec.rb
85
+ - spec/date_spec.rb
86
86
  - spec/spec_helper.rb
87
87
  homepage: https://github.com/58bits/partial-date#readme
88
88
  licenses:
@@ -111,5 +111,5 @@ specification_version: 3
111
111
  summary: A simple date class that can be used to store partial date values in a single
112
112
  column/attribute.
113
113
  test_files:
114
- - spec/partial-date_spec.rb
114
+ - spec/date_spec.rb
115
115
  - spec/spec_helper.rb