Class: Contracts::Base

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

Overview

A Base contract.

Direct Known Subclasses

Expects, Nothrows, Returns

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#methodObject

contains the method once the contract is initialized, which happens in the Class#singleton_,method_added callback.



120
121
122
# File 'lib/contracts.rb', line 120

def method
  @method
end

Instance Method Details

#+@Object

This is the unary “+” operator. It adds the contract to the current_contracts array.



113
114
115
# File 'lib/contracts.rb', line 113

def +@
  Contracts.current_contracts << self
end

#method_nameObject

Returns a description of the method; i.e. Class#name or Class.name



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/contracts.rb', line 124

def method_name
  if method.is_a?(Method)                               # A singleton method?
    # The method owner is the singleton class of the class. Sadly, the
    # the singleton class has no name; hence we try to construct the name
    # from its to_s description.
    klass_name = method.owner.to_s.gsub(/#<(.*?):(.*)>/, "\\2")
    "#{klass_name}.#{method.name}"
  else
    "#{method.owner}##{method.name}"
  end
end