Class: DateUtils::Year

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

Overview

represents a Year

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

extract_options_from_args!, #include?

Constructor Details

#initialize(date = nil) ⇒ Year

create a new Year -instance with given Date



278
279
280
281
282
283
284
285
286
287
# File 'lib/date_utils.rb', line 278

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
  @date = date
  @first_day = Date::parse("#{@year}-01-01")
  @last_day = Date::parse("#{@year}-12-31")
end

Instance Attribute Details

#dateObject (readonly)

the initial Date of the Year -instance



265
266
267
# File 'lib/date_utils.rb', line 265

def date
  @date
end

#first_dayObject (readonly)

first day of this year



271
272
273
# File 'lib/date_utils.rb', line 271

def first_day
  @first_day
end

#last_dayObject (readonly)

last day of this year



274
275
276
# File 'lib/date_utils.rb', line 274

def last_day
  @last_day
end

#yearObject (readonly)

the Year as an Integer of self



268
269
270
# File 'lib/date_utils.rb', line 268

def year
  @year
end

Class Method Details

.create(*args) ⇒ Object

call with hash: year :call-seq: Year.create(:year => x)



294
295
296
297
298
# File 'lib/date_utils.rb', line 294

def self.create(*args)
  options = Common::extract_options_from_args!(args)
  date_year = options.has_key?(:year) ? Date.civil(options[:year]) : (raise ArgumentError.new("no key :year in hash."))
  return Year.new(date_year) 
end

Instance Method Details

#get_month(num) ⇒ Object

returns Month ‘num’ of self



333
334
335
# File 'lib/date_utils.rb', line 333

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

#get_week(num) ⇒ Object

returns Week ‘num’ of self



327
328
329
# File 'lib/date_utils.rb', line 327

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



302
303
304
305
306
307
308
# File 'lib/date_utils.rb', line 302

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

#nextObject

returns new Year instance one year later TODO implement



339
340
# File 'lib/date_utils.rb', line 339

def next
end

#previousObject

returns new Year instance one year previous TODO implement



344
345
# File 'lib/date_utils.rb', line 344

def previous
end

#weeksObject

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



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/date_utils.rb', line 313

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