Sha256: 572461c2452cc77cebf1cdbaa726ad4cf42785f7efb72e56bc87bd461041647c
Contents?: true
Size: 1.9 KB
Versions: 14
Compression:
Stored size: 1.9 KB
Contents
require "shamu/json_api/base_builder" module Shamu module JsonApi # Used by a {Serilaizer} to write fields and relationships class ResourceBuilder < BaseBuilder include BuilderMethods::Identifier # @overload attribute( attributes ) # @param [Hash] attributes to write. # @overload attribute( name, value ) # @param [String, Symbol] name of the attribute. # @param [Object] value that can be persited to a JSON primitive value. # # Write one or more attributes to the output. # # @return [void] def attribute( name_or_hash, value = nil ) require_identifier! if value add_attribute name_or_hash, value else name_or_hash.each do |n, v| add_attribute n, v end end end alias_method :attributes, :attribute # Build a relationship reference. # # ``` # relationship :author do |builder| # builder.identifier author # builder.link :related, author_url author # builder.link :self, book_author_url( book, author ) # end # ``` # # @param [String,Symbol] name of the relationship. # @return [void] # @yield (builder) # @yieldparam [RelationshipBuilder] builder used to define the properties # of the relationship. def relationship( name, &block ) require_identifier! return unless context.include_field?( type, name ) builder = RelationshipBuilder.new( context ) yield builder relationships = ( output[:relationships] ||= {} ) relationships[ name.to_sym ] = builder.compile end private def add_attribute( name, value ) return unless context.include_field?( type, name ) attributes = ( output[:attributes] ||= {} ) attributes[ name.to_sym ] = value end end end end
Version data entries
14 entries across 14 versions & 1 rubygems