Class: Huey::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/huey/event.rb

Overview

An event encapsulates logic to send to a group, either at a certain time or arbitrarily

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Event

Returns a new instance of Event.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/huey/event.rb', line 35

def initialize(options)
  [:group, :actions].each do |key|
    raise ArgumentError, "You must supply #{key} to create an event" unless options.keys.include?(key)

    self.instance_variable_set("@#{key}".to_sym, options[key])
  end

  self.at = options[:at]
  self.name = options[:name]
  self.group = Huey::Group.find(self.group) unless self.group.is_a?(Huey::Group)

  Huey::Event.all << self
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



7
8
9
# File 'lib/huey/event.rb', line 7

def actions
  @actions
end

#atObject

Returns the value of attribute at.



7
8
9
# File 'lib/huey/event.rb', line 7

def at
  @at
end

#groupObject

Returns the value of attribute group.



7
8
9
# File 'lib/huey/event.rb', line 7

def group
  @group
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/huey/event.rb', line 7

def name
  @name
end

Class Method Details

.allObject



23
24
25
# File 'lib/huey/event.rb', line 23

def self.all
  @all ||= []
end

.execute(force = false) ⇒ Object



31
32
33
# File 'lib/huey/event.rb', line 31

def self.execute(force = false)
  Huey::Event.all.collect {|s| s.execute(force)}
end

.find(name) ⇒ Object



27
28
29
# File 'lib/huey/event.rb', line 27

def self.find(name)
  Huey::Event.all.find {|s| s.name == name}
end

.import(file) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/huey/event.rb', line 9

def self.import(file)
  hash = YAML.load_file(file)

  hash.each do |key, value|
    options = {name: key}
    value.each do |k, v|
      options[k.to_sym] = v
    end

    Huey::Event.new(options)
  end
  Huey::Event.all
end

Instance Method Details

#execute(force = false) ⇒ Object



53
54
55
# File 'lib/huey/event.rb', line 53

def execute(force = false)
  self.group.send(:update, actions) if force || at.nil? || should_run?
end

#should_run?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/huey/event.rb', line 49

def should_run?
  ((Chronic.parse(self.at) - 0.5)..(Chronic.parse(self.at) + 0.5)).cover?(Time.now)
end