Class: Divergent::Success

Inherits:
Object
  • Object
show all
Includes:
Try
Defined in:
lib/divergent/try.rb

Overview

:nodoc: true

Instance Method Summary collapse

Methods included from Try

#get_or_else, #or_else, unit

Methods included from Monad

included

Constructor Details

#initialize(value) ⇒ Success

Returns a new instance of Success.



136
137
138
# File 'lib/divergent/try.rb', line 136

def initialize(value)
  @value = value
end

Instance Method Details

#each {|@value| ... } ⇒ Object

Yields:

  • (@value)


160
161
162
163
# File 'lib/divergent/try.rb', line 160

def each()
  yield(@value)
  nil
end

#failedObject



189
190
191
# File 'lib/divergent/try.rb', line 189

def failed
  Failure.new(UnSupportedOperationError.new('Success.failed'))
end

#failure?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/divergent/try.rb', line 152

def failure?
  false
end

#filterObject



171
172
173
174
175
176
177
178
179
# File 'lib/divergent/try.rb', line 171

def filter()
  fmap do |v|
    if yield(v)
      self
    else
      Failure.new(NoSuchElementError.new("Predicate does not hold for #{v}"))
    end
  end
end

#flattenObject



197
198
199
200
201
202
203
# File 'lib/divergent/try.rb', line 197

def flatten
  if @value.is_a? Try
    @value
  else
    self
  end
end

#fmapObject



140
141
142
143
144
145
146
# File 'lib/divergent/try.rb', line 140

def fmap()
  begin
    yield @value
  rescue => e
    Failure.new(e)
  end
end

#getObject



156
157
158
# File 'lib/divergent/try.rb', line 156

def get
  @value
end

#mapObject



165
166
167
168
169
# File 'lib/divergent/try.rb', line 165

def map()
  fmap do |v|
    Try.unit { yield v }
  end
end

#recover(*_errors, &block) ⇒ Object



185
186
187
# File 'lib/divergent/try.rb', line 185

def recover(*_errors, &block)
  self
end

#recover_with(&block) ⇒ Object



181
182
183
# File 'lib/divergent/try.rb', line 181

def recover_with(&block)
  self
end

#success?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/divergent/try.rb', line 148

def success?
  true
end

#to_sObject Also known as: inspect



205
206
207
# File 'lib/divergent/try.rb', line 205

def to_s
  "Success<#{@value}>"
end

#transform(s, _f) ⇒ Object



193
194
195
# File 'lib/divergent/try.rb', line 193

def transform(s, _f)
  fmap(&s)
end