Class: Shamu::JsonApi::ResourceBuilder
- Inherits:
-
BaseBuilder
- Object
- BaseBuilder
- Shamu::JsonApi::ResourceBuilder
- 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
-
#attribute(name_or_hash, value = nil)
(also: #attributes)
Write one or more attributes to the output.
-
#relationship(name) {|builder| ... }
Build a relationship reference.
Methods included from BuilderMethods::Identifier
Methods inherited from BaseBuilder
Methods included from BuilderMethods::Meta
Methods included from BuilderMethods::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.
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
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 |