Class: Contracts::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/contracts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = 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_exceptionsObject (readonly)

Returns the value of attribute allowed_exceptions.



347
348
349
# File 'lib/contracts.rb', line 347

def allowed_exceptions
  @allowed_exceptions
end

#examplesObject (readonly)

Returns the value of attribute examples.



347
348
349
# File 'lib/contracts.rb', line 347

def examples
  @examples
end

#idObject (readonly)

Returns the value of attribute id.



347
348
349
# File 'lib/contracts.rb', line 347

def id
  @id
end

#invariantsObject (readonly)

Returns the value of attribute invariants.



347
348
349
# File 'lib/contracts.rb', line 347

def invariants
  @invariants
end

#method_nameObject (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_locationObject

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_typeObject (readonly)

Returns the value of attribute method_type.



347
348
349
# File 'lib/contracts.rb', line 347

def method_type
  @method_type
end

#mutation_policyObject (readonly)

Returns the value of attribute mutation_policy.



347
348
349
# File 'lib/contracts.rb', line 347

def mutation_policy
  @mutation_policy
end

#observedObject (readonly)

Returns the value of attribute observed.



347
348
349
# File 'lib/contracts.rb', line 347

def observed
  @observed
end

#optionsObject (readonly)

Returns the value of attribute options.



347
348
349
# File 'lib/contracts.rb', line 347

def options
  @options
end

#ownerObject (readonly)

Returns the value of attribute owner.



347
348
349
# File 'lib/contracts.rb', line 347

def owner
  @owner
end

#parametersObject

Returns the value of attribute parameters.



347
348
349
# File 'lib/contracts.rb', line 347

def parameters
  @parameters
end

#positionalsObject

Returns the value of attribute positionals.



347
348
349
# File 'lib/contracts.rb', line 347

def positionals
  @positionals
end

#postconditionsObject (readonly)

Returns the value of attribute postconditions.



347
348
349
# File 'lib/contracts.rb', line 347

def postconditions
  @postconditions
end

#preconditionsObject (readonly)

Returns the value of attribute preconditions.



347
348
349
# File 'lib/contracts.rb', line 347

def preconditions
  @preconditions
end

#required_changesObject (readonly)

Returns the value of attribute required_changes.



347
348
349
# File 'lib/contracts.rb', line 347

def required_changes
  @required_changes
end

#return_constraintObject

Returns the value of attribute return_constraint.



347
348
349
# File 'lib/contracts.rb', line 347

def return_constraint
  @return_constraint
end

#snapshot_blockObject (readonly)

Returns the value of attribute snapshot_block.



347
348
349
# File 'lib/contracts.rb', line 347

def snapshot_block
  @snapshot_block
end

#source_locationObject (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_typesObject (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_invariantsObject



394
# File 'lib/contracts.rb', line 394

def all_invariants = Contracts.invariants_for(owner)

#inherited_invariantsObject



396
# File 'lib/contracts.rb', line 396

def inherited_invariants = all_invariants - own_invariants

#observed_fieldsObject



391
# File 'lib/contracts.rb', line 391

def observed_fields = observed.map(&:name).freeze

#own_invariantsObject



395
# File 'lib/contracts.rb', line 395

def own_invariants = Contracts.invariants_for(owner).select { |invariant| invariant.owner == owner }

#permitted_changesObject



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

Returns:

  • (Boolean)


393
# File 'lib/contracts.rb', line 393

def pure? = mutation_policy == :pure

#required_change_boundsObject



402
403
404
# File 'lib/contracts.rb', line 402

def required_change_bounds
  @required_change_bounds || {}.freeze
end

#to_hObject



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: options }
end

#to_jsonObject



390
# File 'lib/contracts.rb', line 390

def to_json(*) = JSON.generate(to_h)