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
#initialize(date) ⇒ Week
Initialize the week with a date.
Example:
require "date"
date = Date.today
week = Week.new(date)
Return:
* [Week] The new week
24 25 26 |
# File 'lib/sixarm_ruby_week/week.rb', line 24 def initialize(date) @date = date end |
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
.now ⇒ Object
Return the week that starts today
Example:
week = Week.now
56 57 58 |
# File 'lib/sixarm_ruby_week/week.rb', line 56 def self.now Week.new(Date.today) end |
.parse(date_text) ⇒ Object
Parse date text to a week.
Example:
week = Week.parse("2015-12-31")
Return:
* [Week] A new week.
70 71 72 |
# File 'lib/sixarm_ruby_week/week.rb', line 70 def self.parse(date_text) Week.new(Date.parse(date_text)) end |
Instance Method Details
#+(other) ⇒ Object
Addition: week + other => week
Return a date object pointing at other weeks after self.
The other should be a numeric value.
Example:
week = Week.new(date)
week + 3 #=> three weeks later
Return:
* [Week]
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
Return week1.date < week2.date
Example:
a = Week.new(date)
b = Week.new(date - 1)
a < b #=> true
Return:
* [true,false]
158 159 160 |
# File 'lib/sixarm_ruby_week/week.rb', line 158 def <(other) self.date < other.date end |
#<=(other) ⇒ Object
Return week1.date <= week2.date
Example:
a = Week.new(date)
b = Week.new(date - 1)
a <= b #=> true
Return:
* [true,false]
175 176 177 |
# File 'lib/sixarm_ruby_week/week.rb', line 175 def <=(other) self.date <= other.date end |
#<=>(other) ⇒ Object
Return week1.date <=> week2.date
a = Week.now
b = Week.now
a <=> b #=> 0
Return:
* [-1,0,1]
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?
Return week1.date == week2.date
Example:
a = Week.now
b = Week.now
a == b #=> true
Return:
* [true,false]
113 114 115 |
# File 'lib/sixarm_ruby_week/week.rb', line 113 def ==(other) self.date == other.date end |
#>(other) ⇒ Object
Return week1.date > week2.date
Example:
a = Week.new(date)
b = Week.new(date + 1)
a > b #=> true
Return:
* [true,false]
192 193 194 |
# File 'lib/sixarm_ruby_week/week.rb', line 192 def >(other) self.date > other.date end |
#>=(other) ⇒ Object
Return week1.date >= week2.date
Example:
a = Week.new(date)
b = Week.new(date + 1)
a >= b #=> true
Return:
* [true,false]
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
Return the first date of this week.
Example:
week = Week.new(date)
week.first_date #=> date
Aliases:
week.begin_date
week.start_date
Return:
* [Date] week.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
Does the week includes the date?
Example:
date = Date.today
week = Week.new(date)
week.include?(date + 1) => true
week.include?(date + 7) => false
Return:
* [true/false]
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
Return the last date of this week.
Return
Example:
week = Week.new(date)
week.last_date #=> date + 6
Aliases:
week.end_date
week.stop_date
Return:
* [Date] week.date + 6
356 357 358 |
# File 'lib/sixarm_ruby_week/week.rb', line 356 def last_date @date + 6 end |
#next ⇒ Object
Return the next week, i.e. seven days later.
Example:
week = Week.new(date)
week.next #=> seven days later.
Return:
* [Week] The next week
305 306 307 |
# File 'lib/sixarm_ruby_week/week.rb', line 305 def next return self + 1 end |
#previous ⇒ Object
Return the previous week, i.e. seven days earlier.
Example:
week = Week.new(date)
week.previous #=> seven days earlier
Return:
* [Week] The previous week
290 291 292 |
# File 'lib/sixarm_ruby_week/week.rb', line 290 def previous return self - 1 end |
#to_s ⇒ Object
Return the week’s date, coverted to a string.
Example:
date = Date.parse("2015-12-31")
week = Week.new(date)
week.to_s #=> "2015-12-31"
Return:
* [String] The week's date, coverted to a string.
40 41 42 |
# File 'lib/sixarm_ruby_week/week.rb', line 40 def to_s @date.to_s end |