Class: YES::Constraints::Range

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

Overview

Validate the a nodes value is within a certain range. Primarily this works for numeric values, but it can also work for string in ASCII/UTF-8 order, by using a 2-element array for comparison.

//note:
  range: ['A','G']

Valid values for are then only A, B, C, D, E, F and G.

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)


26
27
28
# File 'lib/yes/constraints/range.rb', line 26

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

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

Returns:

  • (Array<Validaiton>)


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

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) ⇒ Boolean

Validate if a node is the only one of it’s value in a sequence or mapping.

Returns:

  • (Boolean)

    validity



34
35
36
37
# File 'lib/yes/constraints/range.rb', line 34

def validate(spec)
  range = spec['range']
  match_delta(range, node.transform) ? true : false
end