Class: Servitium::Service
- Inherits:
-
Object
- Object
- Servitium::Service
- Includes:
- ActiveSupport::Callbacks, CaptureExceptionsMixin, I18n, TransactionalMixin
- Defined in:
- lib/servitium/service.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#raise_on_error ⇒ Object
readonly
Returns the value of attribute raise_on_error.
Class Method Summary collapse
- .after_async_failure(*filters) ⇒ Object
- .after_async_success(*filters) ⇒ Object
-
.after_commit(*filters) ⇒ Object
Callbacks.
- .after_failure(*filters) ⇒ Object
- .after_perform(*filters) ⇒ Object
- .around_async_failure(*filters) ⇒ Object
- .around_async_success(*filters) ⇒ Object
- .around_perform(*filters) ⇒ Object
- .before_perform(*filters) ⇒ Object
-
.call ⇒ Object
Call the service returning the service instance.
- .context ⇒ Object
-
.context_base_class_name ⇒ Object
Get the base class for new contexts defined using context blocks Defaults to Servitium::Context.
-
.context_base_class_name=(base_class) ⇒ Object
Override the base class for contexts defined using context blocks, you can use this to change the base class to your own ApplicationContext.
- .context_class ⇒ Object
- .context_class! ⇒ Object
- .context_class_name ⇒ Object
- .format_args(hash) ⇒ Object
- .format_array(array) ⇒ Object
- .initialized_context ⇒ Object
-
.perform ⇒ Object
Main point of entry for services.
-
.perform! ⇒ Object
Main point of entry for services, will raise in case of errors.
- .perform_async ⇒ Object
-
.perform_later ⇒ Object
Perform this service async.
- .perform_sync ⇒ Object
- .queue_name ⇒ Object
Instance Method Summary collapse
-
#ctx ⇒ Object
Returns the value of attribute context.
-
#initialize(*args) ⇒ Service
constructor
A new instance of Service.
Methods included from I18n
Methods included from CaptureExceptionsMixin
Methods included from TransactionalMixin
Constructor Details
#initialize(*args) ⇒ Service
Returns a new instance of Service.
28 29 30 31 32 33 |
# File 'lib/servitium/service.rb', line 28 def initialize(*args) @raise_on_error = false @command = args.first.is_a?(Symbol) ? args.shift : :perform @context = context_class.new(*args) super() end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
13 14 15 |
# File 'lib/servitium/service.rb', line 13 def context @context end |
#raise_on_error ⇒ Object (readonly)
Returns the value of attribute raise_on_error.
13 14 15 |
# File 'lib/servitium/service.rb', line 13 def raise_on_error @raise_on_error end |
Class Method Details
.after_async_failure(*filters) ⇒ Object
259 260 261 |
# File 'lib/servitium/service.rb', line 259 def after_async_failure(*filters, &) set_callback(:async_failure, :after, *filters, &) end |
.after_async_success(*filters) ⇒ Object
255 256 257 |
# File 'lib/servitium/service.rb', line 255 def after_async_success(*filters, &) set_callback(:async_success, :after, *filters, &) end |
.after_commit(*filters) ⇒ Object
Callbacks
227 228 229 |
# File 'lib/servitium/service.rb', line 227 def after_commit(*filters, &) set_callback(:commit, :after, *filters, &) end |
.after_failure(*filters) ⇒ Object
243 244 245 |
# File 'lib/servitium/service.rb', line 243 def after_failure(*filters, &) set_callback(:failure, :after, *filters, &) end |
.after_perform(*filters) ⇒ Object
239 240 241 |
# File 'lib/servitium/service.rb', line 239 def after_perform(*filters, &) set_callback(:perform, :after, *filters, &) end |
.around_async_failure(*filters) ⇒ Object
251 252 253 |
# File 'lib/servitium/service.rb', line 251 def around_async_failure(*filters, &) set_callback(:async_failure, :around, *filters, &) end |
.around_async_success(*filters) ⇒ Object
247 248 249 |
# File 'lib/servitium/service.rb', line 247 def around_async_success(*filters, &) set_callback(:async_success, :around, *filters, &) end |
.around_perform(*filters) ⇒ Object
235 236 237 |
# File 'lib/servitium/service.rb', line 235 def around_perform(*filters, &) set_callback(:perform, :around, *filters, &) end |
.before_perform(*filters) ⇒ Object
231 232 233 |
# File 'lib/servitium/service.rb', line 231 def before_perform(*filters, &) set_callback(:perform, :before, *filters, &) end |
.call ⇒ Object
Call the service returning the service instance
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/servitium/service.rb', line 156 def call(*) inst = new(*) valid_in = inst.context.valid? valid_in &&= inst.context.valid?(:in) if inst.context.class.inbound_scope_used if valid_in inst.context.instance_variable_set(:@called, true) inst.send(:call) inst.context.valid?(:out) if inst.context.errors.blank? && inst.context.class.inbound_scope_used inst.send(:after_commit) unless inst.context.failed? end inst.send(:failure) if inst.context.failed? inst end |
.context ⇒ Object
295 296 297 298 299 300 301 302 303 304 |
# File 'lib/servitium/service.rb', line 295 def context(*, &) return initialized_context(*) unless block_given? begin context_class!.new rescue nil end context_class!.class_eval(&) end |
.context_base_class_name ⇒ Object
Get the base class for new contexts defined using context blocks Defaults to Servitium::Context
285 286 287 |
# File 'lib/servitium/service.rb', line 285 def context_base_class_name @@_context_base_class_name ||= "Servitium::Context" end |
.context_base_class_name=(base_class) ⇒ Object
Override the base class for contexts defined using context blocks, you can use this to change the base class to your own ApplicationContext
291 292 293 |
# File 'lib/servitium/service.rb', line 291 def context_base_class_name=(base_class) @@_context_base_class_name = base_class end |
.context_class ⇒ Object
263 264 265 |
# File 'lib/servitium/service.rb', line 263 def context_class context_class_name.safe_constantize end |
.context_class! ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/servitium/service.rb', line 271 def context_class! return context_class if context_class context_class_parts = context_class_name.split("::") context_class_name_part = context_class_parts.pop context_module_name = context_class_parts.join("::") context_module = context_module_name.present? ? context_module_name.constantize : Object context_module.const_set(context_class_name_part, Class.new(context_base_class_name.constantize)) context_class end |
.context_class_name ⇒ Object
267 268 269 |
# File 'lib/servitium/service.rb', line 267 def context_class_name name.gsub("Service", "Context") end |
.format_args(hash) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/servitium/service.rb', line 192 def format_args(hash) hash.transform_values! do |v| case v when ActiveRecord::Base v.id when Hash format_args(v) when Array format_array(v) else v end end end |
.format_array(array) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/servitium/service.rb', line 207 def format_array(array) array.map do |ele| case ele when ActiveRecord::Base ele.id when Hash format_args(ele) when Array format_array(ele) else ele end end end |
.initialized_context ⇒ Object
306 307 308 |
# File 'lib/servitium/service.rb', line 306 def initialized_context(*) context_class.new(*) end |
.perform ⇒ Object
Main point of entry for services
151 152 153 |
# File 'lib/servitium/service.rb', line 151 def perform(*) call(*).context end |
.perform! ⇒ Object
Main point of entry for services, will raise in case of errors
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/servitium/service.rb', line 133 def perform!(*) inst = new(*) begin inst.context.validate!(:in) if inst.context.class.inbound_scope_used inst.context.validate! inst.context.instance_variable_set(:@called, true) inst.send(:call!) inst.context.validate!(:out) if inst.context.errors.blank? && inst.context.class.inbound_scope_used inst.send(:after_commit) unless inst.context.failed? ensure inst.send(:failure) if inst.context.failed? end inst.context end |
.perform_async ⇒ Object
124 125 126 |
# File 'lib/servitium/service.rb', line 124 def perform_async(*) perform_later(*) end |
.perform_later ⇒ Object
Perform this service async
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/servitium/service.rb', line 173 def perform_later(*) inst = new(*) valid_in = inst.context.valid? valid_in &&= inst.context.valid?(:in) if inst.context.class.inbound_scope_used if valid_in inst.context.instance_variable_set(:@called, true) if Servitium.config.bg_jobs_platform == :sidekiq formatted_args = JSON.load(JSON.dump(format_args(inst.context.attributes_hash))) Servitium::ServiceSidekiqJob.set(queue: name.constantize.queue_name).perform_async(name, formatted_args) else Servitium::ServiceActiveJob.set(queue: name.constantize.queue_name).perform_later(name, inst.context.attributes_hash) end end inst.context end |
.perform_sync ⇒ Object
128 129 130 |
# File 'lib/servitium/service.rb', line 128 def perform_sync(*) perform(*) end |
.queue_name ⇒ Object
222 223 224 |
# File 'lib/servitium/service.rb', line 222 def queue_name "default" end |
Instance Method Details
#ctx ⇒ Object
Returns the value of attribute context.
15 16 17 |
# File 'lib/servitium/service.rb', line 15 def context @context end |