Class: Biz::DayOfWeek

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

Constant Summary collapse

SYMBOLS =
[:sun, :mon, :tue, :wed, :thu, :fri, :sat]
DAYS =
[
  SUNDAY    = new(0),
  MONDAY    = new(1),
  TUESDAY   = new(2),
  WEDNESDAY = new(3),
  THURSDAY  = new(4),
  FRIDAY    = new(5),
  SATURDAY  = new(6)
]
WEEKDAYS =
[
  MONDAY,
  TUESDAY,
  WEDNESDAY,
  THURSDAY,
  FRIDAY
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wday) ⇒ DayOfWeek

Returns a new instance of DayOfWeek.



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

def initialize(wday)
  @wday = Integer(wday)
end

Instance Attribute Details

#wdayObject (readonly)

Returns the value of attribute wday.



28
29
30
# File 'lib/biz/day_of_week.rb', line 28

def wday
  @wday
end

Class Method Details

.firstObject



14
15
16
# File 'lib/biz/day_of_week.rb', line 14

def self.first
  DAYS.first
end

.from_symbol(symbol) ⇒ Object



10
11
12
# File 'lib/biz/day_of_week.rb', line 10

def self.from_symbol(symbol)
  DAYS.fetch(SYMBOLS.index(symbol))
end

.from_time(time) ⇒ Object Also known as: from_date



6
7
8
# File 'lib/biz/day_of_week.rb', line 6

def self.from_time(time)
  DAYS.fetch(time.wday)
end

.lastObject



18
19
20
# File 'lib/biz/day_of_week.rb', line 18

def self.last
  DAYS.last
end

Instance Method Details

#contains?(week_time) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/biz/day_of_week.rb', line 34

def contains?(week_time)
  minutes.cover?(week_time)
end

#day_minute(week_minute) ⇒ Object



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

def day_minute(week_minute)
  (week_minute - 1) % Time::MINUTES_IN_DAY + 1
end

#end_minuteObject



42
43
44
# File 'lib/biz/day_of_week.rb', line 42

def end_minute
  start_minute + Time::MINUTES_IN_DAY
end

#minutesObject



46
47
48
# File 'lib/biz/day_of_week.rb', line 46

def minutes
  start_minute..end_minute
end

#start_minuteObject



38
39
40
# File 'lib/biz/day_of_week.rb', line 38

def start_minute
  wday * Time::MINUTES_IN_DAY
end

#symbolObject



58
59
60
# File 'lib/biz/day_of_week.rb', line 58

def symbol
  SYMBOLS.fetch(wday)
end

#week_minute(day_minute) ⇒ Object



50
51
52
# File 'lib/biz/day_of_week.rb', line 50

def week_minute(day_minute)
  start_minute + day_minute
end