ratchet v0.3.0 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

attributes(data_attrs, elem_attrs)

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")}
content(text)

Extract content from a data property

iex> Data.content("text")
"text"
iex> Data.content({"text", []})
"text"
content?(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
prepare(data)

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]
property(map, property)

Get the specified property from the given data

Data is defined in the following forms:

  1. A map of property keys to data values
  2. A tuple who's first element is such a map and second element is data attributes
  3. 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