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
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 section Functions
Embeds default spec/0
, spec/1
and spec/2
functions into the caller module
with given model definition opts
.
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
Returns the value stored under path
in the model
definition.
Options:
default
: If value isnil
, 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
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.
Options that are recommended to support:
default
: If value isnil
, default value will be returned in its place. (Default:nil
)