Class: YES::Constraints::Length

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

Overview

Validate if a node’s value is within a certain length. The value is converted to a string using #to_s for the comparison.

//code:
  length: 3

A valid code value could then have no more than three characters.

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)


27
28
29
# File 'lib/yes/constraints/length.rb', line 27

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

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

Returns:

  • (Array<Constraint>)


18
19
20
21
22
23
# File 'lib/yes/constraints/length.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 value is within a certain length. The value is converted to a string using #to_s for the comparison.

Returns:

  • (Boolean)

    validity



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

def validate(spec)
  length = spec['length']
  match_delta(length, node.value.to_s.size)
end