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
- #required_change_bounds ⇒ Object
- #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.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/contracts.rb', line 350 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.
347 348 349 |
# File 'lib/contracts.rb', line 347 def allowed_exceptions @allowed_exceptions end |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
347 348 349 |
# File 'lib/contracts.rb', line 347 def examples @examples end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
347 348 349 |
# File 'lib/contracts.rb', line 347 def id @id end |
#invariants ⇒ Object (readonly)
Returns the value of attribute invariants.
347 348 349 |
# File 'lib/contracts.rb', line 347 def invariants @invariants end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
347 348 349 |
# File 'lib/contracts.rb', line 347 def method_name @method_name end |
#method_source_location ⇒ Object
Returns the value of attribute method_source_location.
346 347 348 |
# File 'lib/contracts.rb', line 346 def method_source_location @method_source_location end |
#method_type ⇒ Object (readonly)
Returns the value of attribute method_type.
347 348 349 |
# File 'lib/contracts.rb', line 347 def method_type @method_type end |
#mutation_policy ⇒ Object (readonly)
Returns the value of attribute mutation_policy.
347 348 349 |
# File 'lib/contracts.rb', line 347 def mutation_policy @mutation_policy end |
#observed ⇒ Object (readonly)
Returns the value of attribute observed.
347 348 349 |
# File 'lib/contracts.rb', line 347 def observed @observed end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
347 348 349 |
# File 'lib/contracts.rb', line 347 def @options end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
347 348 349 |
# File 'lib/contracts.rb', line 347 def owner @owner end |
#parameters ⇒ Object
Returns the value of attribute parameters.
347 348 349 |
# File 'lib/contracts.rb', line 347 def parameters @parameters end |
#positionals ⇒ Object
Returns the value of attribute positionals.
347 348 349 |
# File 'lib/contracts.rb', line 347 def positionals @positionals end |
#postconditions ⇒ Object (readonly)
Returns the value of attribute postconditions.
347 348 349 |
# File 'lib/contracts.rb', line 347 def postconditions @postconditions end |
#preconditions ⇒ Object (readonly)
Returns the value of attribute preconditions.
347 348 349 |
# File 'lib/contracts.rb', line 347 def preconditions @preconditions end |
#required_changes ⇒ Object (readonly)
Returns the value of attribute required_changes.
347 348 349 |
# File 'lib/contracts.rb', line 347 def required_changes @required_changes end |
#return_constraint ⇒ Object
Returns the value of attribute return_constraint.
347 348 349 |
# File 'lib/contracts.rb', line 347 def return_constraint @return_constraint end |
#snapshot_block ⇒ Object (readonly)
Returns the value of attribute snapshot_block.
347 348 349 |
# File 'lib/contracts.rb', line 347 def snapshot_block @snapshot_block end |
#source_location ⇒ Object (readonly)
Returns the value of attribute source_location.
347 348 349 |
# File 'lib/contracts.rb', line 347 def source_location @source_location end |
#unchanged_on_raise_types ⇒ Object (readonly)
Returns the value of attribute unchanged_on_raise_types.
347 348 349 |
# File 'lib/contracts.rb', line 347 def unchanged_on_raise_types @unchanged_on_raise_types end |
Instance Method Details
#all_invariants ⇒ Object
394 |
# File 'lib/contracts.rb', line 394 def all_invariants = Contracts.invariants_for(owner) |
#inherited_invariants ⇒ Object
396 |
# File 'lib/contracts.rb', line 396 def inherited_invariants = all_invariants - own_invariants |
#observed_fields ⇒ Object
391 |
# File 'lib/contracts.rb', line 391 def observed_fields = observed.map(&:name).freeze |
#own_invariants ⇒ Object
395 |
# File 'lib/contracts.rb', line 395 def own_invariants = Contracts.invariants_for(owner).select { |invariant| invariant.owner == owner } |
#permitted_changes ⇒ Object
392 |
# File 'lib/contracts.rb', line 392 def permitted_changes = mutation_policy == :pure ? [] : (@permitted_changes || []).freeze |
#permitted_changes=(values) ⇒ Object
398 399 400 |
# File 'lib/contracts.rb', line 398 def permitted_changes=(values) @permitted_changes = values.map(&:to_sym).freeze end |
#pure? ⇒ Boolean
393 |
# File 'lib/contracts.rb', line 393 def pure? = mutation_policy == :pure |
#required_change_bounds ⇒ Object
402 403 404 |
# File 'lib/contracts.rb', line 402 def required_change_bounds @required_change_bounds || {}.freeze end |
#to_h ⇒ Object
384 385 386 387 388 |
# File 'lib/contracts.rb', line 384 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
390 |
# File 'lib/contracts.rb', line 390 def to_json(*) = JSON.generate(to_h) |