class Draper::Factory::Worker

@private

Attributes

decorator_class[R]
object[R]

Public Class Methods

new(decorator_class, object) click to toggle source
# File lib/draper/factory.rb, line 40
def initialize(decorator_class, object)
  @decorator_class = decorator_class
  @object = object
end

Public Instance Methods

call(options) click to toggle source
# File lib/draper/factory.rb, line 45
def call(options)
  update_context options
  decorator.call(object, options)
end
decorator() click to toggle 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

collection?() click to toggle source
# File lib/draper/factory.rb, line 77
def collection?
  object.respond_to?(:first) && !object.is_a?(Struct)
end
decoratable?() click to toggle source
# File lib/draper/factory.rb, line 81
def decoratable?
  object.respond_to?(:decorate)
end
decorator_method(klass) click to toggle 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
object_decorator() click to toggle 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
update_context(options) click to toggle 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