Sha256: f0ff3d8c9aad34ab9d5dfe764340cc952115417602e27c939254d0e513433306

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Micro
  module Attributes
    module Macros
      def __attributes
        @__attributes ||= Set.new
      end

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

      def __attribute(name)
        return false if attribute?(name)

        __attributes.add(name)
        attr_reader(name)

        return true
      end

      def __attributes_data
        @__attributes_data ||= {}
      end

      def __attribute_data(name, value)
        __attributes_data[name] = value if __attribute(name)
      end

      def attribute(arg)
        return __attribute_data(arg.to_s, nil) unless arg.is_a?(Hash)

        arg.each { |key, value| __attribute_data(key.to_s, value) }
      end

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

        args.flatten.each { |arg| attribute(arg) }
      end

      def attributes_data(arg)
        normalized_arg =
          arg.keys.each_with_object({}) { |key, memo| memo[key.to_s] = arg[key] }

        yield(__attributes_data.merge(normalized_arg))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
u-attributes-0.4.0 lib/micro/attributes/macros.rb