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.



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 = 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.



294
295
296
# File 'lib/contracts.rb', line 294

def allowed_exceptions
  @allowed_exceptions
end

#examplesObject (readonly)

Returns the value of attribute examples.



294
295
296
# File 'lib/contracts.rb', line 294

def examples
  @examples
end

#idObject (readonly)

Returns the value of attribute id.



294
295
296
# File 'lib/contracts.rb', line 294

def id
  @id
end

#invariantsObject (readonly)

Returns the value of attribute invariants.



294
295
296
# File 'lib/contracts.rb', line 294

def invariants
  @invariants
end

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

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

Returns the value of attribute method_type.



294
295
296
# File 'lib/contracts.rb', line 294

def method_type
  @method_type
end

#mutation_policyObject (readonly)

Returns the value of attribute mutation_policy.



294
295
296
# File 'lib/contracts.rb', line 294

def mutation_policy
  @mutation_policy
end

#observedObject (readonly)

Returns the value of attribute observed.



294
295
296
# File 'lib/contracts.rb', line 294

def observed
  @observed
end

#optionsObject (readonly)

Returns the value of attribute options.



294
295
296
# File 'lib/contracts.rb', line 294

def options
  @options
end

#ownerObject (readonly)

Returns the value of attribute owner.



294
295
296
# File 'lib/contracts.rb', line 294

def owner
  @owner
end

#parametersObject

Returns the value of attribute parameters.



294
295
296
# File 'lib/contracts.rb', line 294

def parameters
  @parameters
end

#positionalsObject

Returns the value of attribute positionals.



294
295
296
# File 'lib/contracts.rb', line 294

def positionals
  @positionals
end

#postconditionsObject (readonly)

Returns the value of attribute postconditions.



294
295
296
# File 'lib/contracts.rb', line 294

def postconditions
  @postconditions
end

#preconditionsObject (readonly)

Returns the value of attribute preconditions.



294
295
296
# File 'lib/contracts.rb', line 294

def preconditions
  @preconditions
end

#required_changesObject (readonly)

Returns the value of attribute required_changes.



294
295
296
# File 'lib/contracts.rb', line 294

def required_changes
  @required_changes
end

#return_constraintObject

Returns the value of attribute return_constraint.



294
295
296
# File 'lib/contracts.rb', line 294

def return_constraint
  @return_constraint
end

#snapshot_blockObject (readonly)

Returns the value of attribute snapshot_block.



294
295
296
# File 'lib/contracts.rb', line 294

def snapshot_block
  @snapshot_block
end

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



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

def all_invariants = Contracts.invariants_for(owner)

#inherited_invariantsObject



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

def inherited_invariants = all_invariants - own_invariants

#observed_fieldsObject



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

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

#own_invariantsObject



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

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

#permitted_changesObject



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

Returns:

  • (Boolean)


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

def pure? = mutation_policy == :pure

#to_hObject



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

#to_jsonObject



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

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