Class: Contracts::MethodHandler

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

Overview

Handles class and instance methods addition Represents single such method

Constant Summary collapse

METHOD_REFERENCE_FACTORY =
{
  :class_methods => SingletonMethodReference,
  :instance_methods => MethodReference
}
RAW_METHOD_STRATEGY =
{
  :class_methods => lambda { |target, name| target.method(name) },
  :instance_methods => lambda { |target, name| target.instance_method(name) }
}

Instance Method Summary collapse

Constructor Details

#initialize(method_name, is_class_method, target) ⇒ MethodHandler

Creates new instance of MethodHandler

Parameters:

  • method_name (Symbol)
  • is_class_method (Bool)
  • target (Class)
    • class that method got added to



20
21
22
23
24
# File 'lib/contracts/method_handler.rb', line 20

def initialize(method_name, is_class_method, target)
  @method_name = method_name
  @is_class_method = is_class_method
  @target = target
end

Instance Method Details

#handleObject

Handles method addition



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/contracts/method_handler.rb', line 27

def handle
  return unless engine?
  return if decorators.empty?

  validate_decorators!
  validate_pattern_matching!

  engine.add_method_decorator(method_type, method_name, decorator)
  mark_pattern_matching_decorators
  method_reference.make_alias(target)
  redefine_method
end