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

rubocop:disable Style/VariableNumber



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

def self.prepended base
  λ = base.instance_methods.method(:include?)
  base.instance_variable_set(:@∨, {
    left:    %i|errors left|.detect(&λ),
    right:   %i|result output right|.detect(&λ),
    success: %i|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



57
58
59
# File 'lib/dry/mutations/extensions/outcome.rb', line 57

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

#left?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/dry/mutations/extensions/outcome.rb', line 65

def left?
  either.is_a?(Left)
end

#matchObject



73
74
75
76
# File 'lib/dry/mutations/extensions/outcome.rb', line 73

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)


61
62
63
# File 'lib/dry/mutations/extensions/outcome.rb', line 61

def right?
  either.is_a?(Right)
end

#valueObject



69
70
71
# File 'lib/dry/mutations/extensions/outcome.rb', line 69

def value
  either.value
end