Module: TriggerSwitchD::Schedule::InstanceMethods

Defined in:
lib/trigger_switch_d/schedule.rb

Instance Method Summary collapse

Instance Method Details

#_insert_action_(action) ⇒ Object

:nodoc:



79
80
81
82
83
84
# File 'lib/trigger_switch_d/schedule.rb', line 79

def _insert_action_(action) #:nodoc:
  name = action.to_s
  by_name[name] = action
  cronological[action.at] ||= {}
  cronological[action.at][name] = by_name[name]
end

#_remove_executed_actions_from_schedule_(at) ⇒ Object

:nodoc:



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

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

#by_nameObject

Hash of actions accessible by name



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

def by_name
  self[:all]
end

#cronologicalObject

Hash of actions accessible by “HH:MM” format



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

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
# File 'lib/trigger_switch_d/schedule.rb', line 57

def execute(at,output)
  cronological[at].values.each { |action| action.execute(output) } unless cronological[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 {|x,y| Time.parse(x) <=> Time.parse(y)}.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) if find(action.to_s) == nil
  find(action.to_s)
end