Module: Grape::Entity::DSL::ClassMethods

Defined in:
lib/grape_entity/entity.rb

Instance Method Summary collapse

Instance Method Details

#entity(*exposures, &block) ⇒ Object

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.

Examples:

Symbols only.


class User
  include Grape::Entity::DSL

  entity :name, :email
end

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

88
89
90
91
92
# File 'lib/grape_entity/entity.rb', line 88

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) ⇒ Object

Returns the automatically-created entity class for this Class.


59
60
61
62
63
# File 'lib/grape_entity/entity.rb', line 59

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