Class: PoolParty::Monitor

Inherits:
Object show all
Defined in:
lib/poolparty/monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(monitor_name, &block) ⇒ Monitor

Returns a new instance of Monitor.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/poolparty/monitor.rb', line 20

def initialize(monitor_name, &block)
  msg =<<-EOE
You must pass a block with your monitor
  Example:
monitor :cpu do |c|
  vote_for(:expand) if c > 0.8
  configure if c < 0.1
end
  EOE
  raise PoolPartyError.create("MonitorDefinitionError", msg) unless block
  @name = monitor_name.to_sym
  @monitor_block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *a, &block) ⇒ Object (private)

We don’t want the methods actually executing since we are executing the methods in a cloud, we just want to store the output values of the methods, so we’ll store them in a hash with the method as the key and the value of the method as the value in an array



72
73
74
75
# File 'lib/poolparty/monitor.rb', line 72

def method_missing(meth,*a,&block)
  (methods[meth] ||= []) << a
  methods[meth].flatten!
end

Instance Attribute Details

#monitor_blockObject (readonly)

Returns the value of attribute monitor_block.



18
19
20
# File 'lib/poolparty/monitor.rb', line 18

def monitor_block
  @monitor_block
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/poolparty/monitor.rb', line 18

def name
  @name
end

#value_formatObject (readonly)

Returns the value of attribute value_format.



18
19
20
# File 'lib/poolparty/monitor.rb', line 18

def value_format
  @value_format
end

Instance Method Details

#format(meth = nil, &block) ⇒ Object

Format the monitor values Set the monitor format here. The default will be to turn the value into a float but to allow other formats, call the value here, for instance:

mon.format :to_s

Blocks are also permitted



50
51
52
# File 'lib/poolparty/monitor.rb', line 50

def format(meth=nil, &block)
  @value_format ||= (meth ? meth : block)
end

#run(val) ⇒ Object

Run the block given with the monitor with the given values on run. Clear the methods hash out so they don’t conflict with the previous values retrieved and return the methods available.



38
39
40
41
42
# File 'lib/poolparty/monitor.rb', line 38

def run(val)
  @methods = nil
  instance_exec format_value(val), &monitor_block
  methods
end