Class: Dekorator::Base
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Dekorator::Base
- Defined in:
- lib/dekorator.rb
Overview
Base decorator.
Class Method Summary collapse
-
.base_class ⇒ Class
Guess and returns the decorated object class.
-
.decorate(object_or_enumerable, with: nil) ⇒ Dekorator::Base, Enumerable
Decorate an object with a decorator.
-
.decorates_association(relation_name, with: :__guess__) ⇒ Object
Define that an association must be decorated.
Instance Method Summary collapse
-
#decorate(object_or_enumerable, with: :__guess__) ⇒ Dekorator::Base, Enumerable
Decorate an object with a decorator.
-
#initialize(object) ⇒ Base
constructor
Decorate an object.
-
#object ⇒ Object
Returns the decorated object.
Constructor Details
#initialize(object) ⇒ Base
Decorate an object
121 122 123 124 125 |
# File 'lib/dekorator.rb', line 121 def initialize(object) @_decorated_associations = {} super(object) end |
Class Method Details
.base_class ⇒ Class
Guess and returns 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.
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.
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.
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 |
#object ⇒ Object
Returns the decorated object.
143 144 145 |
# File 'lib/dekorator.rb', line 143 def object __getobj__ end |