Class: YES::Constraints::Tag

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

Overview

Validate that a node has specific tag. This ensure all the matching nodes have the given tag. Be sure to used quotes when starting the tag with ‘!`.

NOTE: the tag should honer %TAG directives in the document, but as of yet this is not supported. Thus fully qualified tags need to be used for anything beyond default !! and ! tags.

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)


25
26
27
# File 'lib/yes/constraints/tag.rb', line 25

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

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

Returns:

  • (Array<Constraint>)


17
18
19
20
21
22
# File 'lib/yes/constraints/tag.rb', line 17

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

Instance Method Details

#match_tag(tag, node) ⇒ Object (private)



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

def match_tag(tag, node)
  node_tag = node.type_id || "tag:yaml.org,2002:#{node.kind}"
  case node_tag
  when /^x-private:/
    tag == $' or tag == '!'+$'
  when /^tag:yaml.org,2002:/
    tag == node_tag or tag == '!!'+$'
  else
    tag == node_tag
  end
end

#validate(spec) ⇒ Boolean

Validate the tag.

Returns:

  • (Boolean)

    validity



32
33
34
35
# File 'lib/yes/constraints/tag.rb', line 32

def validate(spec)
  tag = spec['tag']
  match_tag(tag, node)
end