Class: YES::Constraints::Count

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

Overview

Count validation ensures there are a limited number of matching nodes in a document.

//gold:
  count: 1..4

Would mean the ‘gold` mapping key could only appear 1 to 4 times in the entire document.

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 ‘count` entry is in the spec.

Returns:

  • (Boolean)


23
24
25
# File 'lib/yes/constraints/count.rb', line 23

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

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

Returns:

  • (Array<Constraint>)


17
18
19
20
# File 'lib/yes/constraints/count.rb', line 17

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

Instance Method Details

#validate(spec) ⇒ Object

Validate count ensure there is a minimum and/or maximum number of matching nodes.



29
30
31
32
# File 'lib/yes/constraints/count.rb', line 29

def validate(spec)
  count = spec['count']
  match_delta(count, nodes.size)
end