Sha256: 48763ac551dbaa4e021a937db79defef1ca7cd85cc70de324a72465c79d9edf2

Contents?: true

Size: 984 Bytes

Versions: 1

Compression:

Stored size: 984 Bytes

Contents

require "observer"

module MQTT
  module Homie
    class HomieObject
      attr_reader :id

      def initialize(options = {})
        @id = set(options, :id, required: true)
        raise "invalid id" unless valid_id?
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mqtt-homie-0.1.0 lib/mqtt/homie/homie_object.rb