class Shamu::JsonApi::ResourceBuilder

Used by a {Serilaizer} to write fields and relationships

Public Instance Methods

attribute( name_or_hash, value = nil ) click to toggle source

@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]

# 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
Also aliased as: attributes
attributes( name_or_hash, value = nil )
Alias for: attribute
relationship( name ) { |builder| ... } click to toggle source

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.
# 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

Private Instance Methods

add_attribute( name, value ) click to toggle source
# File lib/shamu/json_api/resource_builder.rb, line 62
def add_attribute( name, value )
  return unless context.include_field?( type, name )

  attributes = ( output[:attributes] ||= {} )
  attributes[ name.to_sym ] = value
end