date_utils 0.1.2 → 0.1.3

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 +52 -11
  2. metadata +2 -2
data/lib/date_utils.rb CHANGED
@@ -11,14 +11,39 @@
11
11
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
13
  # GNU General Public License for more details.
14
- #
14
+ #
15
15
  # You should have received a copy of the GNU General Public License
16
16
  # along with this program; if not, write to the Free Software
17
17
  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
18
  #
19
+
19
20
  require 'date'
20
21
 
22
+ # DateUils
23
+ # is a simple Collection of classes which offer some
24
+ # handy tools to get a grip on 'Date'
25
+ # ----
26
+ # * DateUtils::Year
27
+ # * DateUtils::Month
28
+ # * DateUtils::Week
29
+ #
21
30
  module DateUtils
31
+
32
+ # common to Year/Month/Week
33
+ #
34
+ module Common
35
+
36
+ def include?(date)
37
+ if date.instance_of?(Date)
38
+ self.respond_to?('days') && self.days.include?(date)
39
+ else
40
+ raise ArgumentError, "need Date as input or no instance variable 'days'..."
41
+ end
42
+ end
43
+
44
+
45
+ end
46
+
22
47
  # represents a timezone
23
48
  #
24
49
  class GMTZone
@@ -43,7 +68,8 @@ module DateUtils
43
68
  # ending on Sundays
44
69
  #
45
70
  class Week
46
-
71
+ include Common
72
+
47
73
  # the first day (Monday) of the week
48
74
  attr_reader :first_day
49
75
 
@@ -59,20 +85,24 @@ module DateUtils
59
85
  # the Month of the week
60
86
  attr_reader :month
61
87
 
62
- # create a new Week-instance with the given initial Date
88
+ # create a new Week-instance with the given initial Date or Week-number
63
89
  # if 'date' is nil, create an instance with Date.today
64
90
  #
65
91
  def initialize(date=nil)
66
- date = Date.today if date.nil?
67
- if date.kind_of?(Date)
68
- @month = Month.new(date)
69
- @date = date
70
- @num_week = date.cweek
71
- @first_day = date - ( date.cwday - 1 )
72
- @last_day = date + ( 7 - date.cwday )
92
+ if date.nil?
93
+ _date = Date.today
94
+ elsif date.kind_of?(Date)
95
+ _date = date
96
+ elsif date.kind_of?(Fixnum)
97
+ _date = Year.new.get_week(date).date
73
98
  else
74
- raise ArgumentError, "needs Date object as input."
99
+ raise ArgumentError, "needs Date object or Fixnum for Week as input."
75
100
  end
101
+ @month = Month.new(_date)
102
+ @date = _date
103
+ @num_week = _date.cweek
104
+ @first_day = _date - ( _date.cwday - 1 )
105
+ @last_day = _date + ( 7 - _date.cwday )
76
106
  end
77
107
 
78
108
  # return new Week -instance one week after self
@@ -100,6 +130,8 @@ module DateUtils
100
130
  # Represents a 'Month'
101
131
  #
102
132
  class Month
133
+ include Common
134
+
103
135
  # the initial / regular Date instance
104
136
  attr_reader :date
105
137
 
@@ -157,6 +189,8 @@ module DateUtils
157
189
  # represents a Year
158
190
  #
159
191
  class Year
192
+ include Common
193
+
160
194
  # the initial Date of the Year -instance
161
195
  attr_reader :date
162
196
 
@@ -197,5 +231,12 @@ module DateUtils
197
231
  end
198
232
  arr
199
233
  end
234
+
235
+ # returns Week 'num' of self
236
+ #
237
+ def get_week(num)
238
+ ! num.kind_of?(Fixnum) || num > 52 ? ArgumentError.new("invalid week-number 'num'"): self.weeks[num -1]
239
+ end
240
+
200
241
  end
201
242
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: date_utils
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
7
- date: 2007-03-01 00:00:00 +01:00
6
+ version: 0.1.3
7
+ date: 2007-03-02 00:00:00 +01:00
8
8
  summary: Some handy utils to deal with Date
9
9
  require_paths:
10
10
  - lib