Module: Constraint::Helper

Included in:
Shell, Shell
Defined in:
lib/constraint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constraintsObject (readonly)

The current class’s/object’s head constraint.



87
88
89
# File 'lib/constraint.rb', line 87

def constraints
  @constraints
end

#descriptionsObject (readonly)

A hash of constraint descriptions.



89
90
91
# File 'lib/constraint.rb', line 89

def descriptions
  @descriptions
end

Instance Method Details

#and_constraint(name, desc = nil, &block) ⇒ Object

Define an AndConstraint. This and all previous constraints have to succeed.

name

This constraint’s name

description

Optional constraint description

block

The constraint that must evaluate to true in order for this constraint to succeed



106
107
108
109
# File 'lib/constraint.rb', line 106

def and_constraint(name, desc=nil, &block)
    describe_constraint(name, desc)
    @constraints = AndConstraint.new(self, name, desc, &block)
end

#handle_constraint_violation(name, object) ⇒ Object

Handle constraint violations. Can be overwritten by subclasses in order to rescue the constraint. The return value will replace the original object.

name

The constraint’s name that didn’t succeed

object

The object that caused the violation



123
124
125
126
127
128
# File 'lib/constraint.rb', line 123

def handle_constraint_violation(name, object)
    c = @descriptions.collect {|name, desc| desc}.join(', ')
    raise ConstraintViolation,
        "#{self.class}: #{object.class} didn't meet constraints (#{c}): #{object.inspect}", 
        caller[5..-1]
end

#log_constraint_exception(exception) ⇒ Object

If $VERBOSE is set, print the exception to $stderr.



112
113
114
115
116
# File 'lib/constraint.rb', line 112

def log_constraint_exception(exception)
    if $VERBOSE
        $stderr.puts exception
    end
end

#or_constraint(name, desc = nil, &block) ⇒ Object

Define an OrConstraint. If the new constraint succeedes, all previous constraints become obsolete.

name

This constraint’s name

description

Optional constraint description

block

The constraint that must evaluate to true in order for this constraint to succeed



96
97
98
99
# File 'lib/constraint.rb', line 96

def or_constraint(name, desc=nil, &block)
    describe_constraint(name, desc)
    @constraints = OrConstraint.new(self, name, desc, &block)
end