Class: DateUtils::Week

Inherits:
Object
  • Object
show all
Includes:
Common
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

Methods included from Common

#include?

Constructor Details

#initialize(date = nil) ⇒ Week

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/date_utils.rb', line 91

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

Instance Attribute Details

#dateObject (readonly)

the initial / regular Date instance



83
84
85
# File 'lib/date_utils.rb', line 83

def date
  @date
end

#first_dayObject (readonly)

the first day (Monday) of the week



74
75
76
# File 'lib/date_utils.rb', line 74

def first_day
  @first_day
end

#last_dayObject (readonly)

the last day (Sunday) of the week



77
78
79
# File 'lib/date_utils.rb', line 77

def last_day
  @last_day
end

#monthObject (readonly)

the Month of the week



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

def month
  @month
end

#num_weekObject (readonly)

the num of the week



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

def num_week
  @num_week
end

Instance Method Details

#daysObject

returns collection of days as Date -instances



122
123
124
125
126
# File 'lib/date_utils.rb', line 122

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

#nextObject

return new Week -instance one week after self



110
111
112
# File 'lib/date_utils.rb', line 110

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

#previousObject

returns new Week -instance one week before self



116
117
118
# File 'lib/date_utils.rb', line 116

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