class Dekorator::Base
Base
decorator.
Public Class Methods
Guess and returns the decorated object class.
@return [Class] the decorated object class.
# File lib/dekorator.rb, line 65 def base_class _safe_constantize(name.sub("Decorator", "")) end
Decorate an object with a decorator.
@param object_or_enumerable [Object, Enumerable] the object or Enumerable to decorate. @param with [Class] the decorator class to use. If empty a decorator will be guessed.
@return [Dekorator::Base] if object given. @return [Enumerable] if Enumerable given.
@raise [DecoratorNotFound] if decorator is not found.
# 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
Define that an association must be decorated.
@param relation_name [String, Symbol] the association name to decorate. @param with [Class] the decorator class to use. If empty a decorator will be guessed.
@example 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
# File lib/dekorator.rb, line 54 def decorates_association(relation_name, with: :__guess__) relation_name = relation_name.to_sym define_method(relation_name) do @_decorated_associations[relation_name] ||= decorate(__getobj__.public_send(relation_name), with: with) end end
Decorate an object
@param object [Object] object to decorate.
# File lib/dekorator.rb, line 117 def initialize(object) @_decorated_associations = {} super(object) end
Private Class Methods
@api private
# File lib/dekorator.rb, line 72 def _decorate(object_or_enumerable, with: nil) with = _guess_decorator(object_or_enumerable) if with.nil? || with == :__guess__ if object_or_enumerable.is_a? Enumerable object_or_enumerable.lazy.map { |object| _decorate(object, with: with) } else with.new(object_or_enumerable) end end
@api private
# File lib/dekorator.rb, line 107 def _decorator_class return nil if self == Dekorator::Base self end
@api private
# File lib/dekorator.rb, line 83 def _guess_decorator(object_or_enumerable) object_or_enumerable = object_or_enumerable.first if object_or_enumerable.is_a? Enumerable _safe_constantize("#{object_or_enumerable.class}Decorator") \ || raise(DecoratorNotFound, "Can't guess decorator for #{object_or_enumerable.class} object") end
@api private
# File lib/dekorator.rb, line 100 def _safe_constantize(class_name) Object.const_get(class_name) rescue NameError => _ nil end
@api private
# File lib/dekorator.rb, line 91 def decorable?(object_or_enumerable) return false if object_or_enumerable.respond_to?(:empty?) && object_or_enumerable.empty? return false if !object_or_enumerable return false if object_or_enumerable.is_a?(Dekorator::Base) true end
Public Instance Methods
Decorate an object with a decorator.
@param object_or_enumerable [Object, Enumerable] the object or Enumerable to decorate. @param with [Class] the decorator class to use. If empty a decorator will be guessed.
@return [Dekorator::Base] if object given. @return [Enumerable] if Enumerable given.
@raise [DecoratorNotFound] if decorator is not found.c
# File lib/dekorator.rb, line 132 def decorate(object_or_enumerable, with: :__guess__) self.class.decorate(object_or_enumerable, with: with) end
Returns the decorated object.
@return [Object] the decorated object.
# File lib/dekorator.rb, line 139 def object __getobj__ end