Sha256: 363a10f72ed29c25c70564b8c15baa7594ee8ce03c13cf2e729134fb67edbfa9

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

== array_logic

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

One prerequisite for the comparison is that the objects have an id method that
returns a unique (within the set of objects) integer.

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]]

You can also test for arrays that do not match the rule by using *block* 
and *blockers*:

    rule_two.block([a1, a2])            --> true
    rule_two.blockers([a1, a2], [a1])    --> [[a1, a2]]

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 above, 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.2 README.rdoc