Class: SmartCore::Container

Inherits:
Object
  • Object
show all
Includes:
Enumerable, DefinitionDSL
Defined in:
lib/smart_core/container.rb,
lib/smart_core/container/errors.rb,
lib/smart_core/container/version.rb,
lib/smart_core/container/definition_dsl.rb

Overview

rubocop:disable Style/StaticClass

Since:

  • 0.1.0

Defined Under Namespace

Modules: DefinitionDSL, DependencyCompatability, DependencyResolver, Entities, KeyGuard, Mixin, RegistryBuilder Classes: DependencyWatcher, Host, Registry, ResolvingError

Constant Summary collapse

NO_HOST_CONTAINER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (NilClass)

Since:

  • 0.8.1

nil
NO_HOST_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (NilClass)

Since:

  • 0.8.1

nil
Error =

Since:

  • 0.1.0

Class.new(SmartCore::Error)
ArgumentError =

Since:

  • 0.1.0

Class.new(SmartCore::ArgumentError)
IncompatibleEntityNameError =

Since:

  • 0.1.0

Class.new(ArgumentError)
FrozenRegistryError =

See Also:

Since:

  • 0.1.0

Class.new(SmartCore::FrozenError)
FetchError =

Since:

  • 0.1.0

Class.new(Error)
DependencyOverNamespaceOverlapError =
Class.new(Error)
NamespaceOverDependencyOverlapError =
Class.new(Error)
VERSION =

Returns:

  • (String)

Since:

  • 0.1.0

Version:

  • 0.11.0

'0.11.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefinitionDSL

included

Constructor Details

#initialize(host_container: NO_HOST_CONTAINER, host_path: NO_HOST_PATH) ⇒ void

Parameters:

  • host_container (Hash) (defaults to: NO_HOST_CONTAINER)

    a customizable set of options

  • host_path (Hash) (defaults to: NO_HOST_PATH)

    a customizable set of options

Options Hash (host_container:):

Options Hash (host_path:):

  • (String, NilClass)

Since:

  • 0.1.0

Version:

  • 0.10.0



86
87
88
89
90
91
92
# File 'lib/smart_core/container.rb', line 86

def initialize(host_container: NO_HOST_CONTAINER, host_path: NO_HOST_PATH)
  @host = SmartCore::Container::Host.build(host_container, host_path)
  build_registry!
  @watcher = SmartCore::Container::DependencyWatcher.new(self)
  @host_path = host_path
  @lock = SmartCore::Engine::ReadWriteLock.new
end

Instance Attribute Details

#hostSmartCore::Container::Host (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 0.8.1



71
72
73
# File 'lib/smart_core/container.rb', line 71

def host
  @host
end

#registrySmartCore::Container::Registry (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 0.1.0



65
66
67
# File 'lib/smart_core/container.rb', line 65

def registry
  @registry
end

#watcherSmartCore::Container::DependencyWatcher (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 0.8.0



77
78
79
# File 'lib/smart_core/container.rb', line 77

def watcher
  @watcher
end

Class Method Details

.define(initial_container_klass = self, &container_definitions) ⇒ SmartCore::Container

Parameters:

  • initial_container_klass (Class<SmartCore::Container>) (defaults to: self)
  • container_definitions (Block)

Returns:

Since:

  • 0.7.0



32
33
34
35
36
37
38
39
40
# File 'lib/smart_core/container.rb', line 32

def define(initial_container_klass = self, &container_definitions)
  unless initial_container_klass <= SmartCore::Container
    raise(SmartCore::Container::ArgumentError, <<~ERROR_MESSAGE)
      Base class should be a type of SmartCore::Container
    ERROR_MESSAGE
  end

  Class.new(initial_container_klass, &container_definitions).new
end

Instance Method Details

#clear_observers(entity_path = nil) ⇒ void Also known as: clear_listeners

This method returns an undefined value.

Parameters:

  • entity_path (String, Symbol, NilClass) (defaults to: nil)

Since:

  • 0.8.0

Version:

  • 0.10.0



279
280
281
# File 'lib/smart_core/container.rb', line 279

def clear_observers(entity_path = nil) # TODO: support for pattern-based pathes
  @lock.write_sync { watcher.clear_listeners(entity_path) }
end

#dependency?(dependency_path, memoized: nil) ⇒ Boolean

Parameters:

  • dependency_path (String, Symbol)
  • memoized (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (memoized:):

  • (NilClass, Boolean)

Returns:

  • (Boolean)

Since:

  • 0.5.0

Version:

  • 0.10.0



212
213
214
# File 'lib/smart_core/container.rb', line 212

def dependency?(dependency_path, memoized: nil)
  @lock.read_sync { DependencyResolver.dependency?(self, dependency_path, memoized: memoized) }
end

#each_dependency(yield_all: SmartCore::Container::Registry::DEFAULT_ITERATION_YIELD_BEHAVIOUR, &block) {|dependency_name, dependency_value| ... } ⇒ Enumerable Also known as: each, each_pair

Parameters:

  • block (Block)
  • yield_all (Hash) (defaults to: SmartCore::Container::Registry::DEFAULT_ITERATION_YIELD_BEHAVIOUR)

    a customizable set of options

Options Hash (yield_all:):

  • (Boolean)

Yields:

  • (dependency_name, dependency_value)

Returns:

  • (Enumerable)

Since:

  • 0.4.0

Version:

  • 0.10.0



226
227
228
229
230
231
# File 'lib/smart_core/container.rb', line 226

def each_dependency(
  yield_all: SmartCore::Container::Registry::DEFAULT_ITERATION_YIELD_BEHAVIOUR,
  &block
)
  @lock.read_sync { registry.each_dependency(yield_all: yield_all, &block) }
end

#fetch(dependency_path) ⇒ Any

Parameters:

  • dependency_path (String, Symbol)

Returns:

  • (Any)

Since:

  • 0.1.0

Version:

  • 0.10.0



144
145
146
# File 'lib/smart_core/container.rb', line 144

def fetch(dependency_path)
  @lock.read_sync { DependencyResolver.fetch(self, dependency_path) }
end

#freeze!void

This method returns an undefined value.

Since:

  • 0.1.0

Version:

  • 0.10.0



153
154
155
# File 'lib/smart_core/container.rb', line 153

def freeze!
  @lock.write_sync { registry.freeze! }
end

#frozen?Boolean

Returns:

  • (Boolean)

Since:

  • 0.1.0

Version:

  • 0.10.0



162
163
164
# File 'lib/smart_core/container.rb', line 162

def frozen?
  @lock.read_sync { registry.frozen? }
end

#hash_tree(resolve_dependencies: false) ⇒ Hash<String|Symbol,SmartCore::Container::Entities::Base|Any> Also known as: to_h, to_hash

Parameters:

  • resolve_dependencies (Hash) (defaults to: false)

    a customizable set of options

Options Hash (resolve_dependencies:):

  • (Boolean)

Returns:

Since:

  • 0.1.0

Version:

  • 0.10.0



241
242
243
# File 'lib/smart_core/container.rb', line 241

def hash_tree(resolve_dependencies: false)
  @lock.read_sync { registry.hash_tree(resolve_dependencies: resolve_dependencies) }
end

#key?(key) ⇒ Boolean

Parameters:

  • key (String, Symbol)

Returns:

  • (Boolean)

Since:

  • 0.5.0

Version:

  • 0.10.0



191
192
193
# File 'lib/smart_core/container.rb', line 191

def key?(key)
  @lock.read_sync { DependencyResolver.key?(self, key) }
end

#keys(all_variants: SmartCore::Container::Registry::DEFAULT_KEY_EXTRACTION_BEHAVIOUR) ⇒ Array<String>

Parameters:

  • all_variants (Hash) (defaults to: SmartCore::Container::Registry::DEFAULT_KEY_EXTRACTION_BEHAVIOUR)

    a customizable set of options

Options Hash (all_variants:):

  • (Boolean)

Returns:

  • (Array<String>)

Since:

  • 0.4.0

Version:

  • 0.10.0



181
182
183
# File 'lib/smart_core/container.rb', line 181

def keys(all_variants: SmartCore::Container::Registry::DEFAULT_KEY_EXTRACTION_BEHAVIOUR)
  @lock.read_sync { registry.keys(all_variants: all_variants) }
end

#namespace(namespace_name, &dependencies_definition) ⇒ void

This method returns an undefined value.

Parameters:

  • namespace_name (String, Symbol)
  • dependencies_definition (Block)

Since:

  • 0.1.0

Version:

  • 0.10.0



120
121
122
123
124
125
# File 'lib/smart_core/container.rb', line 120

def namespace(namespace_name, &dependencies_definition)
  @lock.write_sync do
    registry.register_namespace(namespace_name, self, &dependencies_definition)
    watcher.notify(namespace_name)
  end
end

#namespace?(namespace_path) ⇒ Boolean

Parameters:

  • namespace_path (String, Symbol)

Returns:

  • (Boolean)

Since:

  • 0.5.0

Version:

  • 0.10.0



201
202
203
# File 'lib/smart_core/container.rb', line 201

def namespace?(namespace_path)
  @lock.read_sync { DependencyResolver.namespace?(self, namespace_path) }
end

#observe(entity_path, &observer) {|entity_path, container| ... } ⇒ SmartCore::Container::DependencyWatcher::Observer Also known as: subscribe

Parameters:

  • entity_path (String)
  • observer (Block)

Yields:

  • (entity_path, container)

Yield Parameters:

Returns:

Since:

  • 0.8.0

Version:

  • 0.10.0



257
258
259
# File 'lib/smart_core/container.rb', line 257

def observe(entity_path, &observer) # TODO: support for pattern-based pathes
  @lock.write_sync { watcher.watch(entity_path, &observer) }
end

#register(dependency_name, memoize: SmartCore::Container::Registry::DEFAULT_MEMOIZATION_BEHAVIOR, &dependency_definition) ⇒ void

This method returns an undefined value.

Parameters:

  • dependency_name (String, Symbol)
  • dependency_definition (Block)

Since:

  • 0.1.0

Version:

  • 0.10.0



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/smart_core/container.rb', line 101

def register(
  dependency_name,
  memoize: SmartCore::Container::Registry::DEFAULT_MEMOIZATION_BEHAVIOR,
  &dependency_definition
)

  @lock.write_sync do
    registry.register_dependency(dependency_name, memoize: memoize, &dependency_definition)
    watcher.notify(dependency_name)
  end
end

#reload!void

This method returns an undefined value.

Since:

  • 0.1.0

Version:

  • 0.10.0



171
172
173
# File 'lib/smart_core/container.rb', line 171

def reload!
  @lock.write_sync { build_registry! }
end

#resolve(dependency_path) ⇒ Any Also known as: []

Parameters:

  • dependency_path (String, Symbol)

Returns:

  • (Any)

Since:

  • 0.1.0

Version:

  • 0.10.0



133
134
135
# File 'lib/smart_core/container.rb', line 133

def resolve(dependency_path)
  @lock.read_sync { DependencyResolver.resolve(self, dependency_path) }
end

#unobserve(observer) ⇒ Boolean Also known as: unsubscribe

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.8.0

Version:

  • 0.10.0



268
269
270
# File 'lib/smart_core/container.rb', line 268

def unobserve(observer)
  @lock.write_sync { watcher.unwatch(observer) }
end