Class: Zx::Result
- Inherits:
-
Object
- Object
- Zx::Result
- Defined in:
- lib/zx/result.rb
Defined Under Namespace
Classes: FailureError
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #check(&block) ⇒ Object
- #deconstruct ⇒ Object
- #deconstruct_keys(_) ⇒ Object
- #error ⇒ Object
- #failure!(value = nil, type: :error) ⇒ Object
- #failure? ⇒ Boolean
-
#initialize ⇒ Result
constructor
A new instance of Result.
- #on(ontype, tag = nil, &block) ⇒ Object (also: #>>, #|, #pipe)
- #on_failure(tag = nil, &block) ⇒ Object
- #on_success(tag = nil, &block) ⇒ Object
- #on_unknown(&block) ⇒ Object
- #success!(value = nil, type: :ok) ⇒ Object
- #success? ⇒ Boolean
- #then(&block) ⇒ Object (also: #and_then, #step, #fmap)
- #unwrap ⇒ Object
- #value! ⇒ Object
Constructor Details
#initialize ⇒ Result
Returns a new instance of Result.
7 8 9 10 11 12 |
# File 'lib/zx/result.rb', line 7 def initialize @value = nil @success = true @exception = false @type = nil end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/zx/result.rb', line 14 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
14 15 16 |
# File 'lib/zx/result.rb', line 14 def value @value end |
Instance Method Details
#check(&block) ⇒ Object
84 85 86 87 88 |
# File 'lib/zx/result.rb', line 84 def check(&block) return self if !!block.call(@value) failure! end |
#deconstruct ⇒ Object
36 37 38 |
# File 'lib/zx/result.rb', line 36 def deconstruct [type, value] end |
#deconstruct_keys(_) ⇒ Object
40 41 42 |
# File 'lib/zx/result.rb', line 40 def deconstruct_keys(_) { type: type, value: value, error: error } end |
#error ⇒ Object
16 17 18 |
# File 'lib/zx/result.rb', line 16 def error @value unless type == :ok end |
#failure!(value = nil, type: :error) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/zx/result.rb', line 90 def failure!(value = nil, type: :error) @type = type.to_sym @success = false @value = value self end |
#failure? ⇒ Boolean
24 25 26 |
# File 'lib/zx/result.rb', line 24 def failure? !success? end |
#on(ontype, tag = nil, &block) ⇒ Object Also known as: >>, |, pipe
66 67 68 69 70 71 72 |
# File 'lib/zx/result.rb', line 66 def on(ontype, tag = nil, &block) case ontype.to_sym when :success then on_success(tag, &block) when :failure then on_failure(tag, &block) when :unknown then on_unknown(tag, &block) end end |
#on_failure(tag = nil, &block) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/zx/result.rb', line 58 def on_failure(tag = nil, &block) return self if success? Reflect.apply(self, tag, &block) self end |
#on_success(tag = nil, &block) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/zx/result.rb', line 50 def on_success(tag = nil, &block) return self if failure? Reflect.apply(self, tag, &block) self end |
#on_unknown(&block) ⇒ Object
44 45 46 47 48 |
# File 'lib/zx/result.rb', line 44 def on_unknown(&block) Reflect.apply(self, nil, &block) self end |
#success!(value = nil, type: :ok) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/zx/result.rb', line 98 def success!(value = nil, type: :ok) @type = type.to_sym @success = true @value = value self end |
#success? ⇒ Boolean
20 21 22 |
# File 'lib/zx/result.rb', line 20 def success? !!@success end |
#then(&block) ⇒ Object Also known as: and_then, step, fmap
77 78 79 |
# File 'lib/zx/result.rb', line 77 def then(&block) Fmap.call(self, &block) end |
#unwrap ⇒ Object
32 33 34 |
# File 'lib/zx/result.rb', line 32 def unwrap @value end |
#value! ⇒ Object
28 29 30 |
# File 'lib/zx/result.rb', line 28 def value! @value || raise(FailureError) end |