module Scorpion::Object::ClassMethods

Public Instance Methods

attr_dependency( name, contract, **options ) click to toggle source

Define a single dependency and accessor. @param [Symbol] name of the dependency. @param [Class,Module,Symbol] contract describing the desired behavior of the dependency.

# File lib/scorpion/object.rb, line 125
def attr_dependency( name, contract, **options )
  attr = injected_attributes.define_attribute name, contract, **options
  build_injected_attribute attr
  adjust_injected_attribute_visibility attr
  validate_initializer_injections
  attr
end
depend_on( &block ) click to toggle source

Tells a {Scorpion} what to inject into the class when it is constructed @return [nil] @see AttributeSet#define

# File lib/scorpion/object.rb, line 116
def depend_on( &block )
  injected_attributes.define &block
  build_injected_attributes
  validate_initializer_injections
end
initializer_injections() click to toggle source

@!attribute @return [Scorpion::AttributeSet] the set of injected attributes.

# File lib/scorpion/object.rb, line 145
def initializer_injections
  @initializer_injections ||= begin
    if superclass.respond_to?( :initializer_injections )
      superclass.initializer_injections
    else
      AttributeSet.new
    end
  end
end
injected_attributes() click to toggle source

@!attribute @return [Scorpion::AttributeSet] the set of injected attributes.

# File lib/scorpion/object.rb, line 135
def injected_attributes
  @injected_attributes ||= begin
    attrs = AttributeSet.new
    attrs.inherit! superclass.injected_attributes if superclass.respond_to? :injected_attributes
    attrs
  end
end

Private Instance Methods

adjust_injected_attribute_visibility( attr ) click to toggle source
# File lib/scorpion/object.rb, line 192
        def adjust_injected_attribute_visibility( attr )
          unless attr.public?
            class_eval <<-RUBY, __FILE__, __LINE__ + 1
              private :#{ attr.name }=
              private :#{ attr.name }?
            RUBY
          end

          if attr.private?
            class_eval <<-RUBY, __FILE__, __LINE__ + 1
              private :#{ attr.name }
            RUBY
          end
        end
build_injected_attribute( attr ) click to toggle source
# File lib/scorpion/object.rb, line 173
        def build_injected_attribute( attr )
          class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{ attr.name }
              @#{ attr.name } ||= begin
                attr = injected_attributes[ :#{ attr.name } ]
                ( scorpion_hunt || scorpion ).fetch( attr.contract )
              end
            end

            def #{ attr.name }=( value )
              @#{ attr.name } = value
            end

            def #{ attr.name }?
              !!@#{ attr.name }
            end
          RUBY
        end
build_injected_attributes() click to toggle source
# File lib/scorpion/object.rb, line 166
def build_injected_attributes
  injected_attributes.each do |attr|
    build_injected_attribute attr
    adjust_injected_attribute_visibility attr
  end
end
validate_initializer_injections() click to toggle source
# File lib/scorpion/object.rb, line 157
def validate_initializer_injections
  initializer_injections.each do |attr|
    injected = injected_attributes[ attr.name ]
    if injected.contract != attr.contract
      fail Scorpion::ContractMismatchError.new( self, attr, injected )
    end
  end
end