Sha256: bd854a08feb4508cd52d14465e3f60242bfbc192ca010ef353128c5fd54d5ebc

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

require 'dry/core/constants'

module Dry
  module Core
    # Internal support module for class-level settings
    #
    # @api public
    module ClassAttributes
      include Constants

      # Specify what attributes a class will use
      #
      # @example
      #   class MyClass
      #     extend Dry::Core::ClassAttributes
      #
      #     defines :one, :two
      #
      #     one 1
      #     two 2
      #   end
      #
      #   class OtherClass < MyClass
      #     two 'two'
      #   end
      #
      #   MyClass.one # => 1
      #   MyClass.two # => 2
      #
      #   OtherClass.one # => 1
      #   OtherClass.two # => 'two'
      def defines(*args)
        mod = Module.new do
          args.each do |name|
            define_method(name) do |value = Undefined|
              ivar = "@#{name}"

              if value == Undefined
                if instance_variable_defined?(ivar)
                  instance_variable_get(ivar)
                else
                  nil
                end
              else
                instance_variable_set(ivar, value)
              end
            end
          end

          define_method(:inherited) do |klass|
            args.each { |name| klass.public_send(name, send(name)) }

            super(klass)
          end
        end

        extend(mod)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-core-0.3.4 lib/dry/core/class_attributes.rb
dry-core-0.3.3 lib/dry/core/class_attributes.rb
dry-core-0.3.2 lib/dry/core/class_attributes.rb
dry-core-0.3.1 lib/dry/core/class_attributes.rb