Class: Service::Base::Context
- Inherits:
-
Object
- Object
- Service::Base::Context
- Defined in:
- lib/service/base.rb
Overview
Simple structure to hold the context of the service during its whole lifecycle.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#fail(context = {}) ⇒ Context
Marks the context as failed without raising an exception.
-
#fail!(context = {}) ⇒ Context
Marks the context as failed.
-
#failure? ⇒ Boolean
Returns
true
if the context is set as failed. -
#initialize(context = {}) ⇒ Context
constructor
A new instance of Context.
- #inspect_steps ⇒ Object
-
#success? ⇒ Boolean
Returns
true
if the context is set as successful (default). - #to_h ⇒ Object
Constructor Details
#initialize(context = {}) ⇒ Context
Returns a new instance of Context.
23 24 25 |
# File 'lib/service/base.rb', line 23 def initialize(context = {}) @store = context.symbolize_keys end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
84 85 86 87 |
# File 'lib/service/base.rb', line 84 def method_missing(method_name, *args, &block) return super if args.present? store[method_name] end |
Instance Method Details
#[](key) ⇒ Object
27 28 29 |
# File 'lib/service/base.rb', line 27 def [](key) store[key.to_sym] end |
#[]=(key, value) ⇒ Object
31 32 33 |
# File 'lib/service/base.rb', line 31 def []=(key, value) store[key.to_sym] = value end |
#fail(context = {}) ⇒ Context
Marks the context as failed without raising an exception.
66 67 68 69 70 |
# File 'lib/service/base.rb', line 66 def fail(context = {}) store.merge!(context.symbolize_keys) @failure = true self end |
#fail!(context = {}) ⇒ Context
Marks the context as failed.
56 57 58 59 |
# File 'lib/service/base.rb', line 56 def fail!(context = {}) self.fail(context) raise Failure, self end |
#failure? ⇒ Boolean
Returns true
if the context is set as failed
47 48 49 |
# File 'lib/service/base.rb', line 47 def failure? @failure || false end |
#inspect_steps ⇒ Object
72 73 74 |
# File 'lib/service/base.rb', line 72 def inspect_steps Service::StepsInspector.new(self).inspect end |
#success? ⇒ Boolean
Returns true
if the context is set as successful (default)
40 41 42 |
# File 'lib/service/base.rb', line 40 def success? !failure? end |
#to_h ⇒ Object
35 36 37 |
# File 'lib/service/base.rb', line 35 def to_h store.dup end |