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
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
A resource primary key.
A resource struct (also called a resource).
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{}
}
Return the primary_key of the given resource.
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
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.
iex> Post |> Resty.Resource.url_for()
"site.tld/posts"
iex> Post.build(id: 1) |> Resty.Resource.url_for()
"site.tld/posts/1"
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"
Build a URL to the resource.
iex> Post |> Resty.Resource.url_for("slug", key: "value")
"site.tld/posts/slug?key=value"