Sha256: 8b6efde6c5241a4629ed158e4180275d0d58a34965c4a267a6d4032a0c4a9192

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Shamu
  module JsonApi

    # Presenters are responsible for projecting an {Entities::Entity} or PORO
    # to a well-formatted JSON API {ResourceBuilder builder} response.
    #
    # {Presenter} delegates all of the {ResourceBuilder} methods for convenient
    # syntax.
    #
    # ```
    # class UserPresenter < ApplicationPresenter
    #   def present
    #     identifier :user, resource.id
    #
    #     attributes name: resource.name,
    #                email: resource.email
    #
    #     relationship( :address ) do |rel|
    #       rel.identifier :address, resource.address_id
    #       rel.link :related, user_address_url( resource, resource.address_id )
    #     end
    #   end
    # end
    # ```
    class Presenter

      # @param [Object] resource to presenter.
      # @param [ResourceBuilder] builder used to build the JSON API response.
      def initialize( resource, builder )
        @resource = resource
        @builder  = builder
      end

      # Serialize the `resource` to the `builder`.
      #
      # @return [void]
      def present
        fail NotImplementedError
      end

      private

        delegate :relationship, :attribute, :attributes, :link, :identifier, :meta, to: :builder

        attr_reader :resource
        attr_reader :builder

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shamu-0.0.9 lib/shamu/json_api/presenter.rb
shamu-0.0.8 lib/shamu/json_api/presenter.rb
shamu-0.0.7 lib/shamu/json_api/presenter.rb
shamu-0.0.5 lib/shamu/json_api/presenter.rb