Module: TriggerSwitchD::Schedule::InstanceMethods

Defined in:
lib/trigger_switch_d/schedule.rb

Instance Method Summary collapse

Instance Method Details

#__action_is_scheduled__(action) ⇒ Object



90
91
92
# File 'lib/trigger_switch_d/schedule.rb', line 90

def __action_is_scheduled__(action)
  by_name.keys.include?(action.to_s)
end

#__insert_action__(action) ⇒ Object

:nodoc:



82
83
84
85
86
87
88
# File 'lib/trigger_switch_d/schedule.rb', line 82

def __insert_action__(action) #:nodoc:
  return if __action_is_scheduled__(action)
  name = action.to_s
  by_name[name] = action
  cronolog_at = cronological[action.at] ||= {}
  cronolog_at[name] = action
end

#__remove_executed_actions_from_schedule__(at) ⇒ Object

:nodoc:



73
74
75
76
77
78
79
80
# File 'lib/trigger_switch_d/schedule.rb', line 73

def __remove_executed_actions_from_schedule__(at) #:nodoc:
  cronolog = cronological
  cronolog_at = cronolog[at]
  cronolog_at.values.clone.each do |element|
    by_name.delete(element.to_s)
  end unless cronolog_at == nil
  cronolog.delete(at)
end

#by_nameObject

Hash of actions accessible by name



69
70
71
# File 'lib/trigger_switch_d/schedule.rb', line 69

def by_name
  self[:all]
end

#cronologicalObject

Hash of actions accessible by “HH:MM” format



64
65
66
# File 'lib/trigger_switch_d/schedule.rb', line 64

def cronological
  self[:cronological]
end

#delete(name) ⇒ Object

deletes action from schedule, returning the action on exit



51
52
53
54
# File 'lib/trigger_switch_d/schedule.rb', line 51

def delete(name)
  action = by_name.delete(name)
  cronological[action.at].delete(name)
end

#execute(at, output) ⇒ Object

Executes action and removes it from the schedule



57
58
59
60
61
# File 'lib/trigger_switch_d/schedule.rb', line 57

def execute(at,output)
  cronolog_at = cronological[at]
  cronolog_at.values.each { |action| action.execute(output) } unless cronolog_at == nil
  self.__remove_executed_actions_from_schedule__(at)
end

#find(name) ⇒ Object

find by name



32
33
34
# File 'lib/trigger_switch_d/schedule.rb', line 32

def find(name)
  by_name[name]
end

#next_action_at(hour_minute) ⇒ Object

Returns when the next action is scheduled to occur based on the hour_minute. The format is “HH:MM” for both hour_minute as well as the result, with the exception that if no next action is found it will return “23:59:59”



41
42
43
44
45
46
47
48
# File 'lib/trigger_switch_d/schedule.rb', line 41

def next_action_at(hour_minute)
  end_of_day = "23:59:59"
  return end_of_day unless hour_minute =~ /\d{2}:\d{2}/
  time = Time.parse(hour_minute)
  cronological.keys.sort {|first,second| Time.parse(first) <=> Time.parse(second)}.find(Proc.new {end_of_day}) do |at|
    time < Time.parse(at)
  end
end

#schedule_action(action) ⇒ Object



26
27
28
29
# File 'lib/trigger_switch_d/schedule.rb', line 26

def schedule_action(action)
  self.__insert_action__(action)
  find(action.to_s)
end