Heim  >  Fragen und Antworten  >  Hauptteil

ruby/grape问题

想问一下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中文网2712 Tage vor718

Antworte allen(1)Ich werde antworten

  • 迷茫

    迷茫2017-04-22 09:01:04

    • present 类似于 rails 的 render

      class XxxEntity
        expose foo
      end
      
      present value, with: XxxEntity
      // output:
      //   { foo: 'xxx' }
      
    • with 就是指定数据用哪一个 Entity 来处理,你可以把 Entity 看做 json 的 erb,他提供了 json 的模板。

    • 至于 type 不是 present 的参数,而是是传给 Entity 的自定义参数,会传递到 Entity 内部,做一些处理。例如决定某个属性需不需要显示,或者传给 expose 的 block 作为 options 参数,可参考 README https://github.com/intridea/grape-entity#conditional-exposure

    Antwort
    0
  • StornierenAntwort