File: boolean.rb

package info (click to toggle)
ruby-morpher 0.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 520 kB
  • sloc: ruby: 2,366; makefile: 4
file content (76 lines) | stat: -rw-r--r-- 2,021 bytes parent folder | download
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
module Morpher
  class Evaluator
    class Predicate
      # Evaluator for nary boolean predicates
      class Boolean < self
        include Nary

        # Call evaluator with input
        #
        # @param [Object] input
        #
        # @return [Boolean]
        #
        # @api private
        #
        def call(input)
          body .public_send(
            self.class::ENUMERABLE_METHOD
          ) { |evaluator| evaluator.call(input) }
        end

        # Return evaluation for input
        #
        # @param [Object] input
        #
        # @return [Evaluation::Nary]
        #
        # @api private
        #
        def evaluation(input)
          klass = self.class

          evaluations = body.each_with_object([]) do |evaluator, aggregate|
            evaluation = evaluator.evaluation(input)
            aggregate << evaluation
            next if evaluation.output.equal?(klass::OUTPUT_EXPECTATION)
            return send(klass::ERROR_METHOD, input, aggregate)
          end

          send(klass::SUCCESS_METHOD, input, evaluations)
        end

        # Evaluator for nary and predicates
        class And < self
          register :and

          ENUMERABLE_METHOD  = :all?
          OUTPUT_EXPECTATION = true
          ERROR_METHOD       = :evaluation_negative
          SUCCESS_METHOD     = :evaluation_positive
        end # And

        # Evaluator for nary or predicates
        class Or < self
          register :or

          ENUMERABLE_METHOD  = :any?
          OUTPUT_EXPECTATION = false
          ERROR_METHOD       = :evaluation_positive
          SUCCESS_METHOD     = :evaluation_negative
        end # Or

        # Evaluator for nary xor predicates
        class Xor < self
          register :xor

          ENUMERABLE_METHOD  = :one?
          OUTPUT_EXPECTATION = false
          ERROR_METHOD       = :evaluation_positive
          SUCCESS_METHOD     = :evaluation_negative
        end # Xor

      end # Boolean
    end # Predicate
  end # Evaluator
end # Morpher