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 Attribute Summary collapse
-
#sensors ⇒ Object
readonly
Returns the value of attribute sensors.
Instance Method Summary collapse
-
#add_sensor(name, opts) ⇒ Object
Adds sensor.
-
#each ⇒ Object
Iterates over each sensor.
-
#initialize(opts = {}) ⇒ Configuration
constructor
Initializes sensors.
-
#method_missing(name, *args) ⇒ Object
Invokes event for any sensor.
-
#sensor(name) ⇒ Sensor
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, #subsets_of, #symbolize_keys, #titleize, #uniqid
Constructor Details
#initialize(opts = {}) ⇒ Configuration
Initializes sensors
14 15 16 17 18 19 |
# File 'lib/pulse-meter/sensor/configuration.rb', line 14 def initialize(opts = {}) @sensors = {} opts.each do |name, opts| add_sensor(name, opts) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Invokes event for any sensor
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pulse-meter/sensor/configuration.rb', line 49 def method_missing(name, *args) name = name.to_s if @sensors.has_key?(name) @sensors[name].event(*args) elsif (name =~ /^(.*)_at$/) && @sensors.has_key?($1) @sensors[$1].event_at(*args) else raise ArgumentError, "Unknown sensor: `#{name}'" end end |
Instance Attribute Details
#sensors ⇒ Object (readonly)
Returns the value of attribute sensors.
10 11 12 |
# File 'lib/pulse-meter/sensor/configuration.rb', line 10 def sensors @sensors end |
Instance Method Details
#add_sensor(name, opts) ⇒ Object
Adds sensor
24 25 26 27 28 29 30 31 |
# File 'lib/pulse-meter/sensor/configuration.rb', line 24 def add_sensor(name, opts) sensor_type = opts.respond_to?(:sensor_type) ? opts.sensor_type : opts[:sensor_type] klass_s = sensor_class(sensor_type) klass = constantize(klass_s) raise ArgumentError, "#{klass_s} is not a valid class for a sensor" unless klass args = (opts.respond_to?(:args) ? opts.args : opts[:args]) || {} @sensors[name.to_s] = klass.new(name, symbolize_keys(args.to_hash)) end |
#each ⇒ Object
Iterates over each sensor
41 42 43 44 45 |
# File 'lib/pulse-meter/sensor/configuration.rb', line 41 def each @sensors.each_value do |sensor| yield(sensor) end end |
#sensor(name) ⇒ Sensor
Returns previously initialized sensor by name
36 37 38 |
# File 'lib/pulse-meter/sensor/configuration.rb', line 36 def sensor(name) @sensors[name.to_s] end |