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.


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

def initialize(value)
  @value = value
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#copy(changes = {})


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

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

#defined?Boolean

Returns true.

Returns:

  • (Boolean)

    true


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

def defined?
  true
end

#explainSuccess

Returns:


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

def explain
  self
end

#fetch(default = nil)


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

def fetch(default = nil)
  @value
end

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

Yield Parameters:

  • value

Yield Returns:

Returns:


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

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)

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

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

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

Yield Parameters:

  • value

Yield Returns:

  • value

Returns:


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

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

#orSuccess

Returns:


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

def or
  self
end

#pretty_print(q)

This method returns an undefined value.


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

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:


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

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:


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

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

#tap(&block)


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

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