Class: FService::Result::Base Abstract
- Inherits:
-
Object
- Object
- FService::Result::Base
- Defined in:
- lib/f_service/result/base.rb
Overview
This class is abstract.
Abstract base class for Result::Success and Result::Failure.
Instance Attribute Summary collapse
- #types ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(types = []) ⇒ Base
constructor
You usually shouldn't call this directly.
-
#on_failure(*target_types, unhandled: false) {|value, type| ... } ⇒ Success, Failure
This hook runs if the result is failed.
-
#on_success(*target_types, unhandled: false) {|value, type| ... } ⇒ Success, Failure
This hook runs if the result is successful.
-
#to_ary ⇒ Array
Splits the result object into its components.
Constructor Details
#initialize(types = []) ⇒ Base
You usually shouldn't call this directly. See Base#Failure and Base#Success.
19 20 21 22 23 |
# File 'lib/f_service/result/base.rb', line 19 def initialize(types = []) @handled = false @types = types @matching_types = [] end |
Instance Attribute Details
#types ⇒ Object (readonly)
10 11 12 |
# File 'lib/f_service/result/base.rb', line 10 def types @types end |
Instance Method Details
#on_failure(*target_types, unhandled: false) {|value, type| ... } ⇒ Success, Failure
This hook runs if the result is failed. Can receive one or more types to be checked before running the given block.
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/f_service/result/base.rb', line 114 def on_failure(*target_types, unhandled: false) if failed? && unhandled? && expected_type?(target_types, unhandled: unhandled) match_types(target_types) yield(*to_ary) @handled = true freeze end self end |
#on_success(*target_types, unhandled: false) {|value, type| ... } ⇒ Success, Failure
This hook runs if the result is successful. Can receive one or more types to be checked before running the given block.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/f_service/result/base.rb', line 64 def on_success(*target_types, unhandled: false) if successful? && unhandled? && expected_type?(target_types, unhandled: unhandled) match_types(target_types) yield(*to_ary) @handled = true freeze end self end |
#to_ary ⇒ Array
Splits the result object into its components.
128 129 130 131 132 |
# File 'lib/f_service/result/base.rb', line 128 def to_ary data = successful? ? value : error [data, @matching_types.first] end |