Class: God::Condition

Inherits:
Behavior show all
Defined in:
lib/god/condition.rb

Direct Known Subclasses

EventCondition, PollCondition, TriggerCondition

Instance Attribute Summary collapse

Attributes inherited from Behavior

#watch

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Behavior

#after_restart, #after_start, #after_stop, #before_restart, #before_start, #before_stop, #valid?

Methods included from Configurable

#base_name, complain, #complain, #prepare, #reset, #valid?

Instance Attribute Details

#infoObject

Returns the value of attribute info.



5
6
7
# File 'lib/god/condition.rb', line 5

def info
  @info
end

#notifyObject

Returns the value of attribute notify.



5
6
7
# File 'lib/god/condition.rb', line 5

def notify
  @notify
end

#phaseObject

Returns the value of attribute phase.



5
6
7
# File 'lib/god/condition.rb', line 5

def phase
  @phase
end

#transitionObject

Returns the value of attribute transition.



5
6
7
# File 'lib/god/condition.rb', line 5

def transition
  @transition
end

Class Method Details

.generate(kind, watch) ⇒ Object

Generate a Condition of the given kind. The proper class if found by camel casing the kind (which is given as an underscored symbol).

+kind+ is the underscored symbol representing the class (e.g. :foo_bar for God::Conditions::FooBar)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/god/condition.rb', line 10

def self.generate(kind, watch)
  sym = kind.to_s.capitalize.gsub(/_(.)/) { Regexp.last_match(1).upcase }.intern
  c = God::Conditions.const_get(sym).new

  unless c.is_a?(PollCondition) || c.is_a?(EventCondition) || c.is_a?(TriggerCondition)
    abort "Condition '#{c.class.name}' must subclass God::PollCondition, God::EventCondition, or God::TriggerCondition"
  end

  if !EventHandler.loaded? && c.is_a?(EventCondition)
    abort "Condition '#{c.class.name}' requires an event system but none has been loaded"
  end

  c.watch = watch
  c
rescue NameError
  raise NoSuchConditionError, "No Condition found with the class name God::Conditions::#{sym}"
end

.valid?(condition) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/god/condition.rb', line 28

def self.valid?(condition)
  valid = true
  if condition.notify
    begin
      Contact.normalize(condition.notify)
    rescue ArgumentError => e
      valid &= Configurable.complain("Attribute 'notify' #{e.message}", condition)
    end
  end
  valid
end

Instance Method Details

#friendly_nameObject

Construct the friendly name of this Condition, looks like:

Condition FooBar on Watch ‘baz’



43
44
45
# File 'lib/god/condition.rb', line 43

def friendly_name
  "Condition #{self.class.name.split('::').last} on Watch '#{watch.name}'"
end