Method: Grape::DSL::RequestResponse::ClassMethods#represent

Defined in:
lib/grape/dsl/request_response.rb

#represent(model_class, options) ⇒ Object

Allows you to specify a default representation entity for a class. This allows you to map your models to their respective entities once and then simply call ‘present` with the model.

Note that Grape will automatically go up the class ancestry to try to find a representing entity, so if you, for example, define an entity to represent ‘Object` then all presented objects will bubble up and utilize the entity provided on that `represent` call.

Examples:

class ExampleAPI < Grape::API
  represent User, with: Entity::User

  get '/me' do
    present current_user # with: Entity::User is assumed
  end
end

Parameters:

  • model_class (Class)

    The model class that will be represented.

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :with (Class)

    The entity class that will represent the model.

Raises:



154
155
156
157
# File 'lib/grape/dsl/request_response.rb', line 154

def represent(model_class, options)
  raise Grape::Exceptions::InvalidWithOptionForRepresent.new unless options[:with] && options[:with].is_a?(Class)
  namespace_stackable(:representations, model_class => options[:with])
end