Class: Week

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Week) initialize(date)

A new instance of Week



13
14
15
# File 'lib/sixarm_ruby_week.rb', line 13

def initialize(date)
  @date = date
end

Instance Attribute Details

- (Object) date (readonly)

Returns the value of attribute date



11
12
13
# File 'lib/sixarm_ruby_week.rb', line 11

def date
  @date
end

Class Method Details

+ (Object) now

Return the week that starts today



30
31
32
# File 'lib/sixarm_ruby_week.rb', line 30

def self.now
  Week.new(Date.today)
end

+ (Object) parse(date_text)

Parse date text to a week.

Examples:

week = Week.parse('2012-01-02') 


40
41
42
# File 'lib/sixarm_ruby_week.rb', line 40

def self.parse(date_text)
  Week.new(Date.parse(date_text))
end

Instance Method Details

- (Object) +(other)

Addition: week + other => week

Return a date object pointing other weeks after self. The other should be a numeric value.

Examples:

week = Week.parse('2012-01-02')
week + 3 => three weeks later


120
121
122
123
124
125
126
# File 'lib/sixarm_ruby_week.rb', line 120

def +(other)
  if other.is_a? Numeric
    return Week.new(date + (other.to_i * 7))
  else
    raise TypeError
  end
end

- (Object) -(other)

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.

Examples:

date = date.parse('2012-01-02')
week = Week.new(date)
week - 3 => three weeks earlier

date = date.parse('2012-01-02')
start_week =  Week.new(date)
end_week =  Week.new(date + 21)
end_week - start_week => 3


146
147
148
149
150
151
152
153
154
# File 'lib/sixarm_ruby_week.rb', line 146

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

- (Object) <(other)

Return week1.date < week2.date



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

def <(other)
  self.date < other.date
end

- (Object) <=(other)

Return week1.date <= week2.date



89
90
91
# File 'lib/sixarm_ruby_week.rb', line 89

def <=(other)
  self.date <= other.date
end

- (Object) <=>(other)

Return week1.date <=> week2.date



75
76
77
# File 'lib/sixarm_ruby_week.rb', line 75

def <=>(other)
 return self.date <=> other.date
end

- (Object) ==(other)

Return week1.date == week2.date



61
62
63
# File 'lib/sixarm_ruby_week.rb', line 61

def ==(other)
  self.date == other.date
end

- (Object) >(other)

Return week1.date > week2.date



96
97
98
# File 'lib/sixarm_ruby_week.rb', line 96

def >(other)
  self.date > other.date
end

- (Object) >=(other)

Return week1.date >= week2.date



103
104
105
# File 'lib/sixarm_ruby_week.rb', line 103

def >=(other)
  self.date >= other.date
end

- (Object) date_range

Return the range start_date..end_date

Examples:

week = Week.parse('2012-01-02')
week.date_range => Range(2012-01-02..2012-01-08)


206
207
208
# File 'lib/sixarm_ruby_week.rb', line 206

def date_range
  start_date..end_date
end

- (Object) end_date

Return the end date of this week. This is the same as week.date + 6 days

Examples:

week = Week.parse('2012-01-02')
week.end_date.to_s => '2012-01-08'


195
196
197
# File 'lib/sixarm_ruby_week.rb', line 195

def end_date
  @date + 6
end

- (Boolean) eql?(other)

Return week1.date.eql? week2.date

Returns:

  • (Boolean)


68
69
70
# File 'lib/sixarm_ruby_week.rb', line 68

def eql?(other)
  self.date == other.date
end

- (Object) hash

Return the hash for object comparison.

This is simply the has of the date, which means that two week objects created with the same date will compare equally.



54
55
56
# File 'lib/sixarm_ruby_week.rb', line 54

def hash
  date.hash
end

- (Boolean) include?(date)

Return true iif the week includes the date, i.e. if the date is in start_date..end_date

Examples:

week = Week.parse('2012-01-02')
week.include?(Date.parse('2012-01-05')) => true
week.include?(Date.parse('2012-01-10')) => false

Returns:

  • (Boolean)


218
219
220
# File 'lib/sixarm_ruby_week.rb', line 218

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

- (Object) next

Return the next week, i.e. seven day later.



168
169
170
# File 'lib/sixarm_ruby_week.rb', line 168

def next
  return self + 1
end

- (Object) previous

Return the previous week, i.e. seven day earlier.



161
162
163
# File 'lib/sixarm_ruby_week.rb', line 161

def previous
  return self - 1
end

- (Object) start_date

Return the start date of this week. This is the same as week.date.

Examples:

week = Week.parse('2012-01-02')
week.start_date.to_s => '2012-01-02'


183
184
185
# File 'lib/sixarm_ruby_week.rb', line 183

def start_date
  @date
end

- (Object) to_s

Return date.to_s



20
21
22
# File 'lib/sixarm_ruby_week.rb', line 20

def to_s
  @date.to_s
end