Class: Houston::Trigger

Inherits:
Struct
  • Object
show all
Defined in:
lib/houston/boot/triggers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(triggers, *args) ⇒ Trigger

Returns a new instance of Trigger.



54
55
56
57
58
# File 'lib/houston/boot/triggers.rb', line 54

def initialize(triggers, *args)
  @triggers = triggers
  @callback = method(:call).to_proc
  super *args
end

Instance Attribute Details

#actionObject

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



52
53
54
# File 'lib/houston/boot/triggers.rb', line 52

def action
  @action
end

#method_nameObject

Returns the value of attribute method_name

Returns:

  • (Object)

    the current value of method_name



52
53
54
# File 'lib/houston/boot/triggers.rb', line 52

def method_name
  @method_name
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



52
53
54
# File 'lib/houston/boot/triggers.rb', line 52

def params
  @params
end

#persistent_trigger_idObject

Returns the value of attribute persistent_trigger_id

Returns:

  • (Object)

    the current value of persistent_trigger_id



52
53
54
# File 'lib/houston/boot/triggers.rb', line 52

def persistent_trigger_id
  @persistent_trigger_id
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



52
53
54
# File 'lib/houston/boot/triggers.rb', line 52

def value
  @value
end

Instance Method Details

#call(params = {}) ⇒ Object



76
77
78
79
80
81
# File 'lib/houston/boot/triggers.rb', line 76

def call(params={})
  Rails.logger.info "\e[34m[#{to_s} => #{action}]\e[0m"
  config.actions.run action, self.params.merge(params.to_h),
    trigger: to_s,
    async: triggers.async
end

#register!Object



60
61
62
63
64
65
66
# File 'lib/houston/boot/triggers.rb', line 60

def register!
  case method_name
  when :every then config.timer.every(value, &callback)
  when :on then config.observer.on(value, &callback)
  else raise NotImplementedError, "Unrecognized method name: #{method_name.inspect}"
  end
end

#to_sObject



83
84
85
# File 'lib/houston/boot/triggers.rb', line 83

def to_s
  "#{method_name}(#{value})"
end

#unregister!Object



68
69
70
71
72
73
74
# File 'lib/houston/boot/triggers.rb', line 68

def unregister!
  case method_name
  when :every then config.timer.stop(value, callback)
  when :on then config.observer.off(value, &callback)
  else raise NotImplementedError, "Unrecognized method name: #{method_name.inspect}"
  end
end