Module: Monadic
- Defined in:
- lib/monadic/try.rb,
lib/monadic/maybe.rb,
lib/monadic/monad.rb,
lib/monadic/either.rb,
lib/monadic/errors.rb,
lib/monadic/version.rb,
lib/monadic/validation.rb
Defined Under Namespace
Modules: Monad, SuccessFailure Classes: Either, Failure, Just, Maybe, NoValueError, NotEitherError, Nothing, Success, Validation
Constant Summary collapse
- VERSION =
"0.7.4"
Instance Method Summary collapse
-
#Either(value) ⇒ Success, Failure
Coerces any value into either a
Success
or aFailure
. - #Failure(value) ⇒ Object
- #Maybe(value) ⇒ Object (also: #Just, #Nothing)
-
#Success(value) ⇒ Success
Factory method.
-
#Try(arg = nil, &block) ⇒ Object
Wrap a block or a predicate to always return Success or Failure.
-
#Validation(&block) ⇒ Object
Wraps the construction of the Validation class.
Instance Method Details
#Either(value) ⇒ Success, Failure
Coerces any value into either a Success
or a Failure
.
138 139 140 |
# File 'lib/monadic/either.rb', line 138 def Either(value) Either.unit(value) end |
#Failure(value) ⇒ Object
126 127 128 |
# File 'lib/monadic/either.rb', line 126 def Failure(value) Failure.new(value) end |
#Maybe(value) ⇒ Object Also known as: Just, Nothing
122 123 124 |
# File 'lib/monadic/maybe.rb', line 122 def Maybe(value) Maybe.unit(value) end |
#Success(value) ⇒ Success
Factory method
132 133 134 |
# File 'lib/monadic/either.rb', line 132 def Success(value) Success.new(value) end |
#Try(arg = nil, &block) ⇒ Object
Wrap a block or a predicate to always return Success or Failure. It will catch StandardError and return as a Failure.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/monadic/try.rb', line 4 def Try(arg = nil, &block) begin predicate = arg.is_a?(Proc) ? arg.call : arg return Either(block.call) if block_given? && predicate.nil? return Either(predicate).class.unit(block.call) if block_given? # use predicate for Success/Failure and block for value return Either(predicate) rescue => error Failure(error) end end |
#Validation(&block) ⇒ Object
Wraps the construction of the Validation class
3 4 5 |
# File 'lib/monadic/validation.rb', line 3 def Validation(&block) Validation.new.call(&block) end |