Class: AIXM::Schedule::Day
- Includes:
- Concerns::HashEquality, Comparable
- Defined in:
- lib/aixm/schedule/day.rb
Overview
Days suitable for schedules
Constant Summary collapse
- WEEKDAYS =
%i(sunday monday tuesday wednesday thursday friday saturday).freeze
- DAYS =
(WEEKDAYS + %i(workday day_preceding_workday day_following_workday holiday day_preceding_holiday day_following_holiday any)).freeze
Instance Attribute Summary collapse
-
#day ⇒ Symbol
readonly
Day of the week or special named day.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Whether two days are equal.
-
#any? ⇒ Boolean
Whether the day is set to :any.
-
#covered_by?(other) ⇒ Boolean
Whether this schedule day falls within the given range of schedule days.
- #hash ⇒ Object
-
#initialize(day = :any) ⇒ Day
constructor
Set the given day of the week or special named day.
- #inspect ⇒ Object
-
#pred ⇒ AIXM::Schedule::Day
(also: #prev)
Create new day one day prior to this one.
-
#sortable? ⇒ Boolean
Whether this schedule day sortable.
-
#succ ⇒ AIXM::Schedule::Day
(also: #next)
Create new day one day after this one.
-
#to_s ⇒ String
Human readable representation such as “monday” or “day preceding workday”.
-
#to_sym ⇒ Symbol
Symbol used to initialize this day.
Methods included from Concerns::HashEquality
Constructor Details
#initialize(day = :any) ⇒ Day
Set the given day of the week or special named day.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/aixm/schedule/day.rb', line 28 def initialize(day=:any) case day when Symbol, String self.day = day when Integer fail ArgumentError unless day.between?(0, 6) self.day = WEEKDAYS[day] else fail ArgumentError end end |
Instance Attribute Details
#day ⇒ Symbol
Day of the week or special named day
22 23 24 |
# File 'lib/aixm/schedule/day.rb', line 22 def day @day end |
Instance Method Details
#==(other) ⇒ Boolean
Whether two days are equal.
81 82 83 |
# File 'lib/aixm/schedule/day.rb', line 81 def ==(other) day == other.day end |
#any? ⇒ Boolean
Whether the day is set to :any
93 94 95 |
# File 'lib/aixm/schedule/day.rb', line 93 def any? day == :any end |
#covered_by?(other) ⇒ Boolean
Only weekdays and :any
can be computed!
Whether this schedule day falls within the given range of schedule days.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/aixm/schedule/day.rb', line 113 def covered_by?(other) range = Range.from other case when any? || range.first.any? || range.last.any? true when !sortable? || !range.first.sortable? || !range.last.sortable? fail "includes unsortables" when range.min range.cover? self else range.first <= self || self <= range.last end end |
#hash ⇒ Object
86 87 88 |
# File 'lib/aixm/schedule/day.rb', line 86 def hash [self.class, day].hash end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/aixm/schedule/day.rb', line 54 def inspect %Q(#<#{self.class} #{to_s}>) end |
#pred ⇒ AIXM::Schedule::Day Also known as: prev
Create new day one day prior to this one.
61 62 63 64 65 |
# File 'lib/aixm/schedule/day.rb', line 61 def pred return self if any? fail(TypeError, "can't iterate from #{day}") unless wday self.class.new(WEEKDAYS[wday.pred % 7]) end |
#sortable? ⇒ Boolean
Whether this schedule day sortable.
100 101 102 |
# File 'lib/aixm/schedule/day.rb', line 100 def sortable? WEEKDAYS.include? day end |
#succ ⇒ AIXM::Schedule::Day Also known as: next
Create new day one day after this one.
71 72 73 74 75 |
# File 'lib/aixm/schedule/day.rb', line 71 def succ return self if any? fail(TypeError, "can't iterate from #{day}") unless wday self.class.new(WEEKDAYS[wday.succ % 7]) end |
#to_s ⇒ String
Human readable representation such as “monday” or “day preceding workday”
43 44 45 |
# File 'lib/aixm/schedule/day.rb', line 43 def to_s day.to_s.gsub('_', ' ') end |
#to_sym ⇒ Symbol
Symbol used to initialize this day
50 51 52 |
# File 'lib/aixm/schedule/day.rb', line 50 def to_sym day.to_s.to_sym end |