Class: SerialScheduler::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/serial_scheduler/converter.rb

Class Method Summary collapse

Class Method Details

.floor_to_five(fixnum) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/serial_scheduler/converter.rb', line 30

def floor_to_five(fixnum)
  unit_digit = fixnum % 10
  if unit_digit < 5
    fixnum - unit_digit
  else
    fixnum - (unit_digit - 5)
  end
end

.for_each_hour(from, to) ⇒ Object



53
54
55
56
57
# File 'lib/serial_scheduler/converter.rb', line 53

def for_each_hour(from, to)
  for_each_time_with_step(from, to, 1.hour) do |time|
    yield(time)
  end
end

.for_each_time_with_step(from, to, step) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/serial_scheduler/converter.rb', line 59

def for_each_time_with_step(from, to, step)
  current_time = DateTime.parse(from)
  max_time = DateTime.parse(to)
  max_time = max_time + 1.day if max_time <= current_time
  while current_time <= max_time
    yield(SerialScheduler::Converter.to_hour_and_minute(current_time))
    current_time += step
  end
end

.formated_hour_and_minute_string(hours, minutes) ⇒ Object



39
40
41
42
43
# File 'lib/serial_scheduler/converter.rb', line 39

def formated_hour_and_minute_string(hours, minutes)
  minutes = minutes < 10 ? "0#{minutes}" : "#{minutes}"
  hours = hours < 10 ? "0#{hours}" : "#{hours}"
  "#{hours}:#{minutes}"
end

.hour_and_minute(time_or_datetime) ⇒ Object



10
11
12
13
# File 'lib/serial_scheduler/converter.rb', line 10

def hour_and_minute(time_or_datetime)
  datetime = to_datetime(time_or_datetime)
  formated_hour_and_minute_string(datetime.hour, datetime.min)
end

.hour_and_minute_in_5_minute_steps(time_or_datetime) ⇒ Object



19
20
21
22
23
# File 'lib/serial_scheduler/converter.rb', line 19

def hour_and_minute_in_5_minute_steps(time_or_datetime)
  datetime = to_datetime(time_or_datetime)
  minutes = floor_to_five(datetime.min)
  formated_hour_and_minute_string(datetime.hour, minutes)
end

.hours_between(from, to) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/serial_scheduler/converter.rb', line 45

def hours_between(from, to)
  hours = []
  for_each_hour(from,to) do |hour|
    hours << hour
  end
  hours
end

.to_datetime(string_or_time_or_datetime) ⇒ Object



4
5
6
7
8
# File 'lib/serial_scheduler/converter.rb', line 4

def to_datetime(string_or_time_or_datetime)
  string_or_time_or_datetime.kind_of?(String) ?
    DateTime.parse(string_or_time_or_datetime) :
    string_or_time_or_datetime.to_datetime
end

.to_hour_and_minute(time_or_datetime) ⇒ Object



15
16
17
# File 'lib/serial_scheduler/converter.rb', line 15

def to_hour_and_minute(time_or_datetime)
  hour_and_minute(time_or_datetime)
end

.weekday_hour_and_minute_in_5_minute_steps(time_or_datetime) ⇒ Object



25
26
27
28
# File 'lib/serial_scheduler/converter.rb', line 25

def weekday_hour_and_minute_in_5_minute_steps(time_or_datetime)
  datetime = to_datetime(time_or_datetime)
  "#{datetime.wday}:#{hour_and_minute_in_5_minute_steps(datetime)}"
end