Class: Contracts::Base

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

Overview

A Base contract.

Direct Known Subclasses

Expects, Nothrows, Returns, Runtime

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.



154
155
156
# File 'lib/contracts.rb', line 154

def method
  @method
end

Instance Method Details

#+@Object

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



147
148
149
# File 'lib/contracts.rb', line 147

def +@
  Contracts.current_contracts << self
end

#method_nameObject

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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/contracts.rb', line 158

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