Class: Decorator

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

Overview

Base decorator class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(decorated_class, decorated_method) ⇒ Decorator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decorator constructor interface

Parameters:

  • decorated_class (Class)

    The class that is being decorated

  • decorated_method (Symbol)

    The method that is being decorated



39
40
41
42
# File 'lib/decorator.rb', line 39

def initialize(decorated_class, decorated_method)
  @decorated_class = decorated_class
  @decorated_method = decorated_method
end

Instance Attribute Details

#decorated_classSymbol (readonly)

The method being decorated

Examples:

decorator.decorated_method #=> Symbol

Returns:

  • (Symbol)


16
17
18
# File 'lib/decorator.rb', line 16

def decorated_class
  @decorated_class
end

#decorated_methodSymbol (readonly)

The method being decorated

Examples:

decorator.decorated_method #=> Symbol

Returns:

  • (Symbol)


28
29
30
# File 'lib/decorator.rb', line 28

def decorated_method
  @decorated_method
end

Instance Method Details

#call_next(this, chain, *args) ⇒ Object

The hook to call chained decorations

Examples:

class DecoratorDemo < Decorator
  def call(this, chain, *args)
    puts "'#{decorated_class}' has '#{decorated_method.name}' decorated"
    call_next(this, chain, *args)
  end
end

Parameters:

  • this (Instance)

    the instance of the object with the wrapper method

  • chain (Array<Decorator>)

    the remaining decorators to be called

  • *args (Array<Object>)

    the arguments to pass to the wrapper method

Returns:

  • (Object)

    the return value of the next_caller



62
63
64
65
66
67
68
69
70
# File 'lib/decorator.rb', line 62

def call_next(this, chain, *args)
  next_caller = chain.shift

  if next_caller.nil?
    decorated_method.bind(this).call(*args)
  else
    next_caller.call(this, chain, *args)
  end
end