Class: Contracts::MethodReference

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

Overview

MethodReference represents original method reference that was decorated by contracts.ruby. Used for instance methods.

Direct Known Subclasses

SingletonMethodReference

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, method) ⇒ MethodReference

name - name of the method method - method object



9
10
11
12
# File 'lib/contracts/method_reference.rb', line 9

def initialize(name, method)
  @name = name
  @method = method
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/contracts/method_reference.rb', line 5

def name
  @name
end

Instance Method Details

#make_alias(this) ⇒ Object

Aliases original method to a special unique name, which is known only to this class. Usually done right before re-defining the method.



31
32
33
34
35
36
37
38
# File 'lib/contracts/method_reference.rb', line 31

def make_alias(this)
  _aliased_name = aliased_name
  original_name = name

  alias_target(this).class_eval do
    alias_method _aliased_name, original_name
  end
end

#make_definition(this, &blk) ⇒ Object

Makes a method re-definition in proper way



20
21
22
23
24
25
26
# File 'lib/contracts/method_reference.rb', line 20

def make_definition(this, &blk)
  is_private = private?(this)
  is_protected = protected?(this)
  alias_target(this).send(:define_method, name, &blk)
  make_private(this) if is_private
  make_protected(this) if is_protected
end

#method_positionObject

Returns method_position, delegates to Support.method_position



15
16
17
# File 'lib/contracts/method_reference.rb', line 15

def method_position
  Support.method_position(@method)
end

#send_to(this, *args, &blk) ⇒ Object

Calls original method on specified ‘this` argument with specified arguments `args` and block `&blk`.



42
43
44
# File 'lib/contracts/method_reference.rb', line 42

def send_to(this, *args, &blk)
  this.send(aliased_name, *args, &blk)
end