Strukt behaviour (Strukt v0.2.0) View Source

Link to this section Summary

Functions

This variant of defstruct can accept a list of fields, just like Kernel.defstruct/1, in which case it simply defers to Kernel.defstruct/1 and does nothing; or it can be passed a block containing an Ecto.Schema definition. The resulting struct/schema is defined in the current module scope, and will inherit attributes like @derive, @primary_key, etc., which are already defined in the current scope.

This variant of defstruct takes a module name and block containing a struct schema and any other module contents desired, and defines a new module with that name, generating a struct just like Strukt.defstruct/1.

Callbacks

This callback can be overridden to provide custom change behavior.

See new/1

This callback can be overridden to provide custom initialization behavior.

This callback can be overridden to provide custom validation logic.

Link to this section Functions

Link to this macro

defstruct(arg)

View Source (macro)

This variant of defstruct can accept a list of fields, just like Kernel.defstruct/1, in which case it simply defers to Kernel.defstruct/1 and does nothing; or it can be passed a block containing an Ecto.Schema definition. The resulting struct/schema is defined in the current module scope, and will inherit attributes like @derive, @primary_key, etc., which are already defined in the current scope.

Example

defmodule Passthrough do
  use Strukt

  defstruct [:name]
end

defmodule Person do
  use Strukt

  @derive [Jason.Encoder]
  defstruct do
    field :name, :string
  end

  def say_hello(%__MODULE__{name: name}), do: "Hello #{name}!"
end

Above, even though Strukt.defstruct/1 is in scope, it simply passes through the list of fields to Kernel.defstruct/1, as without a proper schema, there isn't much useful we can do. This allows intermixing uses of defstruct/1 in the same scope without conflict.

Link to this macro

defstruct(name, list)

View Source (macro)

This variant of defstruct takes a module name and block containing a struct schema and any other module contents desired, and defines a new module with that name, generating a struct just like Strukt.defstruct/1.

Example

use Strukt

defstruct Person do
  @derive [Jason.Encoder]

  field :name, :string

  def say_hello(%__MODULE__{name: name}), do: "Hello #{name}!"
end

NOTE: Unlike Strukt.defstruct/1, which inherits attributes like @derive or @primary_key from the surrounding scope; this macro requires them to be defined in the body, as shown above.

Link to this section Callbacks

Specs

See change/2

Specs

change(Ecto.Changeset.t() | term(), Keyword.t() | map()) :: Ecto.Changeset.t()

This callback can be overridden to provide custom change behavior.

The default implementation provided for you creates a changeset and applies all of the inline validations defined on the schema.

NOTE: It is recommended that if you need to perform custom validations, that you override validate/1 instead. If you need to override this callback specifically for some reason, make sure you call super/2 at some point during your implementation to ensure that validations are run.

Specs

new() :: {:ok, struct()} | {:error, Ecto.Changeset.t()}

See new/1

Specs

new(Keyword.t() | map()) :: {:ok, struct()} | {:error, Ecto.Changeset.t()}

This callback can be overridden to provide custom initialization behavior.

The default implementation provided for you performs all of the necessary validation and autogeneration of fields with those options set.

NOTE: It is critical that if you do override this callback, that you call super/1 to run the default implementation at some point in your implementation.

Specs

This callback can be overridden to provide custom validation logic.

The default implementation simply returns the changeset it is given. Validations defined inline with fields are handled by a specially generated __validate__/1 function which is called directly by new/1 and change/2.

NOTE: If you override this function, there is no need to invoke super/1