Class: Mexico::Constraints::Constraint
- Inherits:
-
Object
- Object
- Mexico::Constraints::Constraint
- Defined in:
- lib/mexico/constraints/constraint.rb
Overview
This class defines a constraint used for type-validating an object.
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#evaluator ⇒ Object
readonly
Returns the value of attribute evaluator.
-
#key ⇒ Object
Returns the value of attribute key.
-
#parents ⇒ Object
Returns the value of attribute parents.
Class Method Summary collapse
Instance Method Summary collapse
- #add_child(child) ⇒ Object
- #add_parent(parent) ⇒ Object
- #evaluate(document) ⇒ Object
-
#initialize(key, evaluator) ⇒ Constraint
constructor
A new instance of Constraint.
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
#children ⇒ Object
Returns the value of attribute children.
69 70 71 |
# File 'lib/mexico/constraints/constraint.rb', line 69 def children @children end |
#evaluator ⇒ Object
Returns the value of attribute evaluator.
66 67 68 |
# File 'lib/mexico/constraints/constraint.rb', line 66 def evaluator @evaluator end |
#key ⇒ Object
Returns the value of attribute key.
67 68 69 |
# File 'lib/mexico/constraints/constraint.rb', line 67 def key @key end |
#parents ⇒ Object
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
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 |