Module: SmartIoC::Iocify::ClassMethods
- Defined in:
- lib/smart_ioc/iocify.rb
Instance Method Summary collapse
- #after_init(name) ⇒ Object
-
#bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil, file_path: nil) ⇒ Object
Nil.
- #context(name) ⇒ Object
- #factory_method(name) ⇒ Object
- #inject(bean_name, ref: nil, from: nil) ⇒ Object
- #instance ⇒ Object
- #package(name) ⇒ Object
-
#register_inject(bean_name, ref: nil, from: nil) ⇒ Object
Nil.
- #scope(name) ⇒ Object
Instance Method Details
#after_init(name) ⇒ Object
54 55 56 57 |
# File 'lib/smart_ioc/iocify.rb', line 54 def after_init(name) raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol) @after_init = name end |
#bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil, file_path: nil) ⇒ Object
Returns nil.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/smart_ioc/iocify.rb', line 67 def bean(bean_name, scope: nil, package: nil, instance: true, factory_method: nil, context: nil, after_init: nil, file_path: nil) file_path ||= caller[0].split(':').first package ||= SmartIoC::BeanLocations.get_bean_package(file_path) context ||= SmartIoC::Container::DEFAULT_CONTEXT bean_definition = SmartIoC.find_bean_definition(bean_name, package, context) if bean_definition if bean_definition.path == file_path # seems that file with bean definition was reloaded # lets clear all scopes so we do not have SmartIoC::Container.get_instance.unregister_bean(bean_definition) SmartIoC::Container.get_instance.force_clear_scopes else raise ArgumentError, "bean with for class #{self.to_s} was already defined in #{bean_definition.path}" end end bean_definition = SmartIoC.register_bean( bean_name: bean_name, klass: self, scope: scope, path: file_path, package_name: package, instance: instance, factory_method: factory_method, context: context, after_init: after_init, ) if bean_definition.is_instance? class_eval %Q( def initialize raise ArgumentError, "constructor based allocation is not allowed for beans. Use ioc container to allocate bean." end ) end self.instance_variable_set(:@bean_definition, bean_definition) nil end |
#context(name) ⇒ Object
35 36 37 38 |
# File 'lib/smart_ioc/iocify.rb', line 35 def context(name) raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol) @context = name end |
#factory_method(name) ⇒ Object
40 41 42 43 |
# File 'lib/smart_ioc/iocify.rb', line 40 def factory_method(name) raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol) @factory_method = name end |
#inject(bean_name, ref: nil, from: nil) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/smart_ioc/iocify.rb', line 109 def inject(bean_name, ref: nil, from: nil) if @anonymous_bean @injects ||= [] @injects.push({bean_name: bean_name, ref: ref, from: from}) else register_inject(bean_name, ref: ref, from: from) end end |
#instance ⇒ Object
50 51 52 |
# File 'lib/smart_ioc/iocify.rb', line 50 def instance @instance = true end |
#package(name) ⇒ Object
30 31 32 33 |
# File 'lib/smart_ioc/iocify.rb', line 30 def package(name) raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol) @package = name end |
#register_inject(bean_name, ref: nil, from: nil) ⇒ Object
Returns nil.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/smart_ioc/iocify.rb', line 126 def register_inject(bean_name, ref: nil, from: nil) if !@bean_definition raise ArgumentError, "#{self.to_s} is not registered as bean. Add `bean :bean_name` declaration" end bd = @bean_definition bd.add_dependency( bean_name: bean_name, ref: ref, package: from ) bean_method = Proc.new do bean = instance_variable_get(:"@#{bean_name}") return bean if bean klass = self.is_a?(Class) ? self : self.class bean = SmartIoC::Container.get_instance.get_bean( ref || bean_name, package: from, parent_bean_definition: bd, parent_bean_name: bd.name, ) instance_variable_set(:"@#{bean_name}", bean) end if bd.is_instance? define_method bean_name, &bean_method private bean_name else define_singleton_method bean_name, &bean_method class_eval %Q( class << self private :#{bean_name} end ) end nil end |
#scope(name) ⇒ Object
45 46 47 48 |
# File 'lib/smart_ioc/iocify.rb', line 45 def scope(name) raise ArgumentError, "name should be a Symbol" if !name.is_a?(Symbol) @scope = name end |