JViewer (JViewer v0.1.3) View Source

JViewer is an excellent way to declaratively represent elixir data in a JSON encodable format.

Link to this section Summary

Functions

Puts handler in schema's field under given path.

Puts handler_params in schema's field under given path.

Represents data in a JSON encodable format according to schema.

Link to this section Types

Link to this section Functions

Specs

array(keyword()) :: struct()

Specs

boolean(keyword()) :: struct()

Specs

field(keyword()) :: struct()

Specs

number(keyword()) :: struct()

Specs

object(keyword()) :: struct()
Link to this function

put_handler(schema, path, handler)

View Source

Specs

put_handler(schema(), list(), function()) :: schema()

Puts handler in schema's field under given path.

If path cannot be followed, an exception is thrown.

Example

iex> import JViewer
...>
iex> schema =
iex>    object(
iex>      fields: [
iex>        field(
iex>          key: "product",
iex>          type: object(
iex>            fields: [
iex>              field(
iex>                key: "price",
iex>              )
iex>            ]
iex>          )
iex>        )
iex>      ]
iex>    )
...>
iex> put_handler(schema, ["product", "price"], &Map.get/2)
object(
  fields: [
    field(
      key: "product",
      type: object(
        fields: [
          field(
            key: "price",
            handler: &Map.get/2
          )
        ]
      )
    )
  ]
)
Link to this function

put_handler_params(schema, path, params)

View Source

Specs

put_handler_params(schema(), list(), any()) :: schema()

Puts handler_params in schema's field under given path.

If path cannot be followed, an exception is thrown.

Example

iex> import JViewer
...>
iex> schema =
iex>    object(
iex>      fields: [
iex>        field(
iex>          key: "product",
iex>          type: object(
iex>            fields: [
iex>              field(
iex>                key: "price",
iex>                handler: &Map.get/2
iex>              )
iex>            ]
iex>          )
iex>        )
iex>      ]
iex>    )
...>
iex> put_handler_params(schema, ["product", "price"], :price)
object(
  fields: [
    field(
      key: "product",
      type: object(
        fields: [
          field(
            key: "price",
            handler: &Map.get/2,
            handler_params: :price
          )
        ]
      )
    )
  ]
)
Link to this function

represent(data, schema, general_handlers_params \\ %{})

View Source

Specs

represent(map(), schema(), any()) :: map()

Represents data in a JSON encodable format according to schema.

If data cannot be represented, an exception is thrown.

Example

iex> import JViewer
...>
iex> schema =
iex>   object(
iex>     fields: [
iex>       field(
iex>         key: "id",
iex>         type: number()
iex>       ),
iex>       field(
iex>         key: "title",
iex>         type: string()
iex>       )
iex>     ]
iex>   )
...>
iex> data = %{
iex>   id: 1,
iex>   info: "I ate bread for breakfast",
iex>   title: "Notes"
iex> }
...>
iex> represent(data, schema)
%{
  "id" => 1,
  "title" => "Notes"
}

Specs

string(keyword()) :: struct()