Class: SmartInject
- Inherits:
-
Object
- Object
- SmartInject
- Defined in:
- lib/smart_inject.rb
Overview
SmartInject - Made DI deadly simple
SmartInject is a minimalist, flexible and efficient dependency injection for Ruby.
Here, no DSL, no complex machinery, no dynamic change on the object model. This is just a lightweight class that bring you DI using constructor, one of the most used method for DI. You can use it without constraint on your design.
SmartInject can be used in isolation within your Gem just by applying standard ruby code: extend a class from SmartInject, and this is your new DI container.
Feel free to see the code of SmartInject (very short), get the gem or copy-past the SmartInject class file into your project… There is no external dependency.
SmartInject is intented to be under your control. No magic here.
Defined Under Namespace
Classes: ContainerError, InjectionError, SmartInjectError
Class Method Summary collapse
- .[](klass) ⇒ Object
- .add(klass, &block) ⇒ Object
- .remap(looked_klass, injected_klass) ⇒ Object
- .services ⇒ Object
- .share_injections ⇒ Object
- .share_injections? ⇒ Boolean
- .start_container! ⇒ Object
Class Method Details
.[](klass) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/smart_inject.rb', line 34 def [](klass) klass_name = klass.to_s if @share_injections service = running[klass_name] return service if service end service = services.fetch(klass_name) { raise InjectionError, "service #{klass_name} unknown. Try to add #{name}.add(self) at the begining of #{klass_name} class." } try_instanciate(service).tap do |instance| running[klass_name] = instance if @share_injections end end |
.add(klass, &block) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/smart_inject.rb', line 57 def add(klass, &block) (raise ContainerError, "the #{name} container is already started. No add allowed") if frozen? (raise ContainerError, "first argument must be a class. Found: #{klass}") unless klass.instance_of?(Class) services[klass.name] = block || klass running.delete(klass.name) self end |
.remap(looked_klass, injected_klass) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/smart_inject.rb', line 48 def remap(looked_klass, injected_klass) (raise ContainerError, "the #{name} container is already started. No remap allowed") if frozen? (raise ContainerError, "first argument must be a class. Found: #{looked_klass}") unless looked_klass.instance_of?(Class) (raise ContainerError, "second argument must be a class. Found: #{injected_klass}") unless injected_klass.instance_of?(Class) services[looked_klass.name] = injected_klass self end |
.services ⇒ Object
30 31 32 |
# File 'lib/smart_inject.rb', line 30 def services @services ||= {} end |
.share_injections ⇒ Object
78 79 80 81 82 83 |
# File 'lib/smart_inject.rb', line 78 def share_injections (raise ContainerError, "the #{name} container is already started. No share_injections allowed") if frozen? @share_injections = true self end |
.share_injections? ⇒ Boolean
85 86 87 |
# File 'lib/smart_inject.rb', line 85 def share_injections? !!(@share_injections) end |
.start_container! ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/smart_inject.rb', line 66 def start_container! return self if frozen? return freeze if @share_injections services.each do |(klass_name, service)| next if running[klass_name] try_instanciate(service).tap { |instance| running[klass_name] = instance } end freeze end |