Resty v0.7.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

Functions

Clone the given resource

Is the given resource new? (not persisted)

Has the given resource been persisted?

Build a URL to the resource

Build a URL to the resource

Build a URL to the resource

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 relations their ids and persisted states won’t be updated.

iex> Fakes.Post
...> |> Resty.Repo.first!()
...> |> Resty.Resource.clone()
%Fakes.Post{id: nil, name: "test1"}

Is the given resource new? (not persisted)

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

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

Has the given resource been persisted?

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

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

Build a URL to the resource.

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

iex> Fakes.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> Fakes.Post |> Resty.Resource.url_for(key: "value")
"site.tld/posts?key=value"

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

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

Build a URL to the resource.

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