Sha256: 2133b5170825f883279aab9fb2675582718309200da663b0434aa8a4c3ba0cf8

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Micro
  module Attributes
    module Macros
      def __attributes_data__
        @__attributes_data__ ||= {}
      end

      def __attributes
        @__attributes ||= Set.new
      end

      def __attribute_reader(name)
        __attributes.add(name)

        attr_reader(name)
      end

      def __attribute_set(key, can_overwrite, options)
        name = key.to_s
        has_attribute = attribute?(name)

        __attribute_reader(name) unless has_attribute
        __attributes_data__[name] = options[:default] if can_overwrite || !has_attribute

        __call_after_attribute_set__(name, options)
      end

      def __call_after_attribute_set__(attr_name, options); end

      def __attributes_set_after_inherit__(arg)
        arg.each { |key, val| __attribute_set(key, true, default: val) }
      end

      def attribute?(name)
        __attributes.member?(name.to_s)
      end

      def attribute(name, options = Kind::Empty::HASH)
        __attribute_set(name, false, options)
      end

      def attributes(*args)
        return __attributes.to_a if args.empty?

        args.flatten!
        args.each do |arg|
          if arg.is_a?(String) || arg.is_a?(Symbol)
            __attribute_set(arg, false, Kind::Empty::HASH)
          else
            raise Kind::Error.new('String/Symbol'.freeze, arg)
          end
        end
      end

      module ForSubclasses
        WRONG_NUMBER_OF_ARGS = 'wrong number of arguments (given 0, expected 1 or more)'.freeze

        def attribute!(name, options = Kind::Empty::HASH)
          __attribute_set(name, true, options)
        end

        private_constant :WRONG_NUMBER_OF_ARGS
      end

      private_constant :ForSubclasses
    end

    private_constant :Macros
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
u-attributes-2.0.1 lib/micro/attributes/macros.rb
u-attributes-2.0.0 lib/micro/attributes/macros.rb