Skip to content

Commit 0d08375

Browse files
Add documentation and typespecs (#32)
1 parent b55d0c8 commit 0d08375

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

lib/ex_cell/adapter.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
defmodule ExCell.Adapter do
2-
@moduledoc false
3-
@callback container(Map.t) :: {:safe, List.t}
2+
@moduledoc """
3+
An ExCell Adapter defines the way the adapter outputs its HTML. The adapter
4+
requires a function called adapter that accepts a map container name,
5+
attributes, params and the content. It should return a Phoenix.HTML safe string.
6+
"""
7+
8+
@type html :: {:safe, list()}
9+
10+
@type options :: %{
11+
required(:name) => String.t,
12+
required(:attributes) => list(),
13+
required(:params) => map(),
14+
required(:content) => String.t | {:safe, list()}
15+
}
16+
17+
@callback container(options :: options()) :: html()
418
end

lib/ex_cell/adapters/cell_js.ex

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
defmodule ExCell.Adapters.CellJS do
2-
@moduledoc false
2+
@moduledoc """
3+
The CellJS adapter can be used to output the cells as HTML compatible with
4+
[cells-js](https://github.com/DefactoSoftware/cells-js). CellsJS was written
5+
with ExCell in mind.
6+
7+
Tags are automatically closed when they are part of the
8+
[void elements](https://stackoverflow.com/questions/4693939/self-closing-tags-void-elements-in-html5)
9+
specification.
10+
11+
CellsJS are uses two predefined attributes to parse the Javascript. First it
12+
will look for the `data-cell` cell attribute and match it to the defined Cell
13+
in Javascript.
14+
15+
Second it will take the JSON arguments set on the `data-cell-params` attribute
16+
and use that to initialize the cell with user defined parameters.
17+
"""
318

419
@behaviour ExCell.Adapter
520

@@ -27,11 +42,19 @@ defmodule ExCell.Adapters.CellJS do
2742
def void_element?(tag) when is_atom(tag), do: void_element?(Atom.to_string(tag))
2843
def void_element?(tag), do: tag in void_elements()
2944

45+
@doc """
46+
The data_attribute function is used to build up the data attributes and set
47+
the default `data-cell` and `data-cell-params` attributes.
48+
"""
3049
def data_attribute(name, data \\ [], params \\ %{})
3150
def data_attribute(name, nil, params), do: data_attribute(name, [], params)
3251
def data_attribute(name, data, params) when is_list(data), do:
3352
Keyword.merge(data, cell: name, cell_params: Poison.encode!(params))
3453

54+
@doc """
55+
The attributes function is used to auto fill the attributes for a container
56+
with the data attributes.
57+
"""
3558
def attributes(name, attributes \\ [], params \\ %{}) do
3659
Keyword.put(
3760
attributes,
@@ -40,6 +63,10 @@ defmodule ExCell.Adapters.CellJS do
4063
)
4164
end
4265

66+
@doc """
67+
The container renders HTML with the attributes, class name and data attributes
68+
prefilled. The HTML is rendered with Phoenix.HTML.Tag.
69+
"""
4370
def container(%{
4471
name: name,
4572
attributes: attributes,

lib/ex_cell/ex_cell.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
defmodule ExCell do
2-
@moduledoc false
2+
@moduledoc """
3+
ExCell is used to split larger views in smaller cells that can be reused and
4+
easily packaged with Javascript and CSS.
5+
6+
A cell consists of a couple of files:
7+
```
8+
cells
9+
|- avatar
10+
| |- template.html.eex
11+
| |- view.html.eex
12+
| |- style.css (optional)
13+
| |- index.js (optional)
14+
|- header
15+
...
16+
```
17+
18+
You can render the cell in a view, controller or another cell by adding the following code:
19+
```ex
20+
cell(AvatarCell, class: "CustomClassName", user: %User{})
21+
```
22+
23+
This would generate the following HTML when you render the cell:
24+
25+
```html
26+
<span class="AvatarCell" data-cell="AvatarCell" data-cell-params="{}">
27+
<img src="/images/foo/avatar.jpg" class="AvatarCell-Image" alt="foo" />
28+
</span>
29+
```
30+
"""
331
@dialyzer [{:no_match, relative_name: 2}]
432

533
def module_relative_to(module, relative_to) do

0 commit comments

Comments
 (0)