Class: YES::Constraints::Type

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

Overview

Validate the tag type. This ensure all the matching nodes have the same base type specified. The tag type is the portion of the tag the occcurs after the last non-alphanumeric character, usually a ‘:`. In essence the tag type is the tag regardless of namespace.

Also, ‘#` is treated specially as a subset fo they type, so it will be used if given in the type comparison.

Examples:

'foo' =~ '!!foo'
'foo' =~ '!<tag:foo.org/bar:foo>'
'foo' =~ '!<tag:foo.org/bar:foo>'
'foo' =~ '!!foo#alt'
'foo#alt' =~ '!!foo#alt'

Constant Summary collapse

TYPE_MATCH =
{
  'date'   => ['timestamp'],
  'bool'   => ['bool'],
  'number' => ['int', 'float']
}

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)


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

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

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

Returns:

  • (Array<Constraint>)


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

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

Instance Method Details

#base_type_id(node) ⇒ String (private)

Get the base of the node’s type tag, i.e. the part after the last non-alphanumeric characeter plus ‘#`.

Returns:

  • (String)

    base type



63
64
65
# File 'lib/yes/constraints/type.rb', line 63

def base_type_id(node)
  fixed_type_id(node).split(/[^#A-Za-z0-9_-]/).last
end

#fixed_type_id(node) ⇒ String (private)

Return the ‘type_id` of a node. If the type_id is `nil` then returns the `kind`.

Returns:

  • (String)

    type_id or kind of node



71
72
73
74
75
76
77
78
# File 'lib/yes/constraints/type.rb', line 71

def fixed_type_id(node)
  type_id = node.type_id
  if type_id
    type_id
  else
    node.kind.to_s
  end
end

#type_match(node, type) ⇒ Boolean (private)

Get the nodes base type see if matches the given type. The TYPE_MATCH chart is used to map a node base types to a given type.

Returns:

  • (Boolean)

    node’s type matches given type



49
50
51
52
53
54
55
56
57
# File 'lib/yes/constraints/type.rb', line 49

def type_match(node, type)
  node_type, node_subtype = base_type_id(node).split('#')
  want_type, want_subtype = type.split('#')

  return false if want_subtype && want_subtype != node_subtype

  pick_type = TYPE_MATCH[want_type] || [want_type]
  pick_type.any?{ |t| t == node_type }
end

#validate(spec) ⇒ Object

Validate type.



38
39
40
# File 'lib/yes/constraints/type.rb', line 38

def validate(spec)
  type_match(node, spec['type'])
end