Sha256: 158b39795457ee3975daa1d994a14dff10e2bdd5f5b7679c19224fed0a74c60b

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'active_support/core_ext/string/inflections'

module DependencyInjection
  class Definition
    attr_accessor :arguments, :configurator, :klass_name, :method_calls

    def initialize(klass_name, container)
      @container        = container
      self.arguments    = []
      self.klass_name   = klass_name
      self.method_calls = {}
    end

    def add_argument(argument)
      self.add_arguments(argument)
    end

    def add_arguments(*arguments)
      self.arguments += arguments

      self
    end

    def add_configurator(name, method_name)
      self.configurator = [name, method_name]

      self
    end

    def add_method_call(method_name, *arguments)
      self.method_calls[method_name] = arguments

      self
    end

    def klass
      self.klass_name.constantize
    end

    def object
      return @object if @object

      @object = self.klass.new(*resolve(self.arguments))
      self.method_calls.each { |method_name, arguments| @object.send(method_name, *resolve(arguments)) }
      if self.configurator
        name, method_name   = self.configurator
        configurator_object = resolve([name]).first
        configurator_object.send(method_name, @object)
      end

      @object
    end

  private

    def resolve(arguments)
      resolve_references(resolve_container_parameters(arguments))
    end

    def resolve_container_parameters(arguments)
      arguments.map do |argument|
        if /^%(?<parameter_name>.*)%$/ =~ argument
          @container.parameters[parameter_name]
        else
          argument
        end
      end
    end

    def resolve_references(arguments)
      arguments.map do |argument|
        if /^@(?<reference_name>.*)/ =~ argument
          @container.get(reference_name)
        else
          argument
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dependency_injection-0.2.0 lib/dependency_injection/definition.rb