class Hypo::Container

Attributes

components[R]
lifetimes[R]

Public Class Methods

new() click to toggle source
# File lib/hypo/container.rb, line 13
def initialize
  @components = {}
  @lifetimes = {}
  @mutex = Mutex.new

  add_lifetime(Lifetime::Transient.new, :transient)
  add_lifetime(Lifetime::Singleton.new, :singleton)
  add_lifetime(Lifetime::Scope.new, :scope)
  register_instance self, :container
end

Public Instance Methods

add_lifetime(lifetime, name) click to toggle source
# File lib/hypo/container.rb, line 63
def add_lifetime(lifetime, name)
  @lifetimes[name] = lifetime

  self
end
register(item, name = nil) click to toggle source
# File lib/hypo/container.rb, line 24
def register(item, name = nil)
  unless item.is_a?(Class)
    raise ContainerError, 'Using method "register" you can register only a type. ' \
      'If you wanna register an instance please use method "register_instance".'
  end

  component = Component.new(item, self, name)

  @mutex.synchronize do
    @components[component.name] = component
  end

  @components[component.name]
end
register_instance(item, name) click to toggle source
# File lib/hypo/container.rb, line 39
def register_instance(item, name)
  if %w(attrs attributes).include? name
    raise ContainerError, "Name \"#{name}\" is reserved by Hypo container please use another variant."
  end

  @mutex.synchronize do
    instance = Instance.new(item, self, name)
    @components[name] = instance
  end

  @components[name]
end
resolve(name, attrs = nil) click to toggle source
# File lib/hypo/container.rb, line 52
def resolve(name, attrs = nil)
  if [:attrs, :attributes].include? name
  else
    unless @components.key?(name)
      raise MissingComponentError.new(name)
    end

    @components[name].instance(attrs)
  end
end
show(component_name) click to toggle source
# File lib/hypo/container.rb, line 69
def show(component_name)
  @components[component_name]
end