Class: YES::Constraints::AbstractConstraint

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/constraints/abstract_constraint.rb

Overview

AbstractConstraint serves as the base class for all other constraints.

Direct Known Subclasses

NodeConstraint, TreeConstraint

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#specObject (readonly)

Returns the value of attribute spec.



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

def spec
  @spec
end

#treeObject (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

Returns:

  • (Boolean)


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

Returns:

  • (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.

Examples:

1..1  =~ 1
1...2 =~ 1
[1,1] =~ 1
[1,2) =~ 1
(1,2] =~ 2
(1,3) =~ 2

Parameters:

  • range (Array, String)

    The range representation.



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)

Returns:

  • (Boolean)


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

Returns:

  • (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