Class: Week
- Inherits:
-
Object
- Object
- Week
- Defined in:
- lib/sixarm_ruby_week/week.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
Returns the value of attribute date.
Class Method Summary collapse
-
.now ⇒ Object
Return the week that starts today.
-
.parse(date_text) ⇒ Object
Parse date text to a week.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Addition: week + other => week.
-
#-(other) ⇒ Object
Subtraction: week - other => week or integer.
-
#<(other) ⇒ Object
Return week1.date < week2.date.
-
#<=(other) ⇒ Object
Return week1.date <= week2.date.
-
#<=>(other) ⇒ Object
Return week1.date <=> week2.date.
-
#==(other) ⇒ Object
(also: #eql?)
Return week1.date == week2.date.
-
#>(other) ⇒ Object
Return week1.date > week2.date.
-
#>=(other) ⇒ Object
Return week1.date >= week2.date.
-
#date_range ⇒ Object
Return the date range of the week.
-
#first_date ⇒ Object
(also: #begin_date, #start_date)
Return the first date of this week.
-
#hash ⇒ Object
Return a hash for object comparison.
-
#include?(date) ⇒ Boolean
Does the week includes the date?.
-
#initialize(date) ⇒ Week
constructor
Initialize the week with a date.
-
#last_date ⇒ Object
(also: #end_date, #stop_date)
Return the last date of this week.
-
#next ⇒ Object
Return the next week, i.e.
-
#previous ⇒ Object
Return the previous week, i.e.
-
#to_s ⇒ Object
Return the week’s date, coverted to a string.
Constructor Details
Instance Attribute Details
#date ⇒ Object (readonly)
Returns the value of attribute date.
10 11 12 |
# File 'lib/sixarm_ruby_week/week.rb', line 10 def date @date end |
Class Method Details
Instance Method Details
#+(other) ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/sixarm_ruby_week/week.rb', line 234 def +(other) if other.is_a? Numeric return Week.new(date + (other.to_i * 7)) else raise TypeError end end |
#-(other) ⇒ Object
Subtraction: week - other => week or integer
If the other is a numeric value, return a week object pointing other weeks before self.
If the other is a week object, then return the difference between the two weeks.
Example:
week = Week.new(date)
week - 3 => three weeks earlier
Example:
a = Week.new(date)
b = Week.new(date + 21)
b - a => 3
Return:
* [Week]
263 264 265 266 267 268 269 270 271 |
# File 'lib/sixarm_ruby_week/week.rb', line 263 def -(other) if other.is_a? Numeric return Week.new(date - (other * 7)) elsif other.is_a? Week return ((self.date - other.date) / 7).round else raise TypeError end end |
#<(other) ⇒ Object
158 159 160 |
# File 'lib/sixarm_ruby_week/week.rb', line 158 def <(other) self.date < other.date end |
#<=(other) ⇒ Object
175 176 177 |
# File 'lib/sixarm_ruby_week/week.rb', line 175 def <=(other) self.date <= other.date end |
#<=>(other) ⇒ Object
141 142 143 |
# File 'lib/sixarm_ruby_week/week.rb', line 141 def <=>(other) return self.date <=> other.date end |
#==(other) ⇒ Object Also known as: eql?
113 114 115 |
# File 'lib/sixarm_ruby_week/week.rb', line 113 def ==(other) self.date == other.date end |
#>(other) ⇒ Object
192 193 194 |
# File 'lib/sixarm_ruby_week/week.rb', line 192 def >(other) self.date > other.date end |
#>=(other) ⇒ Object
209 210 211 |
# File 'lib/sixarm_ruby_week/week.rb', line 209 def >=(other) self.date >= other.date end |
#date_range ⇒ Object
Return the date range of the week.
This returns start_date..end_date.
Example:
week = Week.now
week.date_range => Range(2012-01-02..2012-01-08)
Return:
* [Range] start_date..stop_date
376 377 378 |
# File 'lib/sixarm_ruby_week/week.rb', line 376 def date_range start_date..stop_date end |
#first_date ⇒ Object Also known as: begin_date, start_date
331 332 333 |
# File 'lib/sixarm_ruby_week/week.rb', line 331 def first_date @date end |
#hash ⇒ Object
Return a hash for object comparison.
This is simply the hash of the date, which means that two week objects created with the same date always compare equally.
Example:
date = Date.today
date.hash #=> 1655958911300557206
week = Week.new(date)
week.hash #=> 1655958911300557206
Return:
* [Fixnum] The week's date's object hash number.
97 98 99 |
# File 'lib/sixarm_ruby_week/week.rb', line 97 def hash date.hash end |
#include?(date) ⇒ Boolean
393 394 395 |
# File 'lib/sixarm_ruby_week/week.rb', line 393 def include?(date) (start_date..end_date).include?(date) end |
#last_date ⇒ Object Also known as: end_date, stop_date
356 357 358 |
# File 'lib/sixarm_ruby_week/week.rb', line 356 def last_date @date + 6 end |
#next ⇒ Object
305 306 307 |
# File 'lib/sixarm_ruby_week/week.rb', line 305 def next return self + 1 end |
#previous ⇒ Object
290 291 292 |
# File 'lib/sixarm_ruby_week/week.rb', line 290 def previous return self - 1 end |