Sha256: c979e5108b97d5fda3635aaebff350da35351433062a4c5a63dbf6cbd369d872

Contents?: true

Size: 1.12 KB

Versions: 16

Compression:

Stored size: 1.12 KB

Contents

module ROM
  # Internal support module for class-level settings
  #
  # @private
  module ClassMacros
    # Specify what macros a class will use
    #
    # @example
    #   class MyClass
    #     extend ROM::ClassMacros
    #
    #     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'
    #
    # @api private
    def defines(*args)
      mod = Module.new

      args.each do |name|
        mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{name}(value = Undefined)
            if value == Undefined
              defined?(@#{name}) && @#{name}
            else
              @#{name} = value
            end
          end
        RUBY
      end

      delegates = args.map { |name| "klass.#{name}(#{name})" }.join("\n")

      mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
        def inherited(klass)
          super
          #{delegates}
        end
      RUBY

      extend(mod)
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
rom-support-2.0.0 lib/rom/support/class_macros.rb
rom-support-1.0.0 lib/rom/support/class_macros.rb
rom-support-1.0.0.rc1 lib/rom/support/class_macros.rb
rom-support-1.0.0.beta1 lib/rom/support/class_macros.rb
rom-support-0.1.0 lib/rom/support/class_macros.rb
rom-0.8.1 lib/rom/support/class_macros.rb
rom-0.8.0 lib/rom/support/class_macros.rb
rom-0.7.1 lib/rom/support/class_macros.rb
rom-0.7.0 lib/rom/support/class_macros.rb
rom-0.6.2 lib/rom/support/class_macros.rb
rom-0.6.1 lib/rom/support/class_macros.rb
rom-0.6.0 lib/rom/support/class_macros.rb
rom-0.6.0.rc1 lib/rom/support/class_macros.rb
rom-0.6.0.beta3 lib/rom/support/class_macros.rb
rom-0.6.0.beta2 lib/rom/support/class_macros.rb
rom-0.6.0.beta1 lib/rom/support/class_macros.rb