Class: DateUtils::Year

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/date_utils.rb

Overview

represents a Year

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#include?

Constructor Details

#initialize(date = nil) ⇒ Year

create a new Year -instance with given Date



202
203
204
205
206
207
208
# File 'lib/date_utils.rb', line 202

def initialize(date=nil)
  date = Date.today if date.nil?
  unless date.kind_of?(Date)
    raise ArgumentError, "needs Date as input!"
  end
  @year = date.year
end

Instance Attribute Details

#dateObject (readonly)

the initial Date of the Year -instance



195
196
197
# File 'lib/date_utils.rb', line 195

def date
  @date
end

#yearObject (readonly)

the Year as an Integer of self



198
199
200
# File 'lib/date_utils.rb', line 198

def year
  @year
end

Instance Method Details

#get_week(num) ⇒ Object

returns Week ‘num’ of self



237
238
239
# File 'lib/date_utils.rb', line 237

def get_week(num)
  ! num.kind_of?(Fixnum) || num > 52 ? ArgumentError.new("invalid week-number 'num'"): self.weeks[num -1]
end

#monthsObject

returns collection of Month -instances of self



212
213
214
215
216
217
218
# File 'lib/date_utils.rb', line 212

def months
  arr = []
  for i in 1..12
    arr.push( Month.new(Date.civil(@year,i) ) )
  end
  arr
end

#weeksObject

returns collection of Week -instances of self neccessarily overlaps year boundarys



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/date_utils.rb', line 223

def weeks
  d = Date.civil(@year)
  arr = []
  week = Week.new(d)
  arr.push(week)
  for i in 1..52
    week = week.next
    arr.push(week)
  end
  arr
end