ruby/grape问题
PHP中文网
PHP中文网 2017-04-22 08:59:04
0
1
786

想问一下ruby grape里以下的present,with,type都是什么意思?看了小半天也没懂。谢谢。

module API
  class Statuses < Grape::API
    version 'v1'

    desc 'Statuses.', {
      params: API::Entities::Status.documentation
    }
    get '/statuses' do
      statuses = Status.all
      type = current_user.admin? ? :full : :default
      present statuses, with: API::Entities::Status, type: type
    end
  end
end

文档地址:https://github.com/intridea/grape-entity/blob/master/README.md

PHP中文网
PHP中文网

认证0级讲师

reply all(1)
迷茫
  • present is similar to rails’ render

    class XxxEntity
      expose foo
    end
    
    present value, with: XxxEntity
    // output:
    //   { foo: 'xxx' }
    
  • with is to specify which Entity is used to process the data. You can think of Entity as the erb of json, which provides the template of json.

  • As for type, it is not a parameter of present, but a custom parameter passed to Entity. It will be passed to the inside of Entity for some processing. For example, to decide whether a certain attribute needs to be displayed, or to pass the block of expose as a options parameter, please refer to README https://github.com/intridea/grape-entity#conditional-exposure

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template