DateUtils 0.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/date_utils.rb +28 -51
  2. metadata +3 -3
@@ -18,7 +18,6 @@
18
18
  #
19
19
 
20
20
  require 'date'
21
- require 'date_utils'
22
21
 
23
22
 
24
23
  # DateUils
@@ -28,40 +27,7 @@ require 'date_utils'
28
27
  # * DateUtils::Year
29
28
  # * DateUtils::Month
30
29
  # * DateUtils::Week
31
- # * DateUtils::Day
32
- # for complete rdoc visit:
33
- # http://dateutils.rubyforge.org
34
30
  #
35
- # _usage_:
36
- #
37
- # year = DateUtils::Year.new(Date.parse('1977-10-18'))
38
- # pp year
39
- #<DateUtils::Year:0xb7c4e19c
40
- # @date=#<Date: 4886869/2,0,2299161>,
41
- # @first_day=#<Date: 4886289/2,0,2299161>,
42
- # @last_day=#<Date: 4887017/2,0,2299161>,
43
- # @year=1977>
44
- #
45
- # year.first_day.to_s
46
- # => "1977-01-01"
47
- # pp year.weeks.first
48
- #<DateUtils::Week:0xb7b37a60
49
- # @date=#<Date: 4886289/2,0,2299161>,
50
- # @first_day=#<Date: 4886279/2,0,2299161>,
51
- # @last_day=#<Date: 4886291/2,0,2299161>,
52
- # @month=
53
- # #<DateUtils::Month:0xb7b37704
54
- # @date=#<Date: 4886289/2,0,2299161>,
55
- # @first_day=#<Date: 4886289/2,0,2299161>,
56
- # @last_day=#<Date: 4886349/2,0,2299161>,
57
- # @month=1,
58
- # @num_days=31>,
59
- # @num_week=53>
60
- #
61
- #
62
- # year.distance_to_now_in_words
63
- # => "29 years,10 months,5 days ago"
64
- #
65
31
  module DateUtils
66
32
 
67
33
  # common to Year/Month/Week
@@ -255,6 +221,8 @@ module DateUtils
255
221
  return Week.new(@last_day + 1)
256
222
  end
257
223
 
224
+ alias succ next
225
+
258
226
  # returns new Week -instance one week before self
259
227
  #
260
228
  def previous
@@ -262,11 +230,12 @@ module DateUtils
262
230
  end
263
231
 
264
232
  # returns collection of days as Date -instances
265
- #
266
- def days
233
+ # or yields Day if block given
234
+ #
235
+ def days(&block)
267
236
  arr = []
268
237
  @first_day.upto(@last_day) { |date| arr << date }
269
- arr
238
+ block_given? ? arr.each { |a| yield a } : arr
270
239
  end
271
240
 
272
241
  private
@@ -286,12 +255,14 @@ module DateUtils
286
255
  @date = date
287
256
  end
288
257
 
258
+
289
259
  end
290
260
 
291
- # Represents a 'Day'
292
- # future usage to supply 'attributes' - e.g. calendar-events...
261
+ # future use of Day planned instead of regular 'Date'-instance
262
+ # when calling e.g. Month#days
293
263
  #
294
264
  class Day
265
+ # the regular 'Date'-instance of this 'Day'
295
266
  attr_reader :date
296
267
 
297
268
  def initialize(date=nil)
@@ -304,7 +275,7 @@ module DateUtils
304
275
  #
305
276
  class Month
306
277
  include Common
307
-
278
+
308
279
  # the initial / regular Date instance
309
280
  attr_reader :date
310
281
 
@@ -357,7 +328,9 @@ module DateUtils
357
328
  def next
358
329
  return Month.new(@last_day + 1)
359
330
  end
360
-
331
+
332
+ alias succ next
333
+
361
334
  # returns a new Month -instance one Month prior to self
362
335
  #
363
336
  def previous
@@ -365,11 +338,11 @@ module DateUtils
365
338
  end
366
339
 
367
340
  # returns collection of days as Date -instances of self
368
- #
369
- def days
341
+ # or yields Day if block given
342
+ def days(&block)
370
343
  arr = []
371
344
  @first_day.upto(@last_day) { |date| arr << date }
372
- arr
345
+ block_given? ? arr.each { |a| yield a } : arr
373
346
  end
374
347
 
375
348
  private
@@ -393,7 +366,7 @@ module DateUtils
393
366
  #
394
367
  class Year
395
368
  include Common
396
-
369
+
397
370
  # the initial Date of the Year -instance
398
371
  attr_reader :date
399
372
 
@@ -431,19 +404,21 @@ module DateUtils
431
404
  end
432
405
 
433
406
  # returns collection of Month -instances of self
434
- #
435
- def months
407
+ # or yields Month if block given
408
+ #
409
+ def months(&block)
436
410
  arr = []
437
411
  for i in 1..12
438
412
  arr.push( Month.new(Date.civil(@year,i) ) )
439
413
  end
440
- arr
414
+ block_given? ? arr.each { |a| yield a } : arr
441
415
  end
442
416
 
443
417
  # returns collection of Week -instances of self
444
418
  # neccessarily overlaps year boundarys
445
- #
446
- def weeks
419
+ # yields Week if block given
420
+ #
421
+ def weeks(&block)
447
422
  d = Date.civil(@year)
448
423
  arr = []
449
424
  week = Week.new(d)
@@ -452,7 +427,7 @@ module DateUtils
452
427
  week = week.next
453
428
  arr.push(week)
454
429
  end
455
- arr
430
+ block_given? ? arr.each { |a| yield a } : arr
456
431
  end
457
432
 
458
433
  # returns Week 'num' of self
@@ -477,6 +452,8 @@ module DateUtils
477
452
  end
478
453
  end
479
454
 
455
+ alias succ next
456
+
480
457
  # returns new Year instance one year previous
481
458
  #
482
459
  def previous
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.3
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: DateUtils
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.3"
7
- date: 2007-08-23 00:00:00 +02:00
6
+ version: 0.3.1
7
+ date: 2007-08-25 00:00:00 +02:00
8
8
  summary: Some handy utils to deal with Date
9
9
  require_paths:
10
10
  - lib