Class: YES::Constraints::AbstractConstraint
- Inherits:
-
Object
- Object
- YES::Constraints::AbstractConstraint
- Defined in:
- lib/yes/constraints/abstract_constraint.rb
Overview
AbstractConstraint serves as the base class for all other constraints.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Class Method Summary collapse
-
.applicable?(spec) ⇒ Boolean
MUST OVERRIDE THIS METHOD IN SUBCLASSES.
-
.checklist(spec, tree, nodes) ⇒ Object
noop.
- .inherited(base) ⇒ Object
Instance Method Summary collapse
- #applicable? ⇒ Boolean
-
#initialize(spec, tree, nodes) ⇒ AbstractConstraint
constructor
A new instance of AbstractConstraint.
-
#match_delta(range, value) ⇒ Object
private
Range matching is used by a couple of validators.
- #recurse_valid?(spec = nil) ⇒ Boolean private
- #valid? ⇒ Boolean
-
#validate(spec) ⇒ Object
MUST OVERRIDE THIS METHOD IN SUBCLASSES.
Constructor Details
#initialize(spec, tree, nodes) ⇒ AbstractConstraint
Returns a new instance of AbstractConstraint.
26 27 28 29 30 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 26 def initialize(spec, tree, nodes) @spec = spec @tree = tree @nodes = nodes end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
35 36 37 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 35 def nodes @nodes end |
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
38 39 40 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 38 def spec @spec end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
41 42 43 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 41 def tree @tree end |
Class Method Details
.applicable?(spec) ⇒ Boolean
MUST OVERRIDE THIS METHOD IN SUBCLASSES
51 52 53 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 51 def self.applicable?(spec) raise "undefined class method -- `applicable?'" end |
.checklist(spec, tree, nodes) ⇒ Object
noop
21 22 23 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 21 def self.checklist(spec, tree, nodes) [] end |
.inherited(base) ⇒ Object
16 17 18 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 16 def self.inherited(base) YES.constraints << base end |
Instance Method Details
#applicable? ⇒ Boolean
62 63 64 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 62 def applicable? self.class.applicable?(spec) end |
#match_delta(range, value) ⇒ Object (private)
Range matching is used by a couple of validators.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 92 def match_delta(range, value) case range when Array # array can do string comparisons value > range.first && value < range.last when /^(.*)\.\.\.(n|N)$/ value >= $1.to_f when /^(.*)\.\.(n|N)$/ value >= $1.to_f when /^(.*)\.\.\.(.*)$/ value >= $1.to_f && value < $2.to_f when /^(.*)\.\.(.*)$/ value >= $1.to_f && value <= $2.to_f when /^\[(.*)\,(.*)\]$/ value >= $1.to_f && value <= $2.to_f when /^\[(.*)\,(.*)\)$/ value >= $1.to_f && value < $2.to_f when /^\((.*)\,(.*)\]$/ value > $1.to_f && value <= $2.to_f when /^\((.*)\,(.*)\)$/ value > $1.to_f && value < $2.to_f else # assume range is just a number range.to_f == value.to_f end end |
#recurse_valid?(spec = nil) ⇒ Boolean (private)
69 70 71 72 73 74 75 76 77 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 69 def recurse_valid?(spec=nil) spec ||= self.spec case spec when Array # logical-or spec.any?{ |sub_spec| recurse_valid?(sub_spec) } else validate(spec) end end |
#valid? ⇒ Boolean
56 57 58 59 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 56 def valid? return true unless applicable? recurse_valid?(spec) end |
#validate(spec) ⇒ Object
MUST OVERRIDE THIS METHOD IN SUBCLASSES
45 46 47 |
# File 'lib/yes/constraints/abstract_constraint.rb', line 45 def validate(spec) raise "undefined method -- `validate'" end |