module Results

Constants

Bad
Because
DEFAULT_EXCEPTIONS_TO_RESCUE_AS_BADS
DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS
Good
HeadCombiner

TODO: Extract this simple util somewhere else

VERSION

The current version of this gem

Public Class Methods

call_or_yield_or_return(proc_or_value, *args) { |proc_or_value| ... } click to toggle source

Helper which will call its argument, or yield it to a block, or simply return it,

depending on what is possible with the given input
# File lib/results.rb, line 228
def self.call_or_yield_or_return(proc_or_value, *args)
  if proc_or_value.respond_to?(:call)
    proc_or_value.call(*args)
  else
    block_given? ? yield(proc_or_value) : proc_or_value
  end
end
combine(*args) click to toggle source
# File lib/results.rb, line 57
def combine(*args)
  flat_args = (args.size == 1 && args.first.is_a?(Enumerable)) ? args.first : args
  raise ArgumentError, 'no results to combine' if flat_args.empty?
  flat_args.inject { |res, nxt| res.zip(nxt).map { |vs| HeadCombiner.new(*vs) } }.map { |hc| hc.elements }
end
from_rescuer(success_or_failure, input, exception_message_transforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS) click to toggle source
# File lib/results.rb, line 24
def from_rescuer(success_or_failure, input, exception_message_transforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS)
  success_or_failure.transform(
    lambda { |v| Good.new(v) },
    lambda { |e| Bad.new(transform_exception_message(e, exception_message_transforms), input) }
  ).get
end
new(input_or_proc) { |input| ... } click to toggle source
# File lib/results.rb, line 12
def new(input_or_proc)
  exceptions_as_bad = DEFAULT_EXCEPTIONS_TO_RESCUE_AS_BADS
  exceptions_xforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS

  rescued = Rescuer.new(*exceptions_as_bad) do
    call_or_yield_or_return(input_or_proc) { |input| block_given? ? yield(input) : input }
  end

  from_rescuer(rescued, input_or_proc, exceptions_xforms)
end
predicate(method_name) click to toggle source
# File lib/results.rb, line 64
def predicate(method_name)
  Filter.new(method_name.to_s.gsub(/\?\Z/, '')) { |v| v.send(method_name) }
end
transform_exception_message(exception, exception_message_transforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS) click to toggle source
# File lib/results.rb, line 32
def transform_exception_message(exception, exception_message_transforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS)
  _, f = exception_message_transforms.find { |klass, _| klass === exception }
  message = exception && exception.message
  f && f.call(message) || message
end
when(*args) click to toggle source
# File lib/results.rb, line 39
def when(*args)
  lambda { |v| Results.new(v).when(*args) }
end
when_not(*args) click to toggle source
# File lib/results.rb, line 44
def when_not(*args)
  lambda { |v| Results.new(v).when_not(*args) }
end

Private Instance Methods

combine(*args) click to toggle source
# File lib/results.rb, line 57
def combine(*args)
  flat_args = (args.size == 1 && args.first.is_a?(Enumerable)) ? args.first : args
  raise ArgumentError, 'no results to combine' if flat_args.empty?
  flat_args.inject { |res, nxt| res.zip(nxt).map { |vs| HeadCombiner.new(*vs) } }.map { |hc| hc.elements }
end
from_rescuer(success_or_failure, input, exception_message_transforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS) click to toggle source
# File lib/results.rb, line 24
def from_rescuer(success_or_failure, input, exception_message_transforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS)
  success_or_failure.transform(
    lambda { |v| Good.new(v) },
    lambda { |e| Bad.new(transform_exception_message(e, exception_message_transforms), input) }
  ).get
end
new(input_or_proc) { |input| ... } click to toggle source
# File lib/results.rb, line 12
def new(input_or_proc)
  exceptions_as_bad = DEFAULT_EXCEPTIONS_TO_RESCUE_AS_BADS
  exceptions_xforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS

  rescued = Rescuer.new(*exceptions_as_bad) do
    call_or_yield_or_return(input_or_proc) { |input| block_given? ? yield(input) : input }
  end

  from_rescuer(rescued, input_or_proc, exceptions_xforms)
end
predicate(method_name) click to toggle source
# File lib/results.rb, line 64
def predicate(method_name)
  Filter.new(method_name.to_s.gsub(/\?\Z/, '')) { |v| v.send(method_name) }
end
transform_exception_message(exception, exception_message_transforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS) click to toggle source
# File lib/results.rb, line 32
def transform_exception_message(exception, exception_message_transforms = DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS)
  _, f = exception_message_transforms.find { |klass, _| klass === exception }
  message = exception && exception.message
  f && f.call(message) || message
end
when(*args) click to toggle source
# File lib/results.rb, line 39
def when(*args)
  lambda { |v| Results.new(v).when(*args) }
end
when_not(*args) click to toggle source
# File lib/results.rb, line 44
def when_not(*args)
  lambda { |v| Results.new(v).when_not(*args) }
end