Class: Philiprehberger::Try::Success
- Inherits:
-
Object
- Object
- Philiprehberger::Try::Success
- Defined in:
- lib/philiprehberger/try.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #deconstruct_keys(_keys) ⇒ Object
- #failure? ⇒ Boolean
- #filter ⇒ Object
- #flat_map ⇒ Object
-
#initialize(value) ⇒ Success
constructor
A new instance of Success.
- #map ⇒ Object
- #map_error ⇒ Object
- #on(_exception_class) ⇒ Object
- #on_error ⇒ Object
- #or_else(_default) ⇒ Object
- #or_try ⇒ Object
- #recover ⇒ Object
- #success? ⇒ Boolean
- #tap {|_self| ... } ⇒ Object
- #transform(on_success: nil, on_failure: nil) ⇒ Object
- #value! ⇒ Object
Constructor Details
#initialize(value) ⇒ Success
Returns a new instance of Success.
41 42 43 44 |
# File 'lib/philiprehberger/try.rb', line 41 def initialize(value) @value = value freeze end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
39 40 41 |
# File 'lib/philiprehberger/try.rb', line 39 def value @value end |
Instance Method Details
#deconstruct_keys(_keys) ⇒ Object
112 113 114 |
# File 'lib/philiprehberger/try.rb', line 112 def deconstruct_keys(_keys) { success: true, value: @value } end |
#failure? ⇒ Boolean
54 55 56 |
# File 'lib/philiprehberger/try.rb', line 54 def failure? false end |
#filter ⇒ Object
97 98 99 100 101 |
# File 'lib/philiprehberger/try.rb', line 97 def filter return Failure.new(ArgumentError.new('filter condition not met')) unless yield @value self end |
#flat_map ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/philiprehberger/try.rb', line 78 def flat_map result = yield @value raise TypeError, 'flat_map block must return Success or Failure' unless result.is_a?(Success) || result.is_a?(Failure) result rescue TypeError raise rescue StandardError => e Failure.new(e) end |
#map ⇒ Object
74 75 76 |
# File 'lib/philiprehberger/try.rb', line 74 def map Try.call { yield @value } end |
#map_error ⇒ Object
93 94 95 |
# File 'lib/philiprehberger/try.rb', line 93 def map_error self end |
#on(_exception_class) ⇒ Object
66 67 68 |
# File 'lib/philiprehberger/try.rb', line 66 def on(_exception_class) self end |
#on_error ⇒ Object
70 71 72 |
# File 'lib/philiprehberger/try.rb', line 70 def on_error self end |
#or_else(_default) ⇒ Object
58 59 60 |
# File 'lib/philiprehberger/try.rb', line 58 def or_else(_default) self end |
#or_try ⇒ Object
62 63 64 |
# File 'lib/philiprehberger/try.rb', line 62 def or_try self end |
#recover ⇒ Object
89 90 91 |
# File 'lib/philiprehberger/try.rb', line 89 def recover self end |
#success? ⇒ Boolean
50 51 52 |
# File 'lib/philiprehberger/try.rb', line 50 def success? true end |
#tap {|_self| ... } ⇒ Object
103 104 105 106 |
# File 'lib/philiprehberger/try.rb', line 103 def tap yield self self end |
#transform(on_success: nil, on_failure: nil) ⇒ Object
108 109 110 |
# File 'lib/philiprehberger/try.rb', line 108 def transform(on_success: nil, on_failure: nil) on_success ? Try.call { on_success.call(@value) } : self end |
#value! ⇒ Object
46 47 48 |
# File 'lib/philiprehberger/try.rb', line 46 def value! @value end |