Class: YES::Constraints::Key

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

Overview

Validate if a mapping node’s keys conforms to a constraint.

//authors:
  type: map
  key:
    type: str

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)


40
41
42
# File 'lib/yes/constraints/key.rb', line 40

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

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

For key constraint, the work is all handled by the checklist method.

Returns:

  • (Array<Constraint>)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/yes/constraints/key.rb', line 18

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

  key_spec = spec['key']
  list     = []

  nodes.each do |node|
    case node.kind
    when :map
      YES.constraints.each do |c|
        list.concat(c.checklist(key_spec, tree, node.value.keys))
      end
    else
      raise "key constraint applies only to mappings"
    end
  end

  list
end

Instance Method Details

#validate(spec) ⇒ Boolean

no-op

Returns:

  • (Boolean)

    validity



48
49
# File 'lib/yes/constraints/key.rb', line 48

def validate(spec)
end