1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
module Morpher
class Evaluator
class Transformer
# Transformer over each element in an enumerable
class Map < self
include Unary
register :map
# Test if evaluator is transitive
#
# @return [true]
# if evaluator is transitive
#
# @return [false]
# otherwise
#
# @api private
#
def transitive?
operand.transitive?
end
# Call evaluator
#
# @param [Enumerable#map] input
#
# @return [Enumerable]
# if input evaluates true under predicate
#
# @raise [TransformError]
# otherwise
#
# @api private
#
def call(input)
input.map(&operand.method(:call))
end
# Return evaluation
#
# @param [Enumerable#map] input
#
# @return [Evaluation]
#
# @api private
#
# rubocop:disable MethodLength
#
def evaluation(input)
evaluations = input.each_with_object([]) do |item, aggregate|
evaluation = operand.evaluation(item)
aggregate << evaluation
unless evaluation.success?
return evaluation_error(input, aggregate)
end
end
Evaluation::Nary.success(
evaluator: self,
input: input,
output: evaluations.map(&:output),
evaluations: evaluations
)
end
# Return inverse evaluator
#
# @return [Evaluator::Transformer]
#
# @api private
#
def inverse
self.class.new(operand.inverse)
end
private
# Return evaluation error
#
# @param [Object] input
# @param [Array<Evaluation>] evaluations
#
# @return [Evaluation]
#
# @api private
#
def evaluation_error(input, evaluations)
Evaluation::Nary.error(
evaluator: self,
input: input,
evaluations: evaluations
)
end
end # Guard
end # Transformer
end # Evaluator
end # Morpher
|