Class: PulseMeter::Sensor::Configuration

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Mixins::Utils
Defined in:
lib/pulse-meter/sensor/configuration.rb

Overview

Constructs multiple sensors from configuration passed

Instance Method Summary collapse

Methods included from Enumerable

#convert_time, #to_table

Methods included from Mixins::Utils

#assert_array!, #assert_positive_integer!, #assert_ranged_float!, #camelize, #camelize_keys, #constantize, #each_subset, #parse_time, #subsets_of, #symbolize_keys, #titleize, #underscore, #uniqid

Constructor Details

#initialize(opts = {}) ⇒ Configuration

Initializes sensors

Parameters:

  • opts (Hash) (defaults to: {})

    sensors’ configuration



10
11
12
# File 'lib/pulse-meter/sensor/configuration.rb', line 10

def initialize(opts = {})
  @opts = opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Invokes event(_at) for any sensor

Raises:

  • (ArgumentError)

    unless sensor exists



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pulse-meter/sensor/configuration.rb', line 55

def method_missing(name, *args)
  with_resque do
    name = name.to_s
    if sensors.has_key?(name)
      sensors[name].event(*args)
    elsif name =~ /\A(.*)_at\z/
      sensor_name = $1
      sensors[sensor_name].event_at(*args) if sensors.has_key?(sensor_name) 
    end
  end
end

Instance Method Details

#add_sensor(name, opts) ⇒ Object

Adds sensor

Parameters:

  • name (Symbol)

    sensor name

  • opts (Hash)

    sensor options



38
39
40
41
42
# File 'lib/pulse-meter/sensor/configuration.rb', line 38

def add_sensor(name, opts)
  with_resque do
    sensors[name.to_s] = create_sensor(name, opts)
  end
end

#eachObject

Iterates over each sensor



45
46
47
48
49
50
51
# File 'lib/pulse-meter/sensor/configuration.rb', line 45

def each
  with_resque do
    sensors.each_value do |s|
      yield(s)
    end
  end
end

#has_sensor?(name) ⇒ Boolean

Returns true value if sensor with specified name exists in configuration, false otherwise

Parameters:

  • name (Symbol)

    sensor name

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/pulse-meter/sensor/configuration.rb', line 27

def has_sensor?(name)
  has_sensor = false
  with_resque do
    has_sensor = sensors.has_key?(name)
  end
  has_sensor
end

#sensor(name) {|sensor| ... } ⇒ Object

Returns previously initialized sensor by name

Parameters:

  • name (Symbol)

    sensor name

Yields:

  • (sensor)

    Gives sensor(if it is found) to the block

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/pulse-meter/sensor/configuration.rb', line 17

def sensor(name)
  raise ArgumentError, "need a block" unless block_given?
  with_resque do
    s = sensors[name.to_s]
    yield(s) if s
  end
end