Class: Shamu::JsonApi::ResourceBuilder

Inherits:
BaseBuilder show all
Includes:
BuilderMethods::Identifier
Defined in:
lib/shamu/json_api/resource_builder.rb

Overview

Used by a Serilaizer to write fields and relationships

Instance Method Summary collapse

Methods included from BuilderMethods::Identifier

#compile, #identifier

Methods inherited from BaseBuilder

#compile, #initialize

Methods included from BuilderMethods::Meta

#meta

Methods included from BuilderMethods::Link

#link

Constructor Details

This class inherits a constructor from Shamu::JsonApi::BaseBuilder

Instance Method Details

#attribute(attributes) #attribute(name, value) Also known as: attributes

This method returns an undefined value.

Write one or more attributes to the output.

Overloads:

  • #attribute(attributes)

    Parameters:

    • attributes (Hash)

      to write.

  • #attribute(name, value)

    Parameters:

    • name (String, Symbol)

      of the attribute.

    • value (Object)

      that can be persited to a JSON primitive value.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shamu/json_api/resource_builder.rb', line 20

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

#relationship(name) {|builder| ... }

This method returns an undefined value.

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

Parameters:

  • name (String, Symbol)

    of the relationship.

Yields:

  • (builder)

Yield Parameters:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/shamu/json_api/resource_builder.rb', line 48

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