Class: Dekorator::Base

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

Overview

Base decorator.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Base

Decorate an object

Parameters:

  • object (Object)

    object to decorate.



121
122
123
124
125
# File 'lib/dekorator.rb', line 121

def initialize(object)
  @_decorated_associations = {}

  super(object)
end

Class Method Details

.base_classClass

Guess and returns the decorated object class.

Returns:

  • (Class)

    the decorated object class.



65
66
67
# File 'lib/dekorator.rb', line 65

def base_class
  _safe_constantize(name.sub("Decorator", ""))
end

.decorate(object_or_enumerable, with: nil) ⇒ Dekorator::Base, Enumerable

Decorate an object with a decorator.

Parameters:

  • object_or_enumerable (Object, Enumerable)

    the object or Enumerable to decorate.

  • with (Class) (defaults to: nil)

    the decorator class to use. If empty a decorator will be guessed.

Returns:

  • (Dekorator::Base)

    if object given.

  • (Enumerable)

    if Enumerable given.

Raises:



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

def decorate(object_or_enumerable, with: nil)
  return object_or_enumerable unless decorable?(object_or_enumerable)

  with ||= _decorator_class

  object_or_enumerable = _decorate(object_or_enumerable, with: with)

  if block_given?
    yield object_or_enumerable
  else
    object_or_enumerable
  end
end

.decorates_association(relation_name, with: :__guess__) ⇒ Object

Define that an association must be decorated.

Examples:

Define an association to decorate

class UserDecorator < Dekorator::Base
  decorates_association :posts
end

# A decorator could be precise
class UserDecorator < Dekorator::Base
  decorates_association :posts, PostDecorator
end

Parameters:

  • relation_name (String, Symbol)

    the association name to decorate.

  • with (Class) (defaults to: :__guess__)

    the decorator class to use. If empty a decorator will be guessed.



54
55
56
57
58
59
60
# File 'lib/dekorator.rb', line 54

def decorates_association(relation_name, with: :__guess__)
  relation_name = relation_name.to_sym

  define_method(:"decorated_#{relation_name}") do
    @_decorated_associations[relation_name] ||= decorate(__getobj__.public_send(relation_name), with: with)
  end
end

Instance Method Details

#decorate(object_or_enumerable, with: :__guess__) ⇒ Dekorator::Base, Enumerable

Decorate an object with a decorator.

Parameters:

  • object_or_enumerable (Object, Enumerable)

    the object or Enumerable to decorate.

  • with (Class) (defaults to: :__guess__)

    the decorator class to use. If empty a decorator will be guessed.

Returns:

  • (Dekorator::Base)

    if object given.

  • (Enumerable)

    if Enumerable given.

Raises:



136
137
138
# File 'lib/dekorator.rb', line 136

def decorate(object_or_enumerable, with: :__guess__)
  self.class.decorate(object_or_enumerable, with: with)
end

#objectObject

Returns the decorated object.

Returns:

  • (Object)

    the decorated object.



143
144
145
# File 'lib/dekorator.rb', line 143

def object
  __getobj__
end