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

Defined in:
lib/grape/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


87
88
89
90
91
# File 'lib/grape/entity.rb', line 87

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.



58
59
60
61
62
# File 'lib/grape/entity.rb', line 58

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