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.



44
45
46
# File 'lib/stupidedi/either.rb', line 44

def initialize(value)
  @value = value
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/stupidedi/either.rb', line 131

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

#copy(changes = {}) ⇒ Object



48
49
50
51
# File 'lib/stupidedi/either.rb', line 48

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

#defined?Boolean

Returns true.

Returns:

  • (Boolean)

    true



54
55
56
# File 'lib/stupidedi/either.rb', line 54

def defined?
  true
end

#explainSuccess

Returns:



119
120
121
# File 'lib/stupidedi/either.rb', line 119

def explain
  self
end

#fetch(default = nil) ⇒ Object



58
59
60
# File 'lib/stupidedi/either.rb', line 58

def fetch(default = nil)
  @value
end

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

Yield Parameters:

  • value

Yield Returns:

Returns:



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

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)


145
146
147
# File 'lib/stupidedi/either.rb', line 145

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

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

Yield Parameters:

  • value

Yield Returns:

  • value

Returns:



96
97
98
# File 'lib/stupidedi/either.rb', line 96

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

#orSuccess

Returns:



114
115
116
# File 'lib/stupidedi/either.rb', line 114

def or
  self
end

#pretty_print(q) ⇒ void

This method returns an undefined value.



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

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:



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

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:



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

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

#tap(&block) ⇒ Object



126
127
128
# File 'lib/stupidedi/either.rb', line 126

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