Sha256: 23c5b78dbe836689d50844e088302b7ded1ff3b3aad7af110741e5005d51dc04

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

require 'erb'
require 'yaml'

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

      def load(filename)
        file = 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.scope     = parameters['scope'] if parameters['scope']
        definition.file_path = parameters['file_path'] if parameters['file_path']
        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

      def load_file(filename)
        YAML::load(ERB.new(IO.read(filename)).result)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dependency_injection-0.4.3 lib/dependency_injection/loaders/yaml.rb
dependency_injection-0.4.2 lib/dependency_injection/loaders/yaml.rb
dependency_injection-0.4.1 lib/dependency_injection/loaders/yaml.rb
dependency_injection-0.4.0 lib/dependency_injection/loaders/yaml.rb