nickel 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,7 @@ module Nickel
10
10
  DATE_DD_WITH_SUFFIX_NB_ON_SUFFIX = %r{\b(0?[1-9]|[12][0-9]|3[01])(?:st|nd|rd|th)\b}
11
11
  DATE_MM_SLASH_DD = %r{\b(?:0?[1-9]|[1][0-2])\/(?:0?[1-9]|[12][0-9]|3[01])}
12
12
  DAY_OF_WEEK = %r{\b(mon|tue|wed|thu|fri|sat|sun)\b}
13
- DAY_OF_WEEK_NB = %r{\b(?:mon|tue|wed|thu|fri|sat|sun)\b} #no backreference
13
+ DAY_OF_WEEK_NB = %r{\b(?:mon|tue|wed|thu|fri|sat|sun)\b} # no backreference
14
14
  MONTH_OF_YEAR = %r{\b(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\b}
15
15
  MONTH_OF_YEAR_NB = %r{\b(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\b}
16
16
  TIME_24HR = %r{[01]?[0-9]|2[0-3]:[0-5][0-9]}
@@ -1,24 +1,23 @@
1
1
  module Nickel
2
-
3
2
  # Some notes about this class, type can take the following values:
4
3
  # :single, :daily, :weekly, :daymonthly, :datemonthly,
5
4
  Occurrence = Struct.new(:type, :start_date, :end_date, :start_time, :end_time, :interval, :day_of_week, :week_of_month, :date_of_month) do
6
5
 
7
6
  def initialize(h)
8
- h.each { |k,v| send("#{k}=", v) }
7
+ h.each { |k, v| send("#{k}=", v) }
9
8
  end
10
9
 
11
10
  def inspect
12
- "#<Occurrence " + members.select{|m| self[m]}.map{|m| %(#{m}: #{self[m]})}.join(", ") + ">"
11
+ '#<Occurrence ' + members.select { |m| self[m] }.map { |m| %(#{m}: #{self[m]}) }.join(', ') + '>'
13
12
  end
14
13
 
15
14
  def finalize(cur_date)
16
15
  cur_date = start_date unless start_date.nil?
17
16
  case type
18
- when :daily then finalize_daily(cur_date)
19
- when :weekly then finalize_weekly(cur_date)
20
- when :datemonthly then finalize_datemonthly(cur_date)
21
- when :daymonthly then finalize_daymonthly(cur_date)
17
+ when :daily then finalize_daily(cur_date)
18
+ when :weekly then finalize_weekly(cur_date)
19
+ when :datemonthly then finalize_datemonthly(cur_date)
20
+ when :daymonthly then finalize_daymonthly(cur_date)
22
21
  end
23
22
  end
24
23
 
@@ -33,7 +32,7 @@ module Nickel
33
32
  # starting DATE"; we want to find the first occurrence after DATE
34
33
  self.start_date = cur_date.this(day_of_week)
35
34
 
36
- if !end_date.nil?
35
+ unless end_date.nil?
37
36
  # find the real end date, if someone says "every monday until
38
37
  # dec 1"; find the actual last occurrence
39
38
  self.end_date = end_date.prev(day_of_week)
@@ -1,3 +1,3 @@
1
1
  module Nickel
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -1,15 +1,14 @@
1
1
  require 'date'
2
2
 
3
3
  module Nickel
4
-
5
4
  # TODO: get methods should accept dayname or dayindex
6
5
  class ZDate
7
6
  include Comparable
8
7
 
9
- @days_of_week = ["mon","tue","wed","thu","fri","sat","sun"]
10
- @full_days_of_week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
11
- @months_of_year = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
12
- @full_months_of_year = ["january","february","march","april","may","june","july","august","september","october","november","december"]
8
+ @days_of_week = %w(mon tue wed thu fri sat sun)
9
+ @full_days_of_week = %w(monday tuesday wednesday thursday friday saturday sunday)
10
+ @months_of_year = %w(jan feb mar apr may jun jul aug sep oct nov dec)
11
+ @full_months_of_year = %w(january february march april may june july august september october november december)
13
12
  @days_in_common_year_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
14
13
  @days_in_leap_year_months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
15
14
 
@@ -27,8 +26,8 @@ module Nickel
27
26
 
28
27
  # Don't use attr_accessor for date, year, month, day; we want to validate on change.
29
28
  def initialize(yyyymmdd = nil)
30
- d = yyyymmdd ? yyyymmdd.dup : ::Time.new.strftime("%Y%m%d")
31
- d.gsub!(/-/,'') # remove any hyphens, so a user can initialize with something like "2008-10-23"
29
+ d = yyyymmdd ? yyyymmdd.dup : ::Time.new.strftime('%Y%m%d')
30
+ d.gsub!(/-/, '') # remove any hyphens, so a user can initialize with something like "2008-10-23"
32
31
  self.date = d
33
32
  end
34
33
 
@@ -70,25 +69,30 @@ module Nickel
70
69
  end
71
70
 
72
71
  def fmt(txt)
73
- txt.gsub!(/%Y/, self.year_str)
74
- txt.gsub!(/%m/, self.month_str)
75
- txt.gsub!(/%d/, self.day_str)
72
+ txt.gsub!(/%Y/, year_str)
73
+ txt.gsub!(/%m/, month_str)
74
+ txt.gsub!(/%d/, day_str)
76
75
  end
77
76
 
78
- def <=>(d2)
79
- return nil unless [:year, :month, :day].all?{|m| d2.respond_to?(m)}
77
+ def <=>(other)
78
+ return nil unless [:year, :month, :day].all? { |m| other.respond_to?(m) }
80
79
 
81
- if before?(d2)
80
+ if before?(other)
82
81
  -1
83
- elsif after?(d2)
82
+ elsif after?(other)
84
83
  1
85
84
  else
86
85
  0
87
86
  end
88
87
  end
89
88
 
90
- # returns true if self is today
91
89
  def is_today?
90
+ warn '[DEPRECATION] `is_today?` is deprecated. Please use `today?` instead.'
91
+ today?
92
+ end
93
+
94
+ # returns true if self is today
95
+ def today?
92
96
  self == ZDate.new
93
97
  end
94
98
 
@@ -103,7 +107,9 @@ module Nickel
103
107
  # we want the last occurrence of this month
104
108
  # add 4 weeks to first occurrence, see if we are in the same month, subtract 1 week if we are not
105
109
  d = first_occ_date.add_weeks(4)
106
- if d.month != self.month then d = d.sub_weeks(1) end
110
+ if d.month != month
111
+ d = d.sub_weeks(1)
112
+ end
107
113
  end
108
114
  d
109
115
  end
@@ -120,12 +126,12 @@ module Nickel
120
126
 
121
127
  # for example, "previous friday"
122
128
  def prev(day)
123
- (self.dayindex == day) ? self.dup : x_weeks_from_day(-1,day)
129
+ (dayindex == day) ? dup : x_weeks_from_day(-1, day)
124
130
  end
125
131
 
126
132
  # returns a new date object
127
133
  def x_weeks_from_day(weeks_away, day2index)
128
- day1index = self.dayindex
134
+ day1index = dayindex
129
135
  if day1index > day2index
130
136
  days_away = 7 * (weeks_away + 1) - (day1index - day2index)
131
137
  elsif day1index < day2index
@@ -133,20 +139,22 @@ module Nickel
133
139
  elsif day1index == day2index
134
140
  days_away = 7 * weeks_away
135
141
  end
136
- self.add_days(days_away) # returns a new date object
142
+ add_days(days_away) # returns a new date object
137
143
  end
138
144
 
139
145
  # add_ methods return new ZDate object, they DO NOT modify self
140
146
  def add_days(number)
141
- if number < 0 then return sub_days(number.abs) end
142
- o = self.dup # new ZDate object
147
+ if number < 0
148
+ return sub_days(number.abs)
149
+ end
150
+ o = dup # new ZDate object
143
151
  # Let's see what month we are going to end in
144
152
  while number > 0
145
153
  if o.days_left_in_month >= number
146
154
  o.date = ZDate.format_date(o.year_str, o.month_str, o.day + number)
147
155
  number = 0
148
156
  else
149
- number = number - 1 - o.days_left_in_month #it costs 1 day to increment the month
157
+ number = number - 1 - o.days_left_in_month # it costs 1 day to increment the month
150
158
  o.increment_month!
151
159
  end
152
160
  end
@@ -154,7 +162,7 @@ module Nickel
154
162
  end
155
163
 
156
164
  def add_weeks(number)
157
- self.add_days(7*number)
165
+ add_days(7 * number)
158
166
  end
159
167
 
160
168
  def add_months(number)
@@ -165,14 +173,14 @@ module Nickel
165
173
  years_to_increment = 0
166
174
  end
167
175
  new_year = year + years_to_increment
168
- new_day = get_day_or_max_day_in_month(self.day, new_month, new_year)
176
+ new_day = get_day_or_max_day_in_month(day, new_month, new_year)
169
177
  ZDate.new(ZDate.format_date(new_year, new_month, new_day))
170
178
  end
171
179
 
172
180
  def add_years(number)
173
181
  new_year = year + number
174
- new_day = get_day_or_max_day_in_month(self.day, self.month, new_year)
175
- ZDate.new(ZDate.format_date(new_year, self.month_str, new_day))
182
+ new_day = get_day_or_max_day_in_month(day, month, new_year)
183
+ ZDate.new(ZDate.format_date(new_year, month_str, new_day))
176
184
  end
177
185
 
178
186
  # DEPRECATED, change_ methods in ZTime modify self, this was confusing,
@@ -190,7 +198,7 @@ module Nickel
190
198
  # returns the first day of the month
191
199
  def jump_to_month(month_number)
192
200
  # find difference in months
193
- if month_number >= self.month
201
+ if month_number >= month
194
202
  ZDate.new(ZDate.format_date(year_str, month_number))
195
203
  else
196
204
  ZDate.new(ZDate.format_date(year + 1, month_number))
@@ -203,24 +211,24 @@ module Nickel
203
211
  end
204
212
 
205
213
  def end_of_month
206
- ZDate.new(ZDate.format_date(year_str, month_str, self.days_in_month))
214
+ ZDate.new(ZDate.format_date(year_str, month_str, days_in_month))
207
215
  end
208
216
 
209
217
  def beginning_of_next_month
210
- o = self.dup
218
+ o = dup
211
219
  o.increment_month!
212
220
  o
213
221
  end
214
222
 
215
223
  # sub_ methods return new ZDate object, they do not modify self.
216
224
  def sub_days(number)
217
- o = self.dup
225
+ o = dup
218
226
  while number > 0
219
227
  if (o.day - 1) >= number
220
228
  o.date = ZDate.format_date(o.year_str, o.month_str, o.day - number)
221
229
  number = 0
222
230
  else
223
- number = number - o.day
231
+ number -= o.day
224
232
  o.decrement_month!
225
233
  end
226
234
  end
@@ -228,11 +236,11 @@ module Nickel
228
236
  end
229
237
 
230
238
  def sub_weeks(number)
231
- self.sub_days(7 * number)
239
+ sub_days(7 * number)
232
240
  end
233
241
 
234
242
  def sub_months(number)
235
- o = self.dup
243
+ o = dup
236
244
  number.times do
237
245
  o.decrement_month!
238
246
  end
@@ -243,9 +251,9 @@ module Nickel
243
251
  def diff_in_days(date_to_compare)
244
252
  # d1 will be the earlier date, d2 the later
245
253
  if date_to_compare > self
246
- d1, d2 = self.dup, date_to_compare.dup
254
+ d1, d2 = dup, date_to_compare.dup
247
255
  elsif self > date_to_compare
248
- d1, d2 = date_to_compare.dup, self.dup
256
+ d1, d2 = date_to_compare.dup, dup
249
257
  else
250
258
  return 0 # same date
251
259
  end
@@ -260,11 +268,11 @@ module Nickel
260
268
  end
261
269
 
262
270
  def diff_in_days_to_this(closest_day_index)
263
- if closest_day_index >= self.dayindex
264
- closest_day_index - self.dayindex # could be 0
265
- else # day_num < self.dayindex
266
- 7 - (self.dayindex - closest_day_index)
267
- end
271
+ if closest_day_index >= dayindex
272
+ closest_day_index - dayindex # could be 0
273
+ else # day_num < self.dayindex
274
+ 7 - (dayindex - closest_day_index)
275
+ end
268
276
  end
269
277
 
270
278
  # We need days_in_months and diff_in_months to be available at the class level as well.
@@ -296,7 +304,7 @@ module Nickel
296
304
  diff_in_months = 12 - (month1 - month2)
297
305
  year2 -= 1 # this makes the next line nice
298
306
  end
299
- diff_in_months += (year2 - year1) * 12
307
+ diff_in_months + (year2 - year1) * 12
300
308
  end
301
309
 
302
310
  def format_year(y)
@@ -313,7 +321,7 @@ module Nickel
313
321
  end
314
322
 
315
323
  # formats the year, month, day into the format expected by the ZDate constructor
316
- def format_date(year, month=1, day=1)
324
+ def format_date(year, month = 1, day = 1)
317
325
  format_year(year) + format_month(month) + format_day(day)
318
326
  end
319
327
 
@@ -349,9 +357,9 @@ module Nickel
349
357
  # d.) 1st 10th
350
358
  def interpret(str, current_date)
351
359
  day_str, month_str, year_str = nil, nil, nil
352
- ambiguous = {:month => false, :year => false} # assume false, we use this flag if we aren't certain about the year
360
+ ambiguous = { month: false, year: false } # assume false, we use this flag if we aren't certain about the year
353
361
 
354
- #appropriate matches
362
+ # appropriate matches
355
363
  a_d = /^(\d{1,2})(rd|st|nd|th)?$/ # handles cases a and d
356
364
  b = /^(\d{1,2})\/(\d{1,2})$/ # handles case b
357
365
  c = /^(\d{1,2})\/(\d{1,2})\/(\d{2}|\d{4})$/ # handles case c
@@ -377,9 +385,9 @@ module Nickel
377
385
  if date
378
386
  if ambiguous[:year]
379
387
  # say the date is 11/1 and someone enters 2/1, they probably mean next year, I pick 4 months as a threshold but that is totally arbitrary
380
- current_date.diff_in_months(date) < -4 and date = date.add_years(1)
388
+ current_date.diff_in_months(date) < -4 && date = date.add_years(1)
381
389
  elsif ambiguous[:month]
382
- current_date.day > date.day and date = date.add_months(1)
390
+ current_date.day > date.day && date = date.add_months(1)
383
391
  end
384
392
  end
385
393
  date
@@ -390,70 +398,70 @@ module Nickel
390
398
  # if self is nov 14 and date2 is oct 1, will return -1
391
399
  def diff_in_months(date2)
392
400
  if date2 > self
393
- ZDate.diff_in_months(self.month, self.year, date2.month, date2.year)
401
+ ZDate.diff_in_months(month, year, date2.month, date2.year)
394
402
  else
395
- ZDate.diff_in_months(date2.month, date2.year, self.month, self.year) * -1
403
+ ZDate.diff_in_months(date2.month, date2.year, month, year) * -1
396
404
  end
397
405
  end
398
406
 
399
407
  def days_in_month
400
408
  if leap_year?
401
- ZDate.days_in_leap_year_months[self.month - 1]
409
+ ZDate.days_in_leap_year_months[month - 1]
402
410
  else
403
- ZDate.days_in_common_year_months[self.month - 1]
411
+ ZDate.days_in_common_year_months[month - 1]
404
412
  end
405
413
  end
406
414
 
407
415
  def days_left_in_month
408
- self.days_in_month - self.day
416
+ days_in_month - day
409
417
  end
410
418
 
411
419
  def months_left_in_year
412
- 12 - self.month
420
+ 12 - month
413
421
  end
414
422
 
415
423
  def dayname
416
424
  # well this is going to be a hack, I need an algo for finding the day
417
425
  # Ruby's Time.local is the fastest way to create a Ruby Time object
418
- t = ::Time.local(self.year, ZDate.months_of_year[self.month - 1], self.day)
419
- t.strftime("%a").downcase
426
+ t = ::Time.local(year, ZDate.months_of_year[month - 1], day)
427
+ t.strftime('%a').downcase
420
428
  end
421
429
 
422
430
  def dayindex
423
- ZDate.days_of_week.index(self.dayname)
431
+ ZDate.days_of_week.index(dayname)
424
432
  end
425
433
 
426
434
  def full_dayname
427
- ZDate.full_days_of_week[self.dayindex]
435
+ ZDate.full_days_of_week[dayindex]
428
436
  end
429
437
 
430
438
  def full_monthname
431
- Z.full_months_of_year[self.month - 1]
439
+ Z.full_months_of_year[month - 1]
432
440
  end
433
441
 
434
442
  def leap_year?
435
- self.year % 400 == 0 || self.year % 4 == 0 && self.year % 100 != 0
443
+ year % 400 == 0 || year % 4 == 0 && year % 100 != 0
436
444
  end
437
445
 
438
446
  def day_of_year
439
- doy = self.day
447
+ doy = day
440
448
  # iterate through days in months arrays, summing up the days
441
449
  if leap_year?
442
- doy = (1...self.month).to_a.inject(doy) {|sum, n| sum += ZDate.days_in_leap_year_months[n-1]}
450
+ doy = (1...month).to_a.reduce(doy) { |sum, n| sum + ZDate.days_in_leap_year_months[n - 1] }
443
451
  else
444
- doy = (1...self.month).to_a.inject(doy) {|sum, n| sum += ZDate.days_in_common_year_months[n-1]}
452
+ doy = (1...month).to_a.reduce(doy) { |sum, n| sum + ZDate.days_in_common_year_months[n - 1] }
445
453
  end
446
454
  doy
447
455
  end
448
456
 
449
457
  def days_left_in_year
450
- leap_year? ? 366 - self.day_of_year : 365 - self.day_of_year
458
+ leap_year? ? 366 - day_of_year : 365 - day_of_year
451
459
  end
452
460
 
453
461
  def get_date_from_day_and_week_of_month(day_num, week_num)
454
462
  # This method is extremely sloppy, clean it up
455
463
  # Get the index of the first day of this month
456
- first_day_of_month = self.beginning_of_month
464
+ first_day_of_month = beginning_of_month
457
465
  first_day_index = first_day_of_month.dayindex
458
466
 
459
467
  diff_in_days_to_first_occ = first_day_of_month.diff_in_days_to_this(day_num)
@@ -466,16 +474,16 @@ module Nickel
466
474
  end
467
475
 
468
476
  # there is a chance that the last occurrence is not the 4th week of the month; if that is the case, add an extra 7 days
469
- if (week_num == -1) && (self.month == self.beginning_of_month.add_days(total_diff_in_days + 7).month)
477
+ if (week_num == -1) && (month == beginning_of_month.add_days(total_diff_in_days + 7).month)
470
478
  total_diff_in_days += 7
471
479
  end
472
480
 
473
481
  # Now we have the number of days FROM THE START OF THE CURRENT MONTH; if we are not past that date, then we have found the first occurrence
474
- if (total_diff_in_days + 1) >= self.day
475
- return self.beginning_of_month.add_days(total_diff_in_days)
482
+ if (total_diff_in_days + 1) >= day
483
+ return beginning_of_month.add_days(total_diff_in_days)
476
484
  else # We have already past the date; calculate the occurrence next month!
477
485
  # Get the index of the first day next month
478
- first_day_index = self.add_months(1).beginning_of_month.dayindex
486
+ first_day_index = add_months(1).beginning_of_month.dayindex
479
487
 
480
488
  # Find the number of days away to the day of interest (NOT the week)
481
489
  if day_num > first_day_index
@@ -494,17 +502,17 @@ module Nickel
494
502
  end
495
503
 
496
504
  # there is a chance that the last occurrence is not the 4th week of the month; if that is the case, add an extra 7 days
497
- if (week_num == -1) && (self.add_months(1).month == self.add_months(1).beginning_of_month.add_days(total_diff_in_days + 7).month)
505
+ if (week_num == -1) && (add_months(1).month == add_months(1).beginning_of_month.add_days(total_diff_in_days + 7).month)
498
506
  total_diff_in_days += 7
499
507
  end
500
508
 
501
- return self.add_months(1).beginning_of_month.add_days(total_diff_in_days)
509
+ return add_months(1).beginning_of_month.add_days(total_diff_in_days)
502
510
  end # END if (total_diff_in_days + 1) ...
503
511
  end
504
512
 
505
513
  # returns a new ZDate object, NOTE! this returns nil if that date does not exist (sept 31st)
506
514
  def get_next_date_from_date_of_month(date_of_month)
507
- o = self.dup
515
+ o = dup
508
516
  if day == date_of_month
509
517
  o
510
518
  else
@@ -524,6 +532,7 @@ module Nickel
524
532
  end
525
533
 
526
534
  protected
535
+
527
536
  # Modifies self.
528
537
  # bumps self to first day of next month
529
538
  def increment_month!
@@ -546,16 +555,16 @@ module Nickel
546
555
 
547
556
  private
548
557
 
549
- def before?(d2)
550
- (year < d2.year) || (year == d2.year && (month < d2.month || (month == d2.month && day < d2.day)))
558
+ def before?(other)
559
+ (year < other.year) || (year == other.year && (month < other.month || (month == other.month && day < other.day)))
551
560
  end
552
561
 
553
- def after?(d2)
554
- (year > d2.year) || (year == d2.year && (month > d2.month || (month == d2.month && day > d2.day)))
562
+ def after?(other)
563
+ (year > other.year) || (year == other.year && (month > other.month || (month == other.month && day > other.day)))
555
564
  end
556
565
 
557
566
  def validate
558
- raise "ZDate says: invalid date" if !valid
567
+ fail 'ZDate says: invalid date' unless valid
559
568
  end
560
569
 
561
570
  def valid
@@ -568,11 +577,11 @@ module Nickel
568
577
  end
569
578
 
570
579
  def valid_month
571
- month >= 1 and month <= 12
580
+ month >= 1 && month <= 12
572
581
  end
573
582
 
574
583
  def valid_day
575
- day >= 1 and day <= days_in_month
584
+ day >= 1 && day <= days_in_month
576
585
  end
577
586
 
578
587
  def get_day_or_max_day_in_month(day, month, year)