Sha256: ab0e761a7108dfc5e95a24b824d0839f87b8b53d1635a35a26af27a6c71368a1

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

== array_logic

A system that allows me to define the logic for comparing arrays.

The logic for an active record model Answer, looks like this:


    a1 = Answer.find(1)
    a2 = Answer.find(2)
      ....
    a5 = Answer.find(5)

    rule_one = ArrayLogic::Rule.new "(a1 and a2) or (a3 and a4)"

    rule_two = ArrayLogic::Rule.new "a1 and not a2"

    rule_three = ArrayLogic::Rule.new "2 in a1 a2 a3"

    rule_four = ArrayLogic::Rule.new "(2 in a1 a2 a3) and (1 in a4 a5)"


                  rule_one      rule_two      rule_three      rule_four
    [a1, a2]      true          false         true            false
    [a3, a4]      true          false         false           false
    [a1, a3, a5]  false         true          true            true

The *match* and *matches* methods allow arrays to be tested against these rules:

    rule_two.match([a1, a2])            --> false
    rule_two.matches([a1, a2], [a1])    --> [[a1]]

See test/array_logic/rule_test for more examples

=== Combinations that match

Two methods allow you to determine sample combinations that match the current
rule.  

    rule = ArrayLogic::Rule.new 'a1 and a2'
    rule.matching_combinations        -->  [[1,2]]
    rule.blocking_combinations -->  [[1],[2]]

To limit the number of samples presented, both only use ids used within
the rule. For the example about, an array that includes [1,2] would match, 
and so would [1,2,3]. However, arrays that only contain 1 or 2 would not match 
(for example [1,3])

Run example.rb to see some more examples

    ruby /lib/example.rb

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
array_logic-0.1.0 README.rdoc