Module: Results

Defined in:
lib/results.rb,
lib/results/version.rb

Defined Under Namespace

Classes: Bad, Because, Filter, Good, HeadCombiner

Constant Summary collapse

DEFAULT_EXCEPTIONS_TO_RESCUE_AS_BADS =
[ArgumentError]
DEFAULT_EXCEPTION_MESSAGE_TRANSFORMS =
{
  ArgumentError => lambda do |m|
    r = /\Ainvalid (value|string) for ([A-Z][a-z]+)(\(\))?(: ".*")?\Z/
    m.gsub(r) { |_| "invalid value for #{$2}".downcase }
  end
}
VERSION =

The current version of this gem

'0.0.2'

Class Method Summary collapse

Class Method Details

.call_or_yield_or_return(proc_or_value, *args) ⇒ Object

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


228
229
230
231
232
233
234
# 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) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
# 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) ⇒ Object



24
25
26
27
28
29
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



64
65
66
# 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) ⇒ Object



32
33
34
35
36
# 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) ⇒ Object



39
40
41
# File 'lib/results.rb', line 39

def when(*args)
  lambda { |v| Results.new(v).when(*args) }
end

.when_not(*args) ⇒ Object



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

def when_not(*args)
  lambda { |v| Results.new(v).when_not(*args) }
end