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
|
module Morpher
class Evaluator
class Transformer
# Evaluator to perform n transformations in a row
class Block < self
include Nary
register :block
# Test if evaluator is transitive
#
# @return [true]
# if block is transitive
#
# @return [false]
# otherwise
#
# @api private
#
def transitive?
body.all?(&:transitive?)
end
# Call transformer
#
# @param [Object] input
#
# @return [Object]
#
# @api private
#
def call(input)
body.reduce(input) do |state, evaluator|
evaluator.call(state)
end
end
# Return inverse evaluator
#
# @return [Evaluator]
#
# @api private
#
def inverse
self.class.new(body.reverse.map(&:inverse))
end
# Return evaluation for input
#
# @param [Object] input
#
# @return [Evaluation::Nary]
#
# @api private
#
# rubocop:disable MethodLength
#
def evaluation(input)
state = input
evaluations = body.each_with_object([]) do |evaluator, aggregate|
evaluation = evaluator.evaluation(state)
aggregate << evaluation
unless evaluation.success?
return evaluation_error(input, aggregate)
end
state = evaluation.output
end
Evaluation::Nary.success(
evaluator: self,
input: input,
output: state,
evaluations: evaluations
)
end
end # Block
end # Transformer
end # Evaluato
end # Morpher
|