Class: ActiveWeek::Week

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/active_week/week.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, number) ⇒ Week

Returns a new instance of Week.



23
24
25
# File 'lib/active_week/week.rb', line 23

def initialize(year, number)
  @first_day = Date.commercial(year, number, WEEKDAY_MONDAY)
end

Instance Attribute Details

#first_dayObject (readonly)

Returns the value of attribute first_day.



5
6
7
# File 'lib/active_week/week.rb', line 5

def first_day
  @first_day
end

Class Method Details

.currentObject



13
14
15
16
# File 'lib/active_week/week.rb', line 13

def current
  date = Time.zone ? Time.zone.today : Date.today
  new(date.cwyear, date.cweek)
end

.from_date(date) ⇒ Object



18
19
20
# File 'lib/active_week/week.rb', line 18

def from_date(date)
  new(date.cwyear, date.cweek)
end

Instance Method Details

#<=>(other) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
# File 'lib/active_week/week.rb', line 57

def <=>(other)
  raise ArgumentError.new("#{self.class} can't be compared with #{other.class}") unless other.is_a?(self.class)

  first_day <=> other.first_day
end

#==(other) ⇒ Object Also known as: eql?

Raises:

  • (ArgumentError)


63
64
65
66
67
# File 'lib/active_week/week.rb', line 63

def ==(other)
  raise ArgumentError.new("#{self.class} can't be compared with #{other.class}") unless other.is_a?(self.class)

  first_day == other.first_day
end

#each_dayObject



51
52
53
54
55
# File 'lib/active_week/week.rb', line 51

def each_day
  return to_enum(__callee__) unless block_given?
  (first_day..last_day).each { |d| yield d }
  self
end

#hashObject



71
72
73
# File 'lib/active_week/week.rb', line 71

def hash
  year.hash ^ number.hash
end

#last_dayObject



35
36
37
# File 'lib/active_week/week.rb', line 35

def last_day
  Date.commercial(year, number, WEEKDAY_SUNDAY)
end

#next_weekObject Also known as: succ



39
40
41
42
# File 'lib/active_week/week.rb', line 39

def next_week
  date = first_day.next_week
  self.class.new(date.cwyear, date.cweek)
end

#numberObject



31
32
33
# File 'lib/active_week/week.rb', line 31

def number
  @first_day.cweek
end

#prev_weekObject



46
47
48
49
# File 'lib/active_week/week.rb', line 46

def prev_week
  date = first_day.prev_week
  self.class.new(date.cwyear, date.cweek)
end

#yearObject



27
28
29
# File 'lib/active_week/week.rb', line 27

def year
  @first_day.cwyear
end