Class: BetterService::Workflowable::Context
- Inherits:
-
Object
- Object
- BetterService::Workflowable::Context
- Defined in:
- lib/better_service/concerns/workflowable/context.rb
Overview
Context - Container for shared data between workflow steps
The Context object holds all data that flows through the workflow pipeline. Each step can read from and write to the context. The context also tracks the success/failure state of the workflow.
Example:
context = Context.new(user: current_user, cart_items: [...])
context.order = Order.create!(...)
context.success? # => true
context.fail!("Payment failed", payment_error: "Card declined")
context.success? # => false
context.errors # => { payment_error: "Card declined" }
Instance Attribute Summary collapse
-
#_called ⇒ Object
readonly
Returns the value of attribute _called.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#add(key, value) ⇒ Object
Add data to context.
-
#called! ⇒ Object
Mark workflow as called (used internally).
-
#called? ⇒ Boolean
Check if workflow has been called.
-
#fail!(message, **errors) ⇒ Object
Mark workflow as failed with error message and optional error details.
-
#failure? ⇒ Boolean
Check if workflow has failed.
-
#get(key) ⇒ Object
Get data from context.
-
#initialize(user, **initial_data) ⇒ Context
constructor
A new instance of Context.
-
#inspect ⇒ Object
Inspect for debugging.
-
#method_missing(method_name, *args) ⇒ Object
Allow reading context data via method calls Example: context.order instead of context.get(:order).
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#success? ⇒ Boolean
Check if workflow has succeeded (no failure called).
-
#to_h ⇒ Object
Return all context data as hash.
Constructor Details
#initialize(user, **initial_data) ⇒ Context
Returns a new instance of Context.
22 23 24 25 26 27 28 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 22 def initialize(user, **initial_data) @user = user @data = initial_data @errors = {} @failed = false @_called = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Allow reading context data via method calls Example: context.order instead of context.get(:order)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 77 def method_missing(method_name, *args) method_str = method_name.to_s if method_str.end_with?("=") # Setter: context.order = value key = method_str.chomp("=").to_sym @data[key] = args.first elsif @data.key?(method_name) # Getter: context.order @data[method_name] else super end end |
Instance Attribute Details
#_called ⇒ Object (readonly)
Returns the value of attribute _called.
20 21 22 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 20 def _called @_called end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
20 21 22 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 20 def errors @errors end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
20 21 22 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 20 def user @user end |
Instance Method Details
#add(key, value) ⇒ Object
Add data to context
64 65 66 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 64 def add(key, value) @data[key] = value end |
#called! ⇒ Object
Mark workflow as called (used internally)
51 52 53 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 51 def called! @_called = true end |
#called? ⇒ Boolean
Check if workflow has been called
56 57 58 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 56 def called? @_called end |
#fail!(message, **errors) ⇒ Object
Mark workflow as failed with error message and optional error details
44 45 46 47 48 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 44 def fail!(, **errors) @failed = true @errors[:message] = @errors.merge!(errors) if errors.any? end |
#failure? ⇒ Boolean
Check if workflow has failed
36 37 38 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 36 def failure? @failed end |
#get(key) ⇒ Object
Get data from context
71 72 73 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 71 def get(key) @data[key] end |
#inspect ⇒ Object
Inspect for debugging
103 104 105 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 103 def inspect "#<BetterService::Workflowable::Context success=#{success?} data=#{@data.inspect} errors=#{@errors.inspect}>" end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
92 93 94 95 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 92 def respond_to_missing?(method_name, include_private = false) method_str = method_name.to_s method_str.end_with?("=") || @data.key?(method_name) || super end |
#success? ⇒ Boolean
Check if workflow has succeeded (no failure called)
31 32 33 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 31 def success? !@failed end |
#to_h ⇒ Object
Return all context data as hash
98 99 100 |
# File 'lib/better_service/concerns/workflowable/context.rb', line 98 def to_h @data.dup end |