Module: JSONAPI::Serializable::Relationship::DSL

Included in:
JSONAPI::Serializable::Relationship
Defined in:
lib/jsonapi/serializable/relationship/dsl.rb

Instance Method Summary collapse

Instance Method Details

#dataObject

Declare the related resources for this relationship. end

Examples:

data do
  @user.posts.map { |p| PostResource.new(post: p) }
end
data do
  @post.author && UserResource.new(user: @user.author)
end
data do
  @user.posts
end

Yield Returns:

  • The related resources for this relationship. If it is nil, an object implementing the Serializable::Resource interface, an empty array, or an array of objects implementing the Serializable::Resource interface, then it is used as is. Otherwise an appropriate Serializable::Resource subclass is inferred from the object(s)' namespace/class, the `class` relationship option, and the @_resource_builder.


31
32
33
34
35
36
37
# File 'lib/jsonapi/serializable/relationship/dsl.rb', line 31

def data
  # NOTE(beauby): Lazify computation since it is only needed when
  #   the corresponding relationship is included.
  @_resources_block = proc do
    @_resource_builder.build(yield, @_exposures, @_options[:class])
  end
end

Declare a link for this relationship. The properties of the link are set

by providing a block in which the DSL methods of
+JSONAPI::Serializable::Link+ are called.

Examples:

link(:self) do
  "http://api.example.com/users/#{@user.id}/relationships/posts"
end
link(:related) do
  href "http://api.example.com/users/#{@user.id}/posts"
  meta authorization_needed: true
end

Parameters:

  • name (Symbol)

    The key of the link.

Yield Returns:

  • (Hash, String, nil)

    The block to compute the value, if any.

See Also:

  • JSONAPI::Serialiable::Link

95
96
97
# File 'lib/jsonapi/serializable/relationship/dsl.rb', line 95

def link(name, &block)
  @_links[name] = Link.as_jsonapi(@_exposures, &block)
end

#linkage(options = {}, &block) ⇒ Object #linkage(options = {}) ⇒ Object

Overloads:

  • #linkage(options = {}, &block) ⇒ Object

    Explicitly declare linkage data.

    Examples:

    linkage do
      @object.posts.map { |p| { id: p.id.to_s, type: 'posts' } }
    end

    Yield Returns:

    • The resource linkage.

  • #linkage(options = {}) ⇒ Object

    Forces standard linkage even if relationship not included.

    Examples:

    linkage always: true

53
54
55
56
# File 'lib/jsonapi/serializable/relationship/dsl.rb', line 53

def linkage(always: false, &block)
  @_include_linkage = always
  @_linkage_block = block
end

#meta(value) ⇒ Object #meta(&block) ⇒ Object

Overloads:

  • #meta(value) ⇒ Object

    Declare the meta information for this relationship.

    Examples:

    meta paginated: true

    Parameters:

    • value (Hash)

      The meta information hash.

  • #meta(&block) ⇒ Object

    Declare the meta information for this relationship.

    Examples:

    meta do
      { paginated: true }
    end

    Yield Returns:

    • (Hash)

      The meta information hash.


73
74
75
# File 'lib/jsonapi/serializable/relationship/dsl.rb', line 73

def meta(value = nil)
  @_meta = value || yield
end