Module: WorkingCalendar::CastHelper

Included in:
SingleDate, WeekDay
Defined in:
lib/working_calendar/cast_helper.rb

Constant Summary collapse

DAY_NAMES =
[:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

Instance Method Summary collapse

Instance Method Details

#cast_date(date) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/working_calendar/cast_helper.rb', line 6

def cast_date(date)
  case 
    when date.is_a?(Date) then date
    when date.respond_to?(:to_date) then date.to_date
    when date.is_a?(String) then Date.parse(date)
    else raise(ArgumentError, "Invalid date #{date}")
  end
end

#cast_day_name(day_name) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/working_calendar/cast_helper.rb', line 15

def cast_day_name(day_name)
  normalized_day_name = day_name.downcase.to_sym
  if DAY_NAMES.include? normalized_day_name
    normalized_day_name
  else
    raise ArgumentError, "Invalid day name #{day_name}"
  end
end

#cast_hours_ranges(hours_ranges) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/working_calendar/cast_helper.rb', line 24

def cast_hours_ranges(hours_ranges)
  hours_ranges.each_with_object({}) do |(from_expression, to_expression), hash|
    from = Timing::HourMinutesSeconds.parse from_expression
    to = Timing::HourMinutesSeconds.parse to_expression
    raise ArgumentError, "Invalid range #{from} - #{to}" if from >= to
    hash[from] = to
  end
end