anka v0.1.2 Anka.Model behaviour View Source

Anka models are used by Anka generators to derive new structures from them.

A model simply must have spec/0, spec/1 and spec/2 functions to provide information about its definition, specially for Anka generators to derive new structures by using these definitions.

The structure of a model definition may change based on needs of generators will be used by developers.

Developers are able to implement their own spec functions while defining their models, but unless otherwise required, it is highly recommended to use Anka.Model.__using__/1 macro instead.

Examples

defmodule Document do
  use Anka.Model, [
    name: [
      singular: :document,
      plural: :documents,
    ],
    struct: [
      fields: [
        :id,
        content: [
          opts: [
            default: ""
          ]
        ]
      ]
    ]
  ]
end

Link to this section Summary

Functions

Embeds default spec/0, spec/1 and spec/2 functions into the caller module with given model definition opts.

Returns definition of model.

Returns the value stored under path in the model definition.

Callbacks

Should return definition of the model.

Should return the value stored under path in the model definition.

Should return the value stored under path in the model definition.

Link to this section Types

Link to this type

container()

View Source (since 0.1.1)
container() :: keyword() | map() | struct() | any()
Link to this type

path()

View Source (since 0.1.1)
path() :: [any(), ...] | any()
Link to this type

t()

View Source (since 0.1.1)
t() :: module() | container()
Link to this type

value()

View Source (since 0.1.1)
value() :: any()

Link to this section Functions

Link to this macro

__using__(opts \\ [])

View Source (macro) (since 0.1.1)

Embeds default spec/0, spec/1 and spec/2 functions into the caller module with given model definition opts.

Link to this function

spec(model)

View Source (since 0.1.1)
spec(t()) :: container()

Returns definition of model.

Examples

iex> Anka.Model.spec(Model.Document)
[
  name: [
    singular: :document,
    plural: :documents
  ],
  struct: [
    fields: [
      :id,
      content: [
        opts: [
          default: ""
        ]
      ]
    ]
  ]
]

iex> Anka.Model.spec(Model.Document) == Model.Document.spec()
true
Link to this function

spec(model, path, opts \\ [])

View Source (since 0.1.1)
spec(t(), path(), keyword()) :: any()

Returns the value stored under path in the model definition.

Options:

  • default: If value is nil, default value will be returned in its place. (Default: nil)

Examples

iex> Anka.Model.spec(Model.Document, :name, default: [])
[singular: :document, plural: :documents]

iex> Anka.Model.spec([singular: :document, plural: :documents], :singular)
:document

iex> Anka.Model.spec(Model.Document, [:struct, :fields, Access.at(0)])
:id

Link to this section Callbacks

Link to this callback

spec()

View Source (since 0.1.1)
spec() :: container()

Should return definition of the model.

Link to this callback

spec(path)

View Source (since 0.1.1)
spec(path :: path()) :: value()

Should return the value stored under path in the model definition.

Link to this callback

spec(path, opts)

View Source (since 0.1.1)
spec(path :: any(), opts :: keyword()) :: any()

Should return the value stored under path in the model definition.

Options that are recommended to support:

  • default: If value is nil, default value will be returned in its place. (Default: nil)