Module: Dry::Mutations::Extensions::Either

Includes:
Dry::Monads::Either::Mixin
Defined in:
lib/dry/mutations/extensions/outcome.rb

Overview

:nodoc:

Defined Under Namespace

Classes: EitherCalculator, Matcher

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/dry/mutations/extensions/outcome.rb', line 44

def self.prepended base
  λ = base.instance_methods.method(:include?)
  base.instance_variable_set(:@∨, {
    left:    [:errors, :left].detect(&λ),
    right:   [:result, :output, :right].detect(&λ),
    success: [:success?].detect(&λ)
  }.reject { |_, v| v.nil? }.merge(base.instance_variable_get(:@∨) || {}))
  fail ArgumentError, "Can not have #{self} #{__callee__} to #{base}: base class must look like an either." unless base.instance_variable_get(:@∨).size == 3
end

Instance Method Details

#eitherObject



55
56
57
# File 'lib/dry/mutations/extensions/outcome.rb', line 55

def either
  @either ||= EitherCalculator.new(self).either
end

#left?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/dry/mutations/extensions/outcome.rb', line 63

def left?
  either.is_a?(Left)
end

#matchObject



71
72
73
74
# File 'lib/dry/mutations/extensions/outcome.rb', line 71

def match
  fail "Call to #{self.class.name}#match requires a block passed." unless block_given?
  Matcher.!.(self, &Proc.new)
end

#right?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/dry/mutations/extensions/outcome.rb', line 59

def right?
  either.is_a?(Right)
end

#valueObject



67
68
69
# File 'lib/dry/mutations/extensions/outcome.rb', line 67

def value
  either.value
end