Sha256: 5e88ebda0b2e699c2cd287f0a605e0c142329b54b7deb37b58f52fdde30b65e6

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'yaml'

module DependencyInjection
  module Loaders
    class Yaml
      def initialize(container)
        @container = container
      end

      def load(filename)
        file = YAML::load_file(filename)
        add_parameters(file['parameters']) if file['parameters']
        add_services(file['services']) if file['services']
      end

    private

      def add_aliased_service(name, aliased_service_name)
        @container.register_alias(name, aliased_service_name)
      end

      def add_parameters(parameters)
        parameters.each { |name, value| @container.add_parameter(name, value) }
      end

      def add_services(services)
        services.each { |name, parameters| add_service(name, parameters) }
      end

      def add_service(name, parameters)
        if parameters['alias']
          add_aliased_service(name, parameters['alias'])
        else
          add_standard_service(name, parameters)
        end
      end

      def add_standard_service(name, parameters)
        lazy_load  = parameters['lazy'] || false
        definition = @container.register(name, parameters['class'], lazy_load)
        definition.add_arguments(*parameters['arguments']) if parameters['arguments']
        if (configurator = parameters['configurator'])
          definition.add_configurator(configurator[0], configurator[1])
        end
        if parameters['calls']
          parameters['calls'].each { |method_name, arguments| definition.add_method_call(method_name, *arguments) }
        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/loaders/yaml.rb