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
|
module Morpher
class Evaluator
class Transformer
# Transformer that allows to guard transformation process
# with a predicate on input
class Guard < self
include Unary, Transitive
register :guard
# Call evaluator
#
# @param [Object] input
#
# @return [Object]
# if input evaluates true under predicate
#
# @raise [TransformError]
# otherwise
#
# @api private
#
def call(input)
if operand.call(input)
input
else
fail TransformError.new(self, input)
end
end
# Return evaluation
#
# @param [Object] input
#
# @return [Evaluation::Guard]
#
# @api private
#
def evaluation(input)
operand_evaluation = operand.evaluation(input)
if operand_evaluation.output
evaluation_success(input, operand_evaluation, input)
else
evaluation_error(input, operand_evaluation)
end
end
# Return inverse evaluator
#
# @return [self]
#
# @api private
#
def inverse
self
end
end # Guard
end # Transformer
end # Evaluator
end # Morpher
|