Class: ActiveWeek::Week
- Inherits:
-
Object
- Object
- ActiveWeek::Week
- Includes:
- Comparable
- Defined in:
- lib/active_week/week.rb
Instance Attribute Summary collapse
-
#first_day ⇒ Object
readonly
Returns the value of attribute first_day.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #each_day ⇒ Object
- #hash ⇒ Object
-
#initialize(year, number) ⇒ Week
constructor
A new instance of Week.
- #last_day ⇒ Object
- #next_week ⇒ Object (also: #succ)
- #number ⇒ Object
- #prev_week ⇒ Object
- #year ⇒ Object
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_day ⇒ Object (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
.current ⇒ Object
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
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?
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_day ⇒ Object
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 |
#hash ⇒ Object
71 72 73 |
# File 'lib/active_week/week.rb', line 71 def hash year.hash ^ number.hash end |
#last_day ⇒ Object
35 36 37 |
# File 'lib/active_week/week.rb', line 35 def last_day Date.commercial(year, number, WEEKDAY_SUNDAY) end |
#next_week ⇒ Object 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 |
#number ⇒ Object
31 32 33 |
# File 'lib/active_week/week.rb', line 31 def number @first_day.cweek end |
#prev_week ⇒ Object
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 |
#year ⇒ Object
27 28 29 |
# File 'lib/active_week/week.rb', line 27 def year @first_day.cwyear end |