class Draper::Factory::Worker
@private
Attributes
Public Class Methods
Source
# File lib/draper/factory.rb, line 40 def initialize(decorator_class, object) @decorator_class = decorator_class @object = object end
Public Instance Methods
Source
# File lib/draper/factory.rb, line 45 def call(options) update_context options decorator.call(object, options) end
Source
# File lib/draper/factory.rb, line 50 def decorator return decorator_method(decorator_class) if decorator_class return object_decorator if decoratable? return decorator_method(Draper::CollectionDecorator) if collection? raise Draper::UninferrableDecoratorError.new(object.class) end
Private Instance Methods
Source
# File lib/draper/factory.rb, line 77 def collection? object.respond_to?(:first) && !object.is_a?(Struct) end
Source
# File lib/draper/factory.rb, line 81 def decoratable? object.respond_to?(:decorate) end
Source
# File lib/draper/factory.rb, line 69 def decorator_method(klass) if collection? && klass.respond_to?(:decorate_collection) klass.method(:decorate_collection) else klass.method(:decorate) end end
Source
# File lib/draper/factory.rb, line 61 def object_decorator if collection? ->(object, options) { object.decorator_class.decorate_collection(object, options.reverse_merge(with: nil))} else ->(object, options) { object.decorate(options) } end end
Source
# File lib/draper/factory.rb, line 85 def update_context(options) args = options.delete(:context_args) options[:context] = options[:context].call(*Array.wrap(args)) if options[:context].respond_to?(:call) end