Class: Navigable::Result

Inherits:
ImmutableStruct show all
Extended by:
ObserverInterface
Defined in:
lib/navigable/result.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*members) ⇒ Object



11
12
13
# File 'lib/navigable/result.rb', line 11

def new(*members)
  super(*members, :errors)
end

.on_failure(errors) ⇒ Object Also known as: on_failure_to_validate, on_failure_to_find, on_failure_to_create, on_failure_to_update, on_failure_to_delete



19
20
21
22
# File 'lib/navigable/result.rb', line 19

def on_failure(errors)
  values = members.to_h { |member| [member, nil] }
  new(**values.merge(errors: errors))
end

.on_success(*values) ⇒ Object



15
16
17
# File 'lib/navigable/result.rb', line 15

def on_success(*values)
  new(*values)
end

Instance Method Details

#and_then(&block) ⇒ Object



31
32
33
34
35
# File 'lib/navigable/result.rb', line 31

def and_then(&block)
  attributes = self.to_h.tap { |attributes| attributes.delete(:errors) }
  block.call(**attributes) if errors.none?
  self
end

#or_else(&block) ⇒ Object



37
38
39
40
# File 'lib/navigable/result.rb', line 37

def or_else(&block)
  block.call(errors) if errors.any?
  self
end