class Ruice::Container

Attributes

env[R]

Public Class Methods

new(properties = {}, env = 'default') click to toggle source
# File lib/ruice/container.rb, line 21
def initialize(properties = {}, env = 'default')
  raise ArgumentError, 'Container properties can not be nil' if properties.nil?
  raise ArgumentError, 'Container properties is not a Hash' unless properties.is_a? Hash
  raise ArgumentError, 'Environment can not be nil' if env.nil?
  raise ArgumentError, 'Environment must be a string' unless env.is_a? String

  properties[:env] = env

  @properties = properties
  @env = env.to_sym

  @bindings = {}
  @instances = {}
end

Public Instance Methods

lookup_property(name, default = nil) click to toggle source
# File lib/ruice/container.rb, line 38
def lookup_property(name, default = nil)
  path = name.split '.'
  current = @properties
  path.each do |key_part|
    break if current.nil?

    raise Exception, 'Can not access value subkey for non-hash ' + current unless current.is_a? Hash

    sym_part = key_part.to_sym

    current = current.fetch(sym_part, nil) || current.fetch(key_part, nil)
  end

  current || default
end
request(name) click to toggle source
# File lib/ruice/container.rb, line 54
def request(name)
  return self if name == Ruice::Container

  return @instances[name] if @instances.key? name

  instance = request_new name

  @instances[name] = instance

  instance
end
request_new(name) click to toggle source
# File lib/ruice/container.rb, line 66
def request_new(name)
  return self if name == Ruice::Container

  return @bindings[name].call self if @bindings.key? name

  raise ArgumentError, 'Dependency name is not class, and no bindings are present' unless name.respond_to? :new

  instance = name.new
  vars = instance.instance_variables

  vars.each do |it|
    value = instance.instance_variable_get it

    next unless value.is_a?(Dependency) || value.is_a?(Property)

    replacement = nil
    replacement = lookup_property value.name, value.default if value.is_a? Property

    if value.is_a? Dependency
      replacement = if value.is_fresh
                      request_new value.target
                    else
                      request value.target
                    end
    end

    instance.instance_variable_set it, replacement
  end

  instance.dic_ready if instance.methods.include? :dic_ready

  instance
end
with(name, subject) click to toggle source
# File lib/ruice/container.rb, line 100
def with(name, subject)
  if subject.is_a? Proc

    raise ArgumentError, 'Duplicate provider - ' + name if @bindings.key? name

    @bindings[name] = subject
    return
  end

  raise ArgumentError, 'Duplicate instance - ' + name if @instances.key? name

  @instances[name] = subject

  self
end