partial-date 1.1.8 → 1.1.9

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/ChangeLog.markdown CHANGED
@@ -1,3 +1,8 @@
1
+ ### 1.1.9 / 2012-05-31
2
+
3
+ * BugFix: to_s will preserve minus sign for negative year when formatter starts with %Y
4
+ * BugFix: <=> correct for negative dates.
5
+
1
6
  ### 1.1.8 / 2012-05-30
2
7
 
3
8
  * Implemented to_s formatters.
@@ -39,7 +39,7 @@ module PartialDate
39
39
 
40
40
  FORMATS = { :default => "%Y-%m-%d", :short => "%d %m %Y", :medium => "%d %b %Y", :long => "%d %B %Y", :number => "%Y%m%d", }
41
41
  FORMAT_METHODS = {
42
- "%Y" => lambda { |d| (d.year != 0) ? d.year.to_s.rjust(4, '0') : "" },
42
+ "%Y" => lambda { |d| (d.year != 0) ? d.year.to_s.rjust(4, '0') : ""},
43
43
  "%m" => lambda { |d| (d.month != 0) ? d.month.to_s.rjust(2, '0') : "" },
44
44
  "%b" => lambda { |d| (d.month != 0) ? ABBR_MONTH_NAMES[d.month - 1] : "" },
45
45
  "%B" => lambda { |d| (d.month != 0) ? MONTH_NAMES[d.month - 1] : "" },
@@ -266,22 +266,44 @@ module PartialDate
266
266
  result.gsub!( key, value.call( self )) if result.include? key
267
267
  end
268
268
 
269
- result.strip!
270
269
  # Remove any leading "/-," chars.
271
270
  # Remove double white spaces.
272
271
  # Remove any duplicate "/-," chars and replace with the single char.
273
272
  # Remove any trailing "/-," chars.
274
- # Anything else - the user is on their own ;-)
275
- result = result.gsub(/\A[\/,-]+/, '').gsub(/\s\s/, ' ').gsub(/[\/\-,]([\/\-,])/, '\1').gsub(/[\/,-]+\z/, '')
273
+ # Anything else - you're on your own ;-)
274
+ lead_trim = (year != 0 && format.lstrip.start_with?("%Y")) ? /\A[\/\,\s]+/ : /\A[\/\,\-\s]+/
275
+ result = result.gsub(lead_trim, '').gsub(/\s\s/, ' ').gsub(/[\/\-\,]([\/\-\,])/, '\1').gsub(/[\/\,\-\s]+\z/, '')
276
276
  end
277
277
 
278
- # Public: Spaceship operator for date comparisons. Comparisons are
279
- # made using the bit containing backing store. However the sign bit
280
- # is in MSB - so we need to left shift both values by 1 first.
278
+ # Public: Spaceship operator for date comparisons. Comparisons
279
+ # are made by cascading down from year, to month to day. This
280
+ # should be faster than passing to self.value <=> other_date.value
281
+ # since the integer value attribute requires multiplication to
282
+ # calculate.
281
283
  #
282
284
  # Returns -1, 1, or 0
283
285
  def <=>(other_date)
284
- (@bits << 1) <=> (other_date.bits << 1)
286
+ if self.year < other_date.year
287
+ return -1
288
+ elsif self.year > other_date.year
289
+ return 1
290
+ else
291
+ if self.month < other_date.month
292
+ return -1
293
+ elsif
294
+ self.month > other_date.month
295
+ return 1
296
+ else
297
+ if self.day < other_date.day
298
+ return -1
299
+ elsif
300
+ self.day > other_date.day
301
+ return 1
302
+ else
303
+ return 0
304
+ end
305
+ end
306
+ end
285
307
  end
286
308
 
287
309
 
@@ -1,4 +1,4 @@
1
1
  module PartialDate
2
2
  # partial-date version
3
- VERSION = "1.1.8"
3
+ VERSION = "1.1.9"
4
4
  end
data/spec/date_spec.rb CHANGED
@@ -47,20 +47,7 @@ describe PartialDate::Date do
47
47
  expect {new_date = PartialDate::Date.new {|d| d.value = 10485761232 }}.to raise_error(PartialDate::PartialDateError, "Date value must be an integer betwen -10485761231 and 10485761231")
48
48
  end
49
49
 
50
- it "should return a string representation of date in the correct format" do
51
- new_date = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 31}
52
- new_date.to_s.should match(/\A\d{4}-\d{2}-\d{2}\z/)
53
- end
54
-
55
- # it "should return a string representation of a partial date in the correct format" do
56
- # new_date = PartialDate::Date.new {|d| d.year = 2012; d.month = 12}
57
- # new_date.to_s.should match(/\\A\\d{4}-\\d{2}\\z/)
58
- # end
59
50
 
60
- # it "should return a string representation of a partial date in the correct format" do
61
- # new_date = PartialDate::Date.new {|d| d.year = 2012}
62
- # new_date.to_s.should match(/\\A\\d{4}\\z/)
63
- # end
64
51
 
65
52
  describe "Sign" do
66
53
  it "should be set to 1" do
@@ -91,6 +78,11 @@ describe PartialDate::Date do
91
78
  expect {date.year = 1048577 }.to raise_error(PartialDate::YearError, "Year must be an integer from -1048576 to 1048576")
92
79
  end
93
80
 
81
+ it "should allow a negative year to be set from the block" do
82
+ date = PartialDate::Date.new { |d| d.year = -1000 }
83
+ date.year.should == -1000
84
+ end
85
+
94
86
  it "should return a postive year when a positive year is set" do
95
87
  date.year = 2050
96
88
  date.year.should == 2050
@@ -238,4 +230,17 @@ describe PartialDate::Date do
238
230
  a.should be == b
239
231
  end
240
232
  end
233
+
234
+ describe "String formats" do
235
+
236
+ it "should return a string representation of date in the correct format" do
237
+ new_date = PartialDate::Date.new {|d| d.year = 2012; d.month = 12; d.day = 31}
238
+ new_date.to_s.should match(/\A\d{4}-\d{2}-\d{2}\z/)
239
+ end
240
+
241
+ it "should have a minus sign in front of negative dates" do
242
+ date = PartialDate::Date.new { |d| d.year = -1000; d.month = 12; d.day = 1}
243
+ date.to_s.should start_with("-")
244
+ end
245
+ end
241
246
  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: 1.1.8
4
+ version: 1.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: