Module: ApiStruct::Extensions::DryMonads

Included in:
ApiStruct::Entity
Defined in:
lib/api_struct/extensions/dry_monads.rb

Instance Method Summary collapse

Instance Method Details

#from_left(error) ⇒ Object



13
14
15
# File 'lib/api_struct/extensions/dry_monads.rb', line 13

def from_left(error)
  ApiStruct::Errors::Entity.new({ status: error.status, body: error.body, error: true }, false)
end

#from_monad(monad) ⇒ Object



4
5
6
# File 'lib/api_struct/extensions/dry_monads.rb', line 4

def from_monad(monad)
  monad.fmap { |v| from_right(v) }.or_fmap { |e| from_left(e) }.value
end

#from_right(value) ⇒ Object



8
9
10
11
# File 'lib/api_struct/extensions/dry_monads.rb', line 8

def from_right(value)
  return Dry::Monads::Right(nil) if value.nil?
  value.is_a?(Array) ? collection(value) : new(value)
end