Class: YES::Constraints::Exclusive

Inherits:
TreeConstraint show all
Defined in:
lib/yes/constraints/exclusive.rb

Overview

Exclusion - This can either be a boolean expression in which case it validates that there is no more than one matching node. Otherwise, the value is taken to be a ypath and validates that there are no matching paths if the main selection is present. – TODO: Provide $parent$ path substitution ? ++

Instance Attribute Summary

Attributes inherited from AbstractConstraint

#nodes, #spec, #tree

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractConstraint

#applicable?, inherited, #initialize, #match_delta, #recurse_valid?, #valid?

Constructor Details

This class inherits a constructor from YES::Constraints::AbstractConstraint

Class Method Details

.applicable?(spec) ⇒ Boolean

Only applicable if ‘exclusive` feild is in the spec.

Returns:

  • (Boolean)


22
23
24
# File 'lib/yes/constraints/exclusive.rb', line 22

def self.applicable?(spec)
  spec['exclusive']
end

.checklist(spec, tree, nodes) ⇒ Array<Constraint>

Returns:

  • (Array<Constraint>)


16
17
18
19
# File 'lib/yes/constraints/exclusive.rb', line 16

def self.checklist(spec, tree, nodes)
  return [] unless applicable?(spec)
  [new(spec, tree, nodes)]
end

Instance Method Details

#validate(spec) ⇒ Boolean

Exclusion - This can either be a boolean expression in which case it validates that there is no more than one matching node. Otherwise, the value is taken to be a ypath and validates that there are no matching paths if the main selection is present.

Returns:

  • (Boolean)

    validity



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yes/constraints/exclusive.rb', line 32

def validate(spec)
  exclusive = spec['exclusive']

  case exclusive
  when true, false
    nodes.size <= 1
  else
    ex_nodes = tree.select(exclusive)
    nodes.size == 0 or ex_nodes.size == 0
  end
end