Resty v0.12.0 Resty.Resource View Source

This module provides a few functions to work with resource structs. Resource structs are created thanks to the Resty.Resource.Base module.

Link to this section Summary

Types

A resource module

A resource primary key

t()

A resource struct (also called a resource)

Parameters used when building an url

Functions

Clone the given resource

Return the primary_key of the given resource

Mark the given resource as persisted

Is the given resource new? (not persisted)

Has the given resource been persisted?

Return the raw attributes of the resource. These attributes are what will eventually get sent to the underlying api

Build a URL to the resource

Build a URL to the resource

Build a URL to the resource

Link to this section Types

A resource module.

For the resource %Post{} the resource module would be Post

Link to this type primary_key() View Source
primary_key() :: any()

A resource primary key.

A resource struct (also called a resource).

Link to this type url_parameters() View Source
url_parameters() :: Keyword.t()

Parameters used when building an url.

Link to this section Functions

Clone the given resource

This will create a new resource struct from the given one. The new struct will be marked as not persisted and will not have an id.

This is not deep cloning. If there are associations their ids and persisted states won’t be updated.

iex> Post
...> |> Resty.Repo.first!()
...> |> Resty.Resource.clone()
%Post{
  id: nil,
  body: "lorem ipsum",
  name: "First Post",
  author_id: 1,
  author: %Resty.Associations.NotLoaded{}
}
Link to this function get_primary_key(resource) View Source

Return the primary_key of the given resource.

Link to this function mark_as_persisted(resource) View Source

Mark the given resource as persisted

iex> Post.build() …> |> Resty.Resource.mark_as_persisted() …> |> Resty.Resource.persisted?() true

Is the given resource new? (not persisted)

iex> Post.build()
...> |> Resty.Resource.new?()
true

iex> Post.build()
...> |> Resty.Repo.save!()
...> |> Resty.Resource.new?()
false

Has the given resource been persisted?

iex> Post.build()
...> |> Resty.Resource.persisted?()
false

iex> Post.build()
...> |> Resty.Repo.save!()
...> |> Resty.Resource.persisted?()
true
Link to this function raw_attributes(resource) View Source

Return the raw attributes of the resource. These attributes are what will eventually get sent to the underlying api.

Link to this function url_for(module_or_resource) View Source

Build a URL to the resource.

iex> Post |> Resty.Resource.url_for()
"site.tld/posts"

iex> Post.build(id: 1) |> Resty.Resource.url_for()
"site.tld/posts/1"
Link to this function url_for(module_or_resource, id_or_params) View Source

Build a URL to the resource.

iex> Post |> Resty.Resource.url_for(key: "value")
"site.tld/posts?key=value"

iex> Post.build(id: 1) |> Resty.Resource.url_for(key: "value")
"site.tld/posts/1?key=value"

iex> Post |> Resty.Resource.url_for("slug")
"site.tld/posts/slug"
Link to this function url_for(resource_module, resource_id, params) View Source

Build a URL to the resource.

iex> Post |> Resty.Resource.url_for("slug", key: "value")
"site.tld/posts/slug?key=value"