Class: Mexico::Constraints::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/mexico/constraints/constraint.rb

Overview

This class defines a constraint used for type-validating an object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, evaluator) ⇒ Constraint

Returns a new instance of Constraint.



48
49
50
51
52
53
# File 'lib/mexico/constraints/constraint.rb', line 48

def initialize(key, evaluator)
  @key = key
  @evaluator = evaluator
  @parents = []
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



69
70
71
# File 'lib/mexico/constraints/constraint.rb', line 69

def children
  @children
end

#evaluatorObject

Returns the value of attribute evaluator.



66
67
68
# File 'lib/mexico/constraints/constraint.rb', line 66

def evaluator
  @evaluator
end

#keyObject

Returns the value of attribute key.



67
68
69
# File 'lib/mexico/constraints/constraint.rb', line 67

def key
  @key
end

#parentsObject

Returns the value of attribute parents.



68
69
70
# File 'lib/mexico/constraints/constraint.rb', line 68

def parents
  @parents
end

Class Method Details

.create(key, config = {}, &evaluator) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/mexico/constraints/constraint.rb', line 25

def self.create(key, config={}, &evaluator)
  constraint = self.new(key, evaluator)
  constraint.add_parent(config[:parent]) if config.has_key?(:parent)
  config[:parents].each{ |p| constraint.add_parent(p) } if config.has_key?(:parents)

  @@REGISTERED_CONSTRAINTS = {} unless defined?(@@REGISTERED_CONSTRAINTS)
  @@REGISTERED_CONSTRAINTS[key] = constraint

  return constraint
end

.get(key) ⇒ Object



40
41
42
# File 'lib/mexico/constraints/constraint.rb', line 40

def self.get(key)
  defined?(@@REGISTERED_CONSTRAINTS) && @@REGISTERED_CONSTRAINTS[key]
end

.knows?(key) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/mexico/constraints/constraint.rb', line 36

def self.knows?(key)
  defined?(@@REGISTERED_CONSTRAINTS) && @@REGISTERED_CONSTRAINTS.has_key?(key)
end

Instance Method Details

#add_child(child) ⇒ Object



60
61
62
63
# File 'lib/mexico/constraints/constraint.rb', line 60

def add_child(child)
  internal_add_child(child)
  child.internal_add_parent(self)
end

#add_parent(parent) ⇒ Object



55
56
57
58
# File 'lib/mexico/constraints/constraint.rb', line 55

def add_parent(parent)
  internal_add_parent(parent)
  parent.internal_add_child(self)
end

#evaluate(document) ⇒ Object



44
45
46
# File 'lib/mexico/constraints/constraint.rb', line 44

def evaluate(document)
  self.evaluator.call(document)
end