Class: YES::Constraints::Choice

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

Overview

Validate from a selction of choices.

choice: [M,F]

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

Only applicable if ‘choice` field is in spec.

Returns:

  • (Boolean)


21
22
23
# File 'lib/yes/constraints/choice.rb', line 21

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

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

Returns:

  • (Array<Constraint>)


13
14
15
16
17
18
# File 'lib/yes/constraints/choice.rb', line 13

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 that a node’s value is amoung a provided list of values.

Returns:

  • (Boolean)

    validity



29
30
31
32
# File 'lib/yes/constraints/choice.rb', line 29

def validate(spec)
  choice = Array(spec['choice'])
  choice.include?(node.transform)
end