Module: Constraint::Helper
Instance Attribute Summary collapse
-
#constaint_descriptions ⇒ Object
readonly
A hash of constraint constaint_descriptions.
-
#constaint_handlers ⇒ Object
readonly
An array of constraint handlers.
-
#constraint_attributes ⇒ Object
readonly
An array of additional attributes that should be copied when replicating a shell.
-
#constraints ⇒ Object
readonly
The current class’s/object’s head constraint.
Instance Method Summary collapse
-
#and_constraint(name, desc = nil, &block) ⇒ Object
Define an SingleConstraint.
- #constraint_attr(name, val) ⇒ Object
-
#log_constraint_exception(exception) ⇒ Object
If $VERBOSE is set, print the exception to $stderr.
-
#on_constraint_violation(*names, &block) ⇒ Object
Define a new constraint handler.
-
#or_constraint(name, desc = nil, &block) ⇒ Object
Define an OrConstraint.
-
#replicate_constraints(predecessor) ⇒ Object
Copy needed class/object variables.
Instance Attribute Details
#constaint_descriptions ⇒ Object (readonly)
A hash of constraint constaint_descriptions.
76 77 78 |
# File 'lib/constraint.rb', line 76 def constaint_descriptions @constaint_descriptions end |
#constaint_handlers ⇒ Object (readonly)
An array of constraint handlers
78 79 80 |
# File 'lib/constraint.rb', line 78 def constaint_handlers @constaint_handlers end |
#constraint_attributes ⇒ Object (readonly)
An array of additional attributes that should be copied when replicating a shell
81 82 83 |
# File 'lib/constraint.rb', line 81 def constraint_attributes @constraint_attributes end |
#constraints ⇒ Object (readonly)
The current class’s/object’s head constraint.
74 75 76 |
# File 'lib/constraint.rb', line 74 def constraints @constraints end |
Instance Method Details
#and_constraint(name, desc = nil, &block) ⇒ Object
Define an SingleConstraint. 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
109 110 111 112 113 |
# File 'lib/constraint.rb', line 109 def and_constraint(name, desc=nil, &block) describe_constraint(name, desc) @constraints = @constraints.dup @constraints << SingleConstraint.new(self, name, desc, &block) end |
#constraint_attr(name, val) ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/constraint.rb', line 145 def constraint_attr(name, val) (@constraint_attributes = @constraint_attributes.dup) << name instance_eval %{ class << self attr_accessor :#{name} end attr_accessor :#{name} } instance_variable_set("@#{name}", val) end |
#log_constraint_exception(exception) ⇒ Object
If $VERBOSE is set, print the exception to $stderr.
116 117 118 119 120 |
# File 'lib/constraint.rb', line 116 def log_constraint_exception(exception) if $VERBOSE $stderr.puts exception end end |
#on_constraint_violation(*names, &block) ⇒ Object
Define a new constraint handler. The block takes a continuation as its first argument. If the amendment of the object isn’t successful, the handler must throw an Constraint::Violation exception.
Example:
enum = EvenInteger.new(2)
enum.and_constraint('LT10') {|enum, n| n < 10}
enum.on_constraint_violation('LT10') do |o, n|
n > 10 ? 10 : 0
end
- *names
-
The constraints names
- block
-
The handler (argument: object)
137 138 139 140 141 142 143 |
# File 'lib/constraint.rb', line 137 def on_constraint_violation(*names, &block) @constaint_handlers = @constaint_handlers.dup names.each do |name| @constaint_handlers[name] ||= [] @constaint_handlers[name] << block end end |
#or_constraint(name, desc = nil, &block) ⇒ Object
Define an OrConstraint. Either this or the previous constraint 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/constraint.rb', line 87 def or_constraint(name, desc=nil, &block) describe_constraint(name, desc) @constraints = @constraints.dup constraint = SingleConstraint.new(self, name, desc, &block) if @constraints.empty? @constraints << constraint else last = @constraints.last case last when OrConstraint last << constraint else @constraints[-1] = OrConstraint.new([last, constraint]) end end end |
#replicate_constraints(predecessor) ⇒ Object
Copy needed class/object variables.
157 158 159 160 161 162 163 164 165 |
# File 'lib/constraint.rb', line 157 def replicate_constraints(predecessor) @constaint_descriptions = predecessor.constaint_descriptions @constaint_handlers = predecessor.constaint_handlers @constraints = predecessor.constraints for name in (@constraint_attributes = predecessor.constraint_attributes) nname = "@#{name}" instance_variable_set(nname, predecessor.instance_variable_get(nname)) end end |