Class: TinyDecorator::SingleDecorator

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Delegatable
Defined in:
lib/tiny_decorator/single_decorator.rb

Overview

Single decorator. Inherite, then define methods to use. This base decorator delegates all. To limit delegation to source object.

“‘ class NewDecorator < TinyDecorator::SingleDecorator

def new_method
  'decorated'
end

end

NewDecorator.decorate(Object.new).new_method # ‘decorated’ NewDecorator.decorate_collection([Object.new, Object.new]).new_method # ‘decorated’ “‘

Use in case source has only 1 decorator. In case more than 1 decorator or conditional decorate, we prefer using TinyDecorator::CompositeDecorator

Constant Summary

Constants included from Delegatable

Delegatable::DELEGATION_RESERVED_KEYWORDS, Delegatable::DELEGATION_RESERVED_METHOD_NAMES, Delegatable::RUBY_RESERVED_KEYWORDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegatable

delegate

Constructor Details

#initialize(delegatee, context = {}, preloaded = {}) ⇒ SingleDecorator

Initialize, use .decorate or .new are the same

Parameters:

  • delegatee (Object)

    source object to decorate

  • context (Hash) (defaults to: {})

    context as hash, could read within decorator scope by context[]



24
25
26
27
28
# File 'lib/tiny_decorator/single_decorator.rb', line 24

def initialize(delegatee, context = {}, preloaded = {})
  @context = context
  @preloaded = preloaded
  __setobj__(delegatee)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



29
30
31
# File 'lib/tiny_decorator/single_decorator.rb', line 29

def context
  @context
end

#preloadedObject (readonly)

Returns the value of attribute preloaded.



29
30
31
# File 'lib/tiny_decorator/single_decorator.rb', line 29

def preloaded
  @preloaded
end

Class Method Details

.decorate_collection(delegatees, context = {}) ⇒ Object

Decorate a collection, collection must extend Enum behavior

Parameters:

  • delegatees (Array)

    collection of source object to decorate

  • context (Hash) (defaults to: {})

    context as hash, could read within decorator scope by context[]



35
36
37
38
39
# File 'lib/tiny_decorator/single_decorator.rb', line 35

def decorate_collection(delegatees, context = {})
  delegatees.map do |delegatee|
    new(delegatee, context)
  end
end