Class: Realm::Container
- Inherits:
-
Object
- Object
- Realm::Container
- Includes:
- Dry::Container::Mixin, Enumerable
- Defined in:
- lib/realm/container.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #create(klass, *args, **kwargs) ⇒ Object
-
#initialize(hash = {}) ⇒ Container
constructor
A new instance of Container.
- #register(key, contents = nil, options = {}, &block) ⇒ Object
- #register_all(hash) ⇒ Object
-
#register_factory(klass, *args, as: nil, memoize: true, **kwargs) ⇒ Object
rubocop:disable Naming/MethodParameterName.
- #resolve_all(klass) ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Container
Returns a new instance of Container.
14 15 16 |
# File 'lib/realm/container.rb', line 14 def initialize(hash = {}) register_all(hash) end |
Class Method Details
Instance Method Details
#[](key) ⇒ Object
43 44 45 |
# File 'lib/realm/container.rb', line 43 def [](key) resolve(key) if key?(key) end |
#create(klass, *args, **kwargs) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/realm/container.rb', line 35 def create(klass, *args, **kwargs) (klass.try(:dependencies) || []).each do |d| fn = -> { resolve_dependable(sanitize_dependable(d.dependable), d.optional?) } kwargs[d.name] = d.lazy? ? fn : fn.call end klass.new(*args, **kwargs) end |
#register(key, contents = nil, options = {}, &block) ⇒ Object
18 19 20 21 |
# File 'lib/realm/container.rb', line 18 def register(key, contents = nil, = {}, &block) [:klass] ||= contents.class if contents && !contents.is_a?(::Hash) super(key, contents, , &block) end |
#register_all(hash) ⇒ Object
23 24 25 26 27 |
# File 'lib/realm/container.rb', line 23 def register_all(hash) hash.each_pair do |key, value| register(key, value) end end |
#register_factory(klass, *args, as: nil, memoize: true, **kwargs) ⇒ Object
rubocop:disable Naming/MethodParameterName
29 30 31 32 33 |
# File 'lib/realm/container.rb', line 29 def register_factory(klass, *args, as: nil, memoize: true, **kwargs) # rubocop:disable Naming/MethodParameterName register(as || klass, klass: klass, memoize: memoize) do create(klass, *args, **kwargs) end end |
#resolve_all(klass) ⇒ Object
47 48 49 50 51 |
# File 'lib/realm/container.rb', line 47 def resolve_all(klass) _container.each_with_object([]) do |(_, item), all| all << item.call if item.[:klass] <= klass end end |