ratchet v0.3.2 Ratchet.Data
Handles Ratchet data during EEx rendering
Summary
Functions
Extract attributes from a data property
Extract content from a data property
Determines if the given data provides plain text content
Prepares data for list comprehension
Get the specified property from the given data
Functions
Extract attributes from a data property
iex> Data.attributes({"", href: "https://google.com", rel: "nofollow"}, [])
{:safe, ~S(href="https://google.com" rel="nofollow")}
iex> Data.attributes([href: "/"], [{"data-prop", "link"}])
{:safe, ~S(href="/" data-prop="link")}
iex> Data.attributes([{"foo", href: "/"}], [{"data-prop", "link"}])
{:safe, ~S(data-prop="link")}
iex> Data.attributes("lolwat", [{"data-prop", "joke"}])
{:safe, ~S(data-prop="joke")}
Extract content from a data property
iex> Data.content("text")
"text"
iex> Data.content({"text", []})
"text"
Determines if the given data provides plain text content
iex> Data.content?("text")
true
iex> Data.content?({"text", href: "/foo/bar"})
true
iex> Data.content?([href: "/"])
false
iex> Data.content?(%{foo: "bar"})
false
iex> Data.content?({%{foo: "bar"}, action: "/baz"})
false
Prepares data for list comprehension
Ratchet must be able to consistently treat data as a list to facilitate rendering multiple elements. This function supports that requirement by ensuring elements are wrapped in a list.
iex> Data.prepare("data")
["data"]
iex> Data.prepare(["one", "two"])
["one", "two"]
iex> Data.prepare([href: "/"])
[[href: "/"]]
iex> Data.prepare([{"foo", href: "/"}])
[{"foo", href: "/"}]
iex> Data.prepare({"foo", class: "btn"})
[{"foo", class: "btn"}]
iex> Data.prepare(nil)
[nil]
Get the specified property from the given data
Data is defined in the following forms:
- A map of property keys to data values
- A tuple who's first element is such a map and second element is data attributes
- Something else...
This function provides a consistent interface for fetching a property from some body of data.
iex> Data.property(%{}, :foo)
nil
iex> Data.property(%{foo: "bar"}, :foo)
"bar"
iex> Data.property({%{foo: "bar"}, []}, :foo)
"bar"
iex> Data.property({"Content", []}, :foo)
nil
iex> Data.property([attr: "value"], :foo)
nil