module Grape::Entity::DSL::ClassMethods

Public Instance Methods

entity(*exposures, &block) click to toggle source

Call this to make exposures to the entity for this Class. Can be called with symbols for the attributes to expose, a block that yields the full Entity DSL (See Grape::Entity), or both.

@example Symbols only.

class User
  include Grape::Entity::DSL

  entity :name, :email
end

@example Mixed.

class User
  include Grape::Entity::DSL

  entity :name, :email do
    expose :latest_status, using: Status::Entity, if: :include_status
    expose :new_attribute, if: { version: 'v2' }
  end
end
# File lib/grape_entity/entity.rb, line 90
def entity(*exposures, &block)
  entity_class.expose(*exposures) if exposures.any?
  entity_class.class_eval(&block) if block_given?
  entity_class
end
entity_class(search_ancestors = true) click to toggle source

Returns the automatically-created entity class for this Class.

# File lib/grape_entity/entity.rb, line 61
def entity_class(search_ancestors = true)
  klass = const_get(:Entity) if const_defined?(:Entity)
  klass ||= ancestors.detect { |a| a.entity_class(false) if a.respond_to?(:entity_class) } if search_ancestors
  klass
end