class Synapse::Configuration::DefinitionBuilder

DSL for building service definitions

Attributes

id[R]

@return [Symbol]

Public Class Methods

build(container, id = nil, &block) click to toggle source

@yield [DefinitionBuilder] @param [Container] container @param [Symbol] id @param [Proc] block @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 13
def self.build(container, id = nil, &block)
  builder = new container, id
  builder.instance_exec &block if block
  builder.register_definition

  builder.id
end
new(container, id = nil) click to toggle source

@param [Container] container @param [Symbol] id @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 24
def initialize(container, id = nil)
  @container = container
  @prototype = false
  @tags = Set.new

  populate_defaults

  @id = id.to_sym if id
end

Public Instance Methods

anonymous() click to toggle source

@api public @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 43
def anonymous
  identified_by SecureRandom.hex 8
end
as_prototype() click to toggle source

@api public @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 70
def as_prototype
  @prototype = true
end
as_singleton() click to toggle source

@api public @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 76
def as_singleton
  @prototype = false
end
build_composite(builder_type = DefinitionBuilder, &block) click to toggle source

Convenience method for building composite services

The given block will be executed in the context of the definition builder

@api public @param [Class] builder_type Defaults to DefinitionBuilder @param [Proc] block @return [Symbol] The identifier of the newly created service

# File lib/synapse/configuration/definition_builder.rb, line 104
def build_composite(builder_type = DefinitionBuilder, &block)
  builder_type.build @container, &block
end
build_definition() click to toggle source

@return [Definition]

# File lib/synapse/configuration/definition_builder.rb, line 109
def build_definition
  Definition.new @tags, @prototype, @factory, @instance
end
clear_tags() click to toggle source

@api public @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 49
def clear_tags
  @tags = Set.new
end
identified_by(id) click to toggle source

@api public @param [Symbol] id @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 37
def identified_by(id)
  @id = id.to_sym
end
register_definition() click to toggle source

@return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 114
def register_definition
  unless @id
    raise ConfigurationError, 'No identifier set for the definition'
  end

  @container.register @id, build_definition
end
replace_tags(*tags) click to toggle source

@api public @param [Symbol…] tags @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 56
def replace_tags(*tags)
  clear_tags
  tag *tags
end
tag(*tags) click to toggle source

@api public @param [Symbol…] tags @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 64
def tag(*tags)
  @tags.merge tags.flatten
end
use_factory(&factory) click to toggle source

@api public @param [Proc] factory @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 83
def use_factory(&factory)
  @factory = proc do
    instance_exec(&factory)
  end
end
use_instance(instance) click to toggle source

@api public @param [Object] instance @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 92
def use_instance(instance)
  @instance = instance
end

Protected Instance Methods

inject_into(object) click to toggle source

Injects any configured dependencies into the given object

@api public @param [Dependent] object @return [Dependent]

# File lib/synapse/configuration/definition_builder.rb, line 133
def inject_into(object)
  @container.inject_into object
  object
end
populate_defaults() click to toggle source

Sets the default values for the definition being built @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 126
def populate_defaults; end
resolve(value, optional = false) click to toggle source

If the given value is a symbol, it will be resolved using the container. Otherwise, it will be passed through

@api public @raise [ConfigurationError] If value is required but was not set @param [Object] value @return [Object]

# File lib/synapse/configuration/definition_builder.rb, line 145
def resolve(value, optional = false)
  if value.is_a? Symbol
    @container.resolve value, optional
  elsif value.nil? and not optional
    raise ConfigurationError, 'Value was not set'
  else
    value
  end
end
resolve_tagged(tag) click to toggle source

Resolves all services that have the given tag

@api public @param [Symbol] tag @return [Array]

# File lib/synapse/configuration/definition_builder.rb, line 160
def resolve_tagged(tag)
  @container.resolve_tagged tag
end
with_tagged(tag, &block) click to toggle source

Resolves any services with the given tag and yields them

@api public @yield [Object] @param [Symbol] tag @return [undefined]

# File lib/synapse/configuration/definition_builder.rb, line 170
def with_tagged(tag, &block)
  tagged = resolve_tagged tag
  tagged.each &block
end