Class: Rwc::Core::Context
- Inherits:
-
Object
- Object
- Rwc::Core::Context
- Defined in:
- lib/rwc/core/context.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
Instance Method Summary collapse
-
#fail!(error:) ⇒ Context
Marks the context as failed, recording the provided error.
-
#initialize ⇒ void
constructor
Initializes a new Context instance.
-
#message ⇒ String
Returns a message string, combining messages or errors based on success state.
-
#payload!(payload:) ⇒ Context
Sets the payload and clears any errors.
-
#succeed(payload = nil) ⇒ Context
Marks the context as successful, optionally providing a payload.
-
#success? ⇒ Boolean
Checks if the operation was successful.
Constructor Details
#initialize ⇒ void
Initializes a new Context instance.
13 14 15 16 17 |
# File 'lib/rwc/core/context.rb', line 13 def initialize @success = true @errors = [] @messages = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/rwc/core/context.rb', line 8 def errors @errors end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
8 9 10 |
# File 'lib/rwc/core/context.rb', line 8 def @messages end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
8 9 10 |
# File 'lib/rwc/core/context.rb', line 8 def payload @payload end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
8 9 10 |
# File 'lib/rwc/core/context.rb', line 8 def success @success end |
Instance Method Details
#fail!(error:) ⇒ Context
Marks the context as failed, recording the provided error.
23 24 25 26 27 28 |
# File 'lib/rwc/core/context.rb', line 23 def fail!(error:) @success = false @errors = [error] self end |
#message ⇒ String
Returns a message string, combining messages or errors based on success state.
61 62 63 |
# File 'lib/rwc/core/context.rb', line 61 def success ? .join(", ") : errors.map(&:message).join(", ") end |
#payload!(payload:) ⇒ Context
Sets the payload and clears any errors.
46 47 48 49 |
# File 'lib/rwc/core/context.rb', line 46 def payload!(payload:) @errors = [] @payload = payload end |
#succeed(payload = nil) ⇒ Context
Marks the context as successful, optionally providing a payload.
34 35 36 37 38 39 40 |
# File 'lib/rwc/core/context.rb', line 34 def succeed(payload = nil) clear_errors @success = true @payload = payload self end |
#success? ⇒ Boolean
Checks if the operation was successful.
54 55 56 |
# File 'lib/rwc/core/context.rb', line 54 def success? @success end |