Sha256: b8cff34abc35963b696e5b87c4ef536323b7a4beade2d786fabfdfa240a562dc

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Scorpion
  # Builds an injectable constructor for a Scorpion::Object.
  class ObjectConstructor

    def initialize( base, arguments, &block )
      @base      = base
      @arguments = arguments
      @block     = block
    end

    def define
      @signature = []
      @body      = ""

      build_signature
      build_body

      add_initialize_block
      assemble
    end

    private
      attr_reader :base, :arguments, :block, :body, :signature

      def build_signature
        arguments.each do |key,expectation|
          signature << key

          base.initializer_injections.define_attribute key, *Array( expectation )
          base.attr_dependency key, *Array( expectation )
        end
      end

      def build_body
        arguments.each do |key,expectation|
          body << "@#{ key } = #{ key };"
        end
      end

      def add_initialize_block
        if block
          body << "__initialize_with_block( &block )"
          base.send :define_method, :__initialize_with_block, &block
        end
      end

      def assemble
        base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def initialize( #{ signature.join( ', ' ) }, &block )
            #{ body }
          end
        RUBY
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scorpion-ioc-0.4.0 lib/scorpion/object_constructor.rb