Sha256: ce4eabb9bd016e1a2816f1ae82287329f25a690ee30e5a5ff7eb8c26b4ad0979

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module Shamu
  module JsonApi
    module BuilderMethods
      module Identifier
        # Write a resource linkage info.
        #
        # @param [String] type of the resource.
        # @param [Object] id of the resource.
        # @return [self]
        def identifier( type, id = :not_set )
          output[:type] = @type = json_type( type )

          output[:id] =
            if id == :not_set
              type.id if type.respond_to?( :id )
            else
              id.to_s
            end

          self
        end

        # (see BaseBuilder#compile)
        def compile
          require_identifier!
          super
        end

        private

          attr_reader :type

          def json_type( type )
            type = type.json_type                  if type.respond_to?( :json_type )
            type = type.model_name.element         if type.respond_to?( :model_name )
            type = type.name.demodulize.underscore if type.is_a?( Module )

            type
          end

          def require_identifier!
            fail IncompleteResourceError unless type
          end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shamu-0.0.11 lib/shamu/json_api/builder_methods/identifier.rb