lib/mqtt/homie/homie_object.rb in mqtt-homie-0.1.0 vs lib/mqtt/homie/homie_object.rb in mqtt-homie-0.1.1
- old
+ new
@@ -1,39 +1,17 @@
require "observer"
module MQTT
module Homie
class HomieObject
- attr_reader :id
+ include HomieAttribute
def initialize(options = {})
- @id = set(options, :id, required: true)
- raise "invalid id" unless valid_id?
+ homie_attr_init(options)
end
def topic
@id
- end
-
- def homie_attributes
- data = {}
- end
-
- private
-
- def valid_id?
- @id && @id.kind_of?(String) && @id.match(/^[-a-z0-9]+$/) && !@id.start_with?("-")
- end
-
- def set(options = {}, name, default: nil, required: false, enum: nil, data_type: nil)
- value = options.include?(name) ? options[name] : default
- raise "#{name} is required for #{object_type} #{@id}" if required && value.nil?
- raise "expected #{name} to be a #{data_type} for #{object_type} #{@id}" if data_type && !value.kind_of?(data_type)
- value
- end
-
- def object_type
- self.class.name.split("::").last
end
end
end
end