Module: Lab42::Result::ClassMethods
- Included in:
- Lab42::Result
- Defined in:
- lib/lab42/result/class_methods.rb
Instance Method Summary collapse
- #error(error, exception: RuntimeError) ⇒ Object
- #from_rescue(error_message = nil, &blk) ⇒ Object
- #new ⇒ Object
- #ok(value = nil) ⇒ Object
Instance Method Details
#error(error, exception: RuntimeError) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/lab42/result/class_methods.rb', line 10 def error(error, exception: RuntimeError) raise ArgumentError, "exception must be a subclass of StandardError, but was #{exception.inspect}" unless Exception.class === exception && exception < StandardError o = allocate o.instance_exec do @ok = false @error = error @exception = exception end o.freeze end |
#from_rescue(error_message = nil, &blk) ⇒ Object
22 23 24 25 26 |
# File 'lib/lab42/result/class_methods.rb', line 22 def from_rescue(=nil, &blk) ok(blk.()) rescue Exception => e error(||e., exception: e.class) end |
#new ⇒ Object
6 7 8 |
# File 'lib/lab42/result/class_methods.rb', line 6 def new(*,**) raise NoMethodError, "only use the constructors `.ok` and `.error`" end |
#ok(value = nil) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/lab42/result/class_methods.rb', line 28 def ok(value=nil) o = allocate o.instance_exec do @ok = true @value = value end o.freeze end |