class Draper::Factory
Attributes
decorator_class[R]
default_options[R]
Public Class Methods
new(options = {})
click to toggle source
Creates a decorator factory.
@option options [Decorator, CollectionDecorator] :with (nil)
decorator class to use. If nil, it is inferred from the object passed to {#decorate}.
@option options [Hash, call] context
extra data to be stored in created decorators. If a proc is given, it will be called each time {#decorate} is called and its return value will be used as the context.
# File lib/draper/factory.rb, line 12 def initialize(options = {}) options.assert_valid_keys(:with, :context) @decorator_class = options.delete(:with) @default_options = options end
Public Instance Methods
decorate(object, options = {})
click to toggle source
Decorates an object, inferring whether to create a singular or collection decorator from the type of object passed.
@param [Object] object
object to decorate.
@option options [Hash] context
extra data to be stored in the decorator. Overrides any context passed to the constructor.
@option options [Object, Array] context_args (nil)
argument(s) to be passed to the context proc.
@return [Decorator, CollectionDecorator] the decorated object.
# File lib/draper/factory.rb, line 29 def decorate(object, options = {}) return nil if object.nil? Worker.new(decorator_class, object).call(options.reverse_merge(default_options)) end