Class: YES::Constraints::Requires

Inherits:
NodeConstraint show all
Defined in:
lib/yes/constraints/requires.rb

Overview

Takes a list of relative YPaths and ensures they are present. This is most useful for ensuring the existance of mapping fields.

foo:
  requires:
    - bar

A valid document would be:

foo:
  bar: true

The literal meaing of this example is “if ‘foo` exists, the make sure `foo/bar` also exists.

Instance Attribute Summary

Attributes inherited from NodeConstraint

#node

Attributes inherited from AbstractConstraint

#nodes, #spec, #tree

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeConstraint

#initialize, #tag, #value

Methods inherited from AbstractConstraint

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

Constructor Details

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

Class Method Details

.applicable?(spec) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/yes/constraints/requires.rb', line 31

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

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

Returns:

  • (Array<Constraint>)


23
24
25
26
27
28
# File 'lib/yes/constraints/requires.rb', line 23

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

Instance Method Details

#validate(spec) ⇒ Boolen

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

Returns:

  • (Boolen)

    validity



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yes/constraints/requires.rb', line 38

def validate(spec)
  requires = Array(spec['requires'])

  requires.each do |rq|
    case rq
    when /^\// # absolute path
      rq_nodes = tree.select(rq)
    else
      rq_nodes = node.select(rq)
    end
    return false unless rq_nodes.size > 0
  end

end