Class: Contracts::MethodReference
- Inherits:
-
Object
- Object
- Contracts::MethodReference
- 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
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, method) ⇒ MethodReference
constructor
name - name of the method method - method object.
-
#make_alias(this) ⇒ Object
Aliases original method to a special unique name, which is known only to this class.
-
#make_definition(this, &blk) ⇒ Object
Makes a method re-definition in proper way.
-
#make_private(this) ⇒ Object
Makes a method private.
-
#method_position ⇒ Object
Returns method_position, delegates to Support.method_position.
-
#send_to(this, *args, &blk) ⇒ Object
Calls original method on specified ‘this` argument with specified arguments `args` and block `&blk`.
Constructor Details
#initialize(name, method) ⇒ MethodReference
name - name of the method method - method object
10 11 12 13 |
# File 'lib/contracts/method_reference.rb', line 10 def initialize(name, method) @name = name @method = method end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/contracts/method_reference.rb', line 6 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.
34 35 36 37 38 39 40 41 |
# File 'lib/contracts/method_reference.rb', line 34 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
21 22 23 |
# File 'lib/contracts/method_reference.rb', line 21 def make_definition(this, &blk) alias_target(this).send(:define_method, name, &blk) end |
#make_private(this) ⇒ Object
Makes a method private
26 27 28 29 |
# File 'lib/contracts/method_reference.rb', line 26 def make_private(this) original_name = name alias_target(this).class_eval { private original_name } end |
#method_position ⇒ Object
Returns method_position, delegates to Support.method_position
16 17 18 |
# File 'lib/contracts/method_reference.rb', line 16 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`.
45 46 47 |
# File 'lib/contracts/method_reference.rb', line 45 def send_to(this, *args, &blk) this.send(aliased_name, *args, &blk) end |