Module: Middlewares::Decorator::Representable

Defined in:
lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/middlewares/decorator/representable.rb

Class Method Summary collapse

Class Method Details

.decorate(object, options) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/middlewares/decorator/representable.rb', line 13

def self.decorate(object, options)
  decorator = "::Decorators::#{options[:decorator].to_s.camelize}".constantize

  if options[:collection?]
    decorator.for_collection.new(object.to_a).to_hash
  else
    decorator.new(object).to_hash
  end
end

.perform(request, response, options) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/endpoint_flux/tasks/endpoint_flux/generators/endpoint_flux/middlewares/decorator/representable.rb', line 4

def self.perform(request, response, options)
  resource_name = options[:wrapped_in] || options[:decorator]
  resource      = response.body[resource_name]

  response.body[resource_name] = decorate(resource, options) if resource

  [request, response]
end