Class: DateUtils::Week

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

Overview

Represents a ‘Week’ beginning on Mondays and ending on Sundays

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date = nil) ⇒ Week

create a new Week-instance with the given initial Date if ‘date’ is nil, create an instance with Date.today



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/date_utils.rb', line 65

def initialize(date=nil)
  date = Date.today if date.nil?
  if date.kind_of?(Date)
    @month = Month.new(date)
    @date = date
    @num_week = date.cweek
    @first_day = date - ( date.cwday - 1 )
    @last_day = date + ( 7 - date.cwday ) 
  else
    raise ArgumentError, "needs Date object as input."
  end
end

Instance Attribute Details

#dateObject (readonly)

the initial / regular Date instance



57
58
59
# File 'lib/date_utils.rb', line 57

def date
  @date
end

#first_dayObject (readonly)

the first day (Monday) of the week



48
49
50
# File 'lib/date_utils.rb', line 48

def first_day
  @first_day
end

#last_dayObject (readonly)

the last day (Sunday) of the week



51
52
53
# File 'lib/date_utils.rb', line 51

def last_day
  @last_day
end

#monthObject (readonly)

the Month of the week



60
61
62
# File 'lib/date_utils.rb', line 60

def month
  @month
end

#num_weekObject (readonly)

the num of the week



54
55
56
# File 'lib/date_utils.rb', line 54

def num_week
  @num_week
end

Instance Method Details

#daysObject

returns collection of days as Date -instances



92
93
94
95
96
# File 'lib/date_utils.rb', line 92

def days
  arr = []
  @first_day.upto(@last_day) { |date| arr << date }
  arr
end

#nextObject

return new Week -instance one week after self



80
81
82
# File 'lib/date_utils.rb', line 80

def next
  return Week.new(@last_day + 1)
end

#previousObject

returns new Week -instance one week before self



86
87
88
# File 'lib/date_utils.rb', line 86

def previous
  return Week.new(@first_day - 1)
end