Sha256: 9170704a8f3f4b2ab62a40a768517ef09cb2afa452344bef9ef2beb34bba93fd

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

module DeviceMap
  module Properties
    class UnknownProperty < StandardError; end

    module DSL
      module ClassMethods
        def property(name, type: :string, source_name: name)
          attr_reader name
          properties[source_name] = Property.new(name, type, source_name)
        end

        # FIXME: This method should not be public
        def properties
          @properties ||= {}
        end
      end

      def self.included(base)
        base.extend(ClassMethods)
      end

      def initialize(attrs)
        attrs.each do |name, value|
          property = properties.fetch(name.to_sym) do
            fail UnknownProperty, "Property #{name} is not defined"
          end

          attr_name = property.name
          casted_value = property.cast(value)
          instance_variable_set(:"@#{attr_name}", casted_value)
        end
      end

      def ==(other)
        properties.all? do |_, property|
          attr_name = property.name
          public_send(attr_name) == other.public_send(attr_name)
        end
      end

      private

      def properties
        self.class.properties
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
device_map-0.1.2 lib/device_map/properties/dsl.rb
device_map-0.1.1 lib/device_map/properties/dsl.rb