Class: Week

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dateObject (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

.nowObject

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_rangeObject

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_dateObject 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

#hashObject

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]

Returns:

  • (Boolean)


393
394
395
# File 'lib/sixarm_ruby_week/week.rb', line 393

def include?(date)
  (start_date..end_date).include?(date)
end

#last_dateObject 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

#nextObject

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

#previousObject

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_sObject

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