Module: OCL::ClassMethods
- Defined in:
- lib/ocl/core.rb
Overview
Class-level methods to define OCL constraints.
Instance Method Summary collapse
-
#attr_accessor_with_invariant(*names) ⇒ Object
Define an attribute with automatic invariant validation on assignment.
-
#derived(name, &block) ⇒ Object
Define a derived (calculated) attribute.
- #derived_properties ⇒ Object
-
#inv(name, &block) ⇒ Object
Define an invariant constraint.
- #invariants ⇒ Object
-
#post(method_name, name, &block) ⇒ Object
Define a postcondition constraint for a method.
- #postconditions ⇒ Object
-
#pre(method_name, name, &block) ⇒ Object
Define a precondition constraint for a method.
- #preconditions ⇒ Object
Instance Method Details
#attr_accessor_with_invariant(*names) ⇒ Object
Define an attribute with automatic invariant validation on assignment.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ocl/core.rb', line 58 def attr_accessor_with_invariant(*names) names.each do |name| attr_reader name define_method("#{name}=") do |value| instance_variable_set("@#{name}", value) validate_invariants! end end end |
#derived(name, &block) ⇒ Object
Define a derived (calculated) attribute.
48 49 50 51 52 53 54 55 |
# File 'lib/ocl/core.rb', line 48 def derived(name, &block) derived_properties[name.to_sym] = block define_method(name) do context = Context.new(self) self.class.derived_properties[name.to_sym].call(context) end end |
#derived_properties ⇒ Object
26 27 28 |
# File 'lib/ocl/core.rb', line 26 def derived_properties @derived_properties ||= {} end |
#inv(name, &block) ⇒ Object
Define an invariant constraint.
31 32 33 |
# File 'lib/ocl/core.rb', line 31 def inv(name, &block) invariants << { name: name, block: block } end |
#invariants ⇒ Object
14 15 16 |
# File 'lib/ocl/core.rb', line 14 def invariants @invariants ||= [] end |
#post(method_name, name, &block) ⇒ Object
Define a postcondition constraint for a method.
42 43 44 45 |
# File 'lib/ocl/core.rb', line 42 def post(method_name, name, &block) wrap_method(method_name) postconditions[method_name] << { name: name, block: block } end |
#postconditions ⇒ Object
22 23 24 |
# File 'lib/ocl/core.rb', line 22 def postconditions @postconditions ||= Hash.new { |h, k| h[k] = [] } end |
#pre(method_name, name, &block) ⇒ Object
Define a precondition constraint for a method.
36 37 38 39 |
# File 'lib/ocl/core.rb', line 36 def pre(method_name, name, &block) wrap_method(method_name) preconditions[method_name] << { name: name, block: block } end |
#preconditions ⇒ Object
18 19 20 |
# File 'lib/ocl/core.rb', line 18 def preconditions @preconditions ||= Hash.new { |h, k| h[k] = [] } end |