Class: YES::Constraints::Required

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

Overview

TODO: For the moment this is the same as Inclusive.

It was originall inteded to work like {RequiresValidation}
but it rpoved hard to work out the validation procedure
when matching to the subfield. If we can fix it maybe we will
keep, but for now THIS IS NOT USED.

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 ‘required` field appears in spec.

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Array<Constraint>)


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

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

Instance Method Details

#validate(spec) ⇒ Boolean

Validates whether a matching node must be present within it’s parent.

Returns:

  • (Boolean)

    validity



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/yes/constraints/required.rb', line 29

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

  case required
  when true, false
    nodes.size > 0
  else
    in_nodes = tree.select(required)
    nodes.size == 0 or in_nodes.size > 0
  end
end