class Mexico::Constraints::Constraint
This class defines a constraint used for type-validating an object.
Attributes
children[RW]
evaluator[RW]
key[RW]
parents[RW]
Public Class Methods
create(key, config={}, &evaluator)
click to toggle source
# 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)
click to toggle source
# File lib/mexico/constraints/constraint.rb, line 40 def self.get(key) defined?(@@REGISTERED_CONSTRAINTS) && @@REGISTERED_CONSTRAINTS[key] end
knows?(key)
click to toggle source
# File lib/mexico/constraints/constraint.rb, line 36 def self.knows?(key) defined?(@@REGISTERED_CONSTRAINTS) && @@REGISTERED_CONSTRAINTS.has_key?(key) end
new(key, evaluator)
click to toggle source
# File lib/mexico/constraints/constraint.rb, line 48 def initialize(key, evaluator) @key = key @evaluator = evaluator @parents = [] @children = [] end
Public Instance Methods
add_child(child)
click to toggle source
# 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)
click to toggle source
# File lib/mexico/constraints/constraint.rb, line 55 def add_parent(parent) internal_add_parent(parent) parent.internal_add_child(self) end
evaluate(document)
click to toggle source
# File lib/mexico/constraints/constraint.rb, line 44 def evaluate(document) self.evaluator.call(document) end
Protected Instance Methods
internal_add_child(child)
click to toggle source
# File lib/mexico/constraints/constraint.rb, line 81 def internal_add_child(child) @children << child end
internal_add_parent(parent)
click to toggle source
# File lib/mexico/constraints/constraint.rb, line 77 def internal_add_parent(parent) @parents << parent end