Class: PulseMeter::Sensor::Configuration
- Inherits:
-
Object
- Object
- PulseMeter::Sensor::Configuration
- Includes:
- Enumerable, Mixins::Utils
- Defined in:
- lib/pulse-meter/sensor/configuration.rb
Overview
Constructs multiple sensors from configuration passed
Instance Method Summary collapse
-
#add_sensor(name, opts) ⇒ Object
Adds sensor.
-
#each ⇒ Object
Iterates over each sensor.
-
#has_sensor?(name) ⇒ Boolean
Returns true value if sensor with specified name exists in configuration, false otherwise.
-
#initialize(opts = {}) ⇒ Configuration
constructor
Initializes sensors.
-
#method_missing(name, *args) ⇒ Object
Invokes event(_at) for any sensor.
-
#sensor(name) {|sensor| ... } ⇒ Object
Returns previously initialized sensor by name.
Methods included from Enumerable
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, #uniqid
Constructor Details
#initialize(opts = {}) ⇒ Configuration
Initializes sensors
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
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
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 |
#each ⇒ Object
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
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
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 |