Class: Kind::Result::Monad

Inherits:
Object
  • Object
show all
Includes:
Abstract
Defined in:
lib/kind/result/monad.rb

Direct Known Subclasses

Failure, Success

Defined Under Namespace

Classes: Wrapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Abstract

#failed?, #failure?, #result?, #succeeded?, #success?, #to_ary

Constructor Details

#initialize(type, value) ⇒ Monad

Returns a new instance of Monad.



26
27
28
29
# File 'lib/kind/result/monad.rb', line 26

def initialize(type, value)
  @type = type
  @value = value
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/kind/result/monad.rb', line 10

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/kind/result/monad.rb', line 10

def value
  @value
end

Class Method Details

.[](arg1 = UNDEFINED, arg2 = UNDEFINED, opt = Empty::HASH) ⇒ Object

:nodoc:



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kind/result/monad.rb', line 12

def self.[](arg1 = UNDEFINED, arg2 = UNDEFINED, opt = Empty::HASH) # :nodoc:
  value_must_be_a = opt[:value_must_be_a]

  type = UNDEFINED == arg2 ? self::DEFAULT_TYPE : STRICT.kind_of(::Symbol, arg1)

  Error.wrong_number_of_args!(given: 0, expected: '1 or 2') if UNDEFINED == arg1

  value = UNDEFINED == arg2 ? arg1 : arg2

  new(type, (value_must_be_a ? STRICT.kind_of(value_must_be_a, value) : value))
end

Instance Method Details

#===(m) ⇒ Object



67
68
69
70
71
72
# File 'lib/kind/result/monad.rb', line 67

def ===(m)
  return false unless Result::Abstract === m
  return false unless (self.success? && m.success?) || (self.failure? && m.failure?)

  self.type == m.type && self.value === m.value
end

#map(_ = UNDEFINED, &_fn) ⇒ Object Also known as: |, >>, map!, then, then!, and_then, and_then!

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/kind/result/monad.rb', line 35

def map(_ = UNDEFINED, &_fn)
  raise NotImplementedError
end

#on {|monad| ... } ⇒ Object

Yields:

  • (monad)


47
48
49
50
51
52
53
# File 'lib/kind/result/monad.rb', line 47

def on
  monad = Wrapper.new(self)

  yield(monad)

  monad.output
end

#on_failure(types = Undefined, matcher = Undefined) {|value| ... } ⇒ Object

Yields:



61
62
63
64
65
# File 'lib/kind/result/monad.rb', line 61

def on_failure(types = Undefined, matcher = Undefined)
  yield(value) if failure? && result?(types, matcher)

  self
end

#on_success(types = Undefined, matcher = Undefined) {|value| ... } ⇒ Object

Yields:



55
56
57
58
59
# File 'lib/kind/result/monad.rb', line 55

def on_success(types = Undefined, matcher = Undefined)
  yield(value) if success? && result?(types, matcher)

  self
end

#value_or(_method_name = UNDEFINED, &block) ⇒ Object

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/kind/result/monad.rb', line 31

def value_or(_method_name = UNDEFINED, &block)
  raise NotImplementedError
end