Class: AIXM::Schedule::Day

Inherits:
Object show all
Includes:
Concerns::HashEquality, Comparable
Defined in:
lib/aixm/schedule/day.rb

Overview

Days suitable for schedules

Examples:

from = AIXM.day(:monday)                   # => :monday
to = AIXM.day(4)                           # => :thursday
AIXM.day(:tuesday).covered_by?(from..to)   # => true

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

Instance Method Summary collapse

Methods included from Concerns::HashEquality

#eql?

Constructor Details

#initialize(day = :any) ⇒ Day

Set the given day of the week or special named day.

Parameters:

  • day (Symbol, String, Integer) (defaults to: :any)

    any from DAYS or 0=Monday to 6=Sunday



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

#daySymbol

Day of the week or special named day

Returns:

  • (Symbol)

    any from DAYS



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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


93
94
95
# File 'lib/aixm/schedule/day.rb', line 93

def any?
  day == :any
end

#covered_by?(other) ⇒ Boolean

Note:

Only weekdays and :any can be computed!

Whether this schedule day falls within the given range of schedule days.

Parameters:

Returns:

  • (Boolean)

Raises:

  • RuntimeError if anything but workdays or :any are involved



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

#hashObject

See Also:

  • Object#hash


86
87
88
# File 'lib/aixm/schedule/day.rb', line 86

def hash
  [self.class, day].hash
end

#inspectObject



54
55
56
# File 'lib/aixm/schedule/day.rb', line 54

def inspect
  %Q(#<#{self.class} #{to_s}>)
end

#predAIXM::Schedule::Day Also known as: prev

Create new day one day prior to this one.

Returns:



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.

Returns:

  • (Boolean)


100
101
102
# File 'lib/aixm/schedule/day.rb', line 100

def sortable?
  WEEKDAYS.include? day
end

#succAIXM::Schedule::Day Also known as: next

Create new day one day after this one.

Returns:



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_sString

Human readable representation such as “monday” or “day preceding workday”

Returns:

  • (String)


43
44
45
# File 'lib/aixm/schedule/day.rb', line 43

def to_s
  day.to_s.gsub('_', ' ')
end

#to_symSymbol

Symbol used to initialize this day

Returns:

  • (Symbol)


50
51
52
# File 'lib/aixm/schedule/day.rb', line 50

def to_sym
  day.to_s.to_sym
end