Sha256: 6bf74c5d144ecbd5d661d41749ed27b44bf614e0b9a84c2f233e43978906b95c

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require "micro/attributes/version"
require "micro/attributes/utils"
require "micro/attributes/macros"

module Micro
  module Attributes
    def self.included(base)
      base.extend Macros

      base.class_eval do
        private_class_method :__attribute
        private_class_method :__attributes
        private_class_method :__attribute_data
        private_class_method :__attribute_data!
        private_class_method :__attributes_data
      end

      def base.inherited(subclass)
        self.attributes_data({}).each do |name, value|
          subclass.attribute(value.nil? ? name : {name => value})
        end
      end
    end

    def self.to_initialize
      @to_initialize ||= Module.new do
        def self.included(base)
          base.send(:include, Micro::Attributes)

          base.class_eval(<<-RUBY)
            def initialize(arg)
              self.attributes = arg
            end

            def with_attribute(key, val)
              self.class.new(attributes.merge(key => val))
            end

            def with_attributes(arg)
              self.class.new(attributes.merge(arg))
            end
          RUBY
        end
      end
    end

    def attribute?(name)
      self.class.attribute?(name)
    end

    def attributes=(arg)
      self.class.attributes_data(Utils.hash_argument!(arg)).each do |name, value|
        instance_variable_set("@#{name}", value) if attribute?(name)
      end
    end

    def attributes
      state =
        self.class.attributes.each_with_object({}) do |name, memo|
          if instance_variable_defined?(iv_name = "@#{name}")
            memo[name] = instance_variable_get(iv_name)
          end
        end

      self.class.attributes_data(state)
    end

    protected :attributes=
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
u-attributes-0.6.1 lib/micro/attributes.rb
u-attributes-0.6.0 lib/micro/attributes.rb