Class: TriggerSwitchD::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/trigger_switch_d/action.rb

Overview

Uses the ruby engine to transform code_line into method execute on an instance of Action

Instance Method Summary collapse

Constructor Details

#initialize(code_line, devices, environment = Config.default_environment, date = Date.today) ⇒ Action

Takes a code_line to be scheduled or executed together with the devices, environment and date to validate it.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/trigger_switch_d/action.rb', line 26

def initialize(code_line,devices,environment=Config.default_environment,date=Date.today)
  @name = code_line.strip
  @date = date
  @at_location = environment[:at_location]
  @lookup = environment[:lookup]
  tokens = code_line.scan(/(\w+) (\w+) (\w+) ([\w\d:]+)/u).flatten
  (class << self;self;end).methodize_device(tokens[1],devices[tokens[1]],@lookup) if devices.include? tokens[1]
  code = tokens.map do |token|
    (@lookup.include? token) ? @lookup[token] : token
  end.reverse
  @at, executable = send(code[3], send(code[2], send(code[1], code[0])))
  (class << self;self;end).methodize_executable(executable)
end

Instance Method Details

#activate(*args) ⇒ Object

:nodoc:



40
41
42
# File 'lib/trigger_switch_d/action.rb', line 40

def activate(*args) #:nodoc:
  action(args[0][0],args[0][1],:turn_on)
end

#at(*args) ⇒ Object

:nodoc:



48
49
50
51
52
53
54
# File 'lib/trigger_switch_d/action.rb', line 48

def at(*args) #:nodoc:
  return @at if args[0] == nil
  if (args[0] =~ /(?:2[0-3]|[01]\d):[0-5]\d/) == 0
    return args[0]
  end
  return @date.extend(Daylight).send(args[0],@at_location).localtime.strftime("%H:%M")
end

#deactivate(*args) ⇒ Object

:nodoc:



44
45
46
# File 'lib/trigger_switch_d/action.rb', line 44

def deactivate(*args) #:nodoc:
  action(args[0][0],args[0][1],:turn_off)
end

#to_sObject



56
57
58
# File 'lib/trigger_switch_d/action.rb', line 56

def to_s
  @name
end