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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

extract_options_from_args!, #include?

Constructor Details

#initialize(val = 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



96
97
98
99
100
101
102
103
104
# File 'lib/date_utils.rb', line 96

def initialize(val=nil)
  if val.nil?
    _date = Date.today
  else
    _date = val.is_a?(Date) ? val : (raise ArgumentError.new("neither Fixnum nor Date given."))
  end 
  set_date(_date)
  create_instance_variables
end

Instance Attribute Details

#dateObject (readonly)

the initial / regular Date instance



88
89
90
# File 'lib/date_utils.rb', line 88

def date
  @date
end

#first_dayObject (readonly)

the first day (Monday) of the week



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

def first_day
  @first_day
end

#last_dayObject (readonly)

the last day (Sunday) of the week



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

def last_day
  @last_day
end

#monthObject (readonly)

the Month of the week



91
92
93
# File 'lib/date_utils.rb', line 91

def month
  @month
end

#num_weekObject (readonly)

the num of the week



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

def num_week
  @num_week
end

Class Method Details

.create(*args) ⇒ Object

create a Week-instance call with hash: year and week :call-seq: Week.create(:year => x, :week => y) Week.create(:week => x) Week.create(:year => x)



113
114
115
116
117
118
119
120
# File 'lib/date_utils.rb', line 113

def self.create(*args)
  options = Common::extract_options_from_args!(args)
  year_date = options.has_key?(:year) ? Date::civil(options[:year].to_i, 1,1) : Date.today
  unless options.has_key?(:week) && !options[:week].nil?
    return Week.new(year_date)
  end
  return Year.new(year_date).get_week(options[:week].to_i)
end

Instance Method Details

#daysObject

returns collection of days as Date -instances



136
137
138
139
140
# File 'lib/date_utils.rb', line 136

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

#nextObject

return new Week -instance one week after self



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

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

#previousObject

returns new Week -instance one week before self



130
131
132
# File 'lib/date_utils.rb', line 130

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