Class: Contracts::Contract
- Inherits:
-
Object
- Object
- Contracts::Contract
- Defined in:
- lib/contracts.rb
Instance Attribute Summary collapse
-
#allowed_exceptions ⇒ Object
readonly
Returns the value of attribute allowed_exceptions.
-
#examples ⇒ Object
readonly
Returns the value of attribute examples.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#invariants ⇒ Object
readonly
Returns the value of attribute invariants.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#method_source_location ⇒ Object
Returns the value of attribute method_source_location.
-
#method_type ⇒ Object
readonly
Returns the value of attribute method_type.
-
#mutation_policy ⇒ Object
readonly
Returns the value of attribute mutation_policy.
-
#observed ⇒ Object
readonly
Returns the value of attribute observed.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#positionals ⇒ Object
Returns the value of attribute positionals.
-
#postconditions ⇒ Object
readonly
Returns the value of attribute postconditions.
-
#preconditions ⇒ Object
readonly
Returns the value of attribute preconditions.
-
#required_changes ⇒ Object
readonly
Returns the value of attribute required_changes.
-
#return_constraint ⇒ Object
Returns the value of attribute return_constraint.
-
#snapshot_block ⇒ Object
readonly
Returns the value of attribute snapshot_block.
-
#source_location ⇒ Object
readonly
Returns the value of attribute source_location.
-
#unchanged_on_raise_types ⇒ Object
readonly
Returns the value of attribute unchanged_on_raise_types.
Instance Method Summary collapse
- #all_invariants ⇒ Object
- #inherited_invariants ⇒ Object
-
#initialize(owner:, method_name:, source_location:, method_type: :instance, options: {}) ⇒ Contract
constructor
A new instance of Contract.
- #observed_fields ⇒ Object
- #own_invariants ⇒ Object
- #permitted_changes ⇒ Object
- #permitted_changes=(values) ⇒ Object
- #pure? ⇒ Boolean
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(owner:, method_name:, source_location:, method_type: :instance, options: {}) ⇒ Contract
Returns a new instance of Contract.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/contracts.rb', line 297 def initialize(owner:, method_name:, source_location:, method_type: :instance, options: {}) @id = "#{owner.name || owner.object_id}:#{method_type}:#{method_name}".freeze @owner = owner @method_name = method_name.to_sym @method_type = method_type @source_location = source_location @options = .freeze @parameters = {} @positionals = [] @preconditions = [] @postconditions = [] @allowed_exceptions = [] @invariants = [] @observed = [] @examples = [] @required_changes = [] @unchanged_on_raise_types = [] @mutation_policy = :unspecified end |
Instance Attribute Details
#allowed_exceptions ⇒ Object (readonly)
Returns the value of attribute allowed_exceptions.
294 295 296 |
# File 'lib/contracts.rb', line 294 def allowed_exceptions @allowed_exceptions end |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
294 295 296 |
# File 'lib/contracts.rb', line 294 def examples @examples end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
294 295 296 |
# File 'lib/contracts.rb', line 294 def id @id end |
#invariants ⇒ Object (readonly)
Returns the value of attribute invariants.
294 295 296 |
# File 'lib/contracts.rb', line 294 def invariants @invariants end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
294 295 296 |
# File 'lib/contracts.rb', line 294 def method_name @method_name end |
#method_source_location ⇒ Object
Returns the value of attribute method_source_location.
293 294 295 |
# File 'lib/contracts.rb', line 293 def method_source_location @method_source_location end |
#method_type ⇒ Object (readonly)
Returns the value of attribute method_type.
294 295 296 |
# File 'lib/contracts.rb', line 294 def method_type @method_type end |
#mutation_policy ⇒ Object (readonly)
Returns the value of attribute mutation_policy.
294 295 296 |
# File 'lib/contracts.rb', line 294 def mutation_policy @mutation_policy end |
#observed ⇒ Object (readonly)
Returns the value of attribute observed.
294 295 296 |
# File 'lib/contracts.rb', line 294 def observed @observed end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
294 295 296 |
# File 'lib/contracts.rb', line 294 def @options end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
294 295 296 |
# File 'lib/contracts.rb', line 294 def owner @owner end |
#parameters ⇒ Object
Returns the value of attribute parameters.
294 295 296 |
# File 'lib/contracts.rb', line 294 def parameters @parameters end |
#positionals ⇒ Object
Returns the value of attribute positionals.
294 295 296 |
# File 'lib/contracts.rb', line 294 def positionals @positionals end |
#postconditions ⇒ Object (readonly)
Returns the value of attribute postconditions.
294 295 296 |
# File 'lib/contracts.rb', line 294 def postconditions @postconditions end |
#preconditions ⇒ Object (readonly)
Returns the value of attribute preconditions.
294 295 296 |
# File 'lib/contracts.rb', line 294 def preconditions @preconditions end |
#required_changes ⇒ Object (readonly)
Returns the value of attribute required_changes.
294 295 296 |
# File 'lib/contracts.rb', line 294 def required_changes @required_changes end |
#return_constraint ⇒ Object
Returns the value of attribute return_constraint.
294 295 296 |
# File 'lib/contracts.rb', line 294 def return_constraint @return_constraint end |
#snapshot_block ⇒ Object (readonly)
Returns the value of attribute snapshot_block.
294 295 296 |
# File 'lib/contracts.rb', line 294 def snapshot_block @snapshot_block end |
#source_location ⇒ Object (readonly)
Returns the value of attribute source_location.
294 295 296 |
# File 'lib/contracts.rb', line 294 def source_location @source_location end |
#unchanged_on_raise_types ⇒ Object (readonly)
Returns the value of attribute unchanged_on_raise_types.
294 295 296 |
# File 'lib/contracts.rb', line 294 def unchanged_on_raise_types @unchanged_on_raise_types end |
Instance Method Details
#all_invariants ⇒ Object
341 |
# File 'lib/contracts.rb', line 341 def all_invariants = Contracts.invariants_for(owner) |
#inherited_invariants ⇒ Object
343 |
# File 'lib/contracts.rb', line 343 def inherited_invariants = all_invariants - own_invariants |
#observed_fields ⇒ Object
338 |
# File 'lib/contracts.rb', line 338 def observed_fields = observed.map(&:name).freeze |
#own_invariants ⇒ Object
342 |
# File 'lib/contracts.rb', line 342 def own_invariants = Contracts.invariants_for(owner).select { |invariant| invariant.owner == owner } |
#permitted_changes ⇒ Object
339 |
# File 'lib/contracts.rb', line 339 def permitted_changes = mutation_policy == :pure ? [] : (@permitted_changes || []).freeze |
#permitted_changes=(values) ⇒ Object
345 346 347 |
# File 'lib/contracts.rb', line 345 def permitted_changes=(values) @permitted_changes = values.map(&:to_sym).freeze end |
#pure? ⇒ Boolean
340 |
# File 'lib/contracts.rb', line 340 def pure? = mutation_policy == :pure |
#to_h ⇒ Object
331 332 333 334 335 |
# File 'lib/contracts.rb', line 331 def to_h { id: id, owner: owner.name, method_name: method_name, method_type: method_type, parameters: parameters.transform_values(&:description), positional: positionals.map(&:description), preconditions: preconditions.map(&:description), postconditions: postconditions.map(&:description), return_constraint: return_constraint&.description, invariants: Contracts.invariants_for(owner).map(&:description), allowed_exceptions: allowed_exceptions.map do |r| r.type.name end, mutation_policy: mutation_policy, observed: observed.map(&:to_h), permitted_changes: permitted_changes, required_changes: required_changes, source_location: source_location, method_source_location: method_source_location, options: } end |
#to_json ⇒ Object
337 |
# File 'lib/contracts.rb', line 337 def to_json(*) = JSON.generate(to_h) |