Class: Stupidedi::Either::Success

Inherits:
Stupidedi::Either show all
Defined in:
lib/stupidedi/either.rb

Direct Known Subclasses

Reader::Success

Filtering the Value collapse

Transforming the Value collapse

Instance Method Summary collapse

Methods inherited from Stupidedi::Either

failure, success

Constructor Details

#initialize(value) ⇒ Success

Returns a new instance of Success.



46
47
48
# File 'lib/stupidedi/either.rb', line 46

def initialize(value)
  @value = value
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/stupidedi/either.rb', line 133

def ==(other)
  other.is_a?(Either) and other.select{|x| x == @value }.defined?
end

#copy(changes = {}) ⇒ Object



50
51
52
53
# File 'lib/stupidedi/either.rb', line 50

def copy(changes = {})
  Success.new \
    changes.fetch(:value, @value)
end

#defined?Boolean

Returns true.

Returns:

  • (Boolean)

    true



56
57
58
# File 'lib/stupidedi/either.rb', line 56

def defined?
  true
end

#explainSuccess

Returns:



121
122
123
# File 'lib/stupidedi/either.rb', line 121

def explain
  self
end

#fetch(default = nil) ⇒ Object



60
61
62
# File 'lib/stupidedi/either.rb', line 60

def fetch(default = nil)
  @value
end

#flatmap {|value| ... } ⇒ Either

Yield Parameters:

  • value

Yield Returns:

Returns:



105
106
107
108
109
110
111
112
113
# File 'lib/stupidedi/either.rb', line 105

def flatmap(&block)
  result = deconstruct(block)

  if result.is_a?(Either)
    result
  else
    raise TypeError, "block did not return an instance of Either"
  end
end

#inspectString

Returns:

  • (String)


147
148
149
# File 'lib/stupidedi/either.rb', line 147

def inspect
  "Either.success(#{@value.inspect})"
end

#map {|value| ... } ⇒ Success

Yield Parameters:

  • value

Yield Returns:

  • value

Returns:



98
99
100
# File 'lib/stupidedi/either.rb', line 98

def map(&block)
  copy(:value => deconstruct(block))
end

#orSuccess

Returns:



116
117
118
# File 'lib/stupidedi/either.rb', line 116

def or
  self
end

#pretty_print(q) ⇒ void

This method returns an undefined value.



138
139
140
141
142
143
144
# File 'lib/stupidedi/either.rb', line 138

def pretty_print(q)
  q.text("Either.success")
  q.group(2, "(", ")") do
    q.breakable ""
    q.pp @value
  end
end

#reject(reason = "reject") {|value| ... } ⇒ Either

Yield Parameters:

  • value

Yield Returns:

  • (Boolean)

Returns:



81
82
83
84
85
86
87
# File 'lib/stupidedi/either.rb', line 81

def reject(reason = "reject", &block)
  if deconstruct(block)
    failure(reason)
  else
    self
  end
end

#select(reason = "select") {|value| ... } ⇒ Either

Yield Parameters:

  • value

Yield Returns:

  • (Boolean)

Returns:



70
71
72
73
74
75
76
# File 'lib/stupidedi/either.rb', line 70

def select(reason = "select", &block)
  if deconstruct(block)
    self
  else
    failure(reason)
  end
end

#tap(&block) ⇒ Object



128
129
130
# File 'lib/stupidedi/either.rb', line 128

def tap(&block)
  deconstruct(block); self
end