class Contracts::MethodReference

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

Attributes

name[R]

Public Class Methods

new(name, method) click to toggle source

name - name of the method method - method object

# File lib/contracts/method_reference.rb, line 9
def initialize(name, method)
  @name = name
  @method = method
end

Public Instance Methods

make_alias(this) click to toggle source

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

# 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) click to toggle source

Makes a method re-definition in proper way

# 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_position() click to toggle source

Returns #method_position, delegates to Contracts::Support.method_position

# File lib/contracts/method_reference.rb, line 15
def method_position
  Support.method_position(@method)
end
send_to(this, *args, &blk) click to toggle source

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

# File lib/contracts/method_reference.rb, line 42
def send_to(this, *args, &blk)
  this.send(aliased_name, *args, &blk)
end

Private Instance Methods

alias_target(this) click to toggle source

Returns alias target for instance methods, subject to be overriden in subclasses.

# File lib/contracts/method_reference.rb, line 70
def alias_target(this)
  this
end
aliased_name() click to toggle source
# File lib/contracts/method_reference.rb, line 74
def aliased_name
  @_original_name ||= construct_unique_name
end
construct_unique_name() click to toggle source
# File lib/contracts/method_reference.rb, line 78
def construct_unique_name
  :"__contracts_ruby_original_#{name}_#{Support.unique_id}"
end
make_private(this) click to toggle source

Makes a method private

# File lib/contracts/method_reference.rb, line 49
def make_private(this)
  original_name = name
  alias_target(this).class_eval { private original_name }
end
make_protected(this) click to toggle source

Makes a method protected

# File lib/contracts/method_reference.rb, line 63
def make_protected(this)
  original_name = name
  alias_target(this).class_eval { protected original_name }
end
private?(this) click to toggle source
# File lib/contracts/method_reference.rb, line 54
def private?(this)
  this.private_instance_methods.map(&:to_sym).include?(name)
end
protected?(this) click to toggle source
# File lib/contracts/method_reference.rb, line 58
def protected?(this)
  this.protected_instance_methods.map(&:to_sym).include?(name)
end