dd v0.0.3 DD.Type.Behaviour behaviour

Link to this section Summary

Callbacks

Convert a string external representation of a value into into it’s internal representation

A record is defined using something like

Convert a value to a string that will be suitable for display (for example as the value of an tag

Validates that the given value is consistent with a type and that it obeys any constraints in it’s specs

Link to this section Callbacks

Link to this callback from_display_value(arg0, arg1)
from_display_value(String.t(), Keyword.t()) :: Any.t()

Convert a string external representation of a value into into it’s internal representation

Link to this callback from_options(atom, keyword)
from_options(atom(), keyword()) :: {module(), atom(), Keyword.t()}

A record is defined using something like

  defrecord Person do
    string(:name, min: 2, max: 50)
    date(:dob)
  end

The defrecord code calls the type module’s from_spec function for each field (so the type modules DD.Type.String and DD.Type.Date would be called about).

This function returns a field definition structure, having first validated (and possibly manipulated) the arguments.

In the first example above, from_spec would we called with

DD.String.from_options(:name, [ min: 2, max: 50 ])

Values that are stored in the field structure have potentially been updated to work with this type. For example, if you pass a string as an option to DD.Type.String’s matches: option, it will be returned as a Regex.

Link to this callback to_display_value(arg0, arg1)
to_display_value(Any.t(), Keyword.t()) :: String.t()

Convert a value to a string that will be suitable for display (for example as the value of an tag.

Link to this callback validate(arg0, arg1)
validate(Any.t(), Access.t()) :: String.t() | nil

Validates that the given value is consistent with a type and that it obeys any constraints in it’s specs.

For example, the validation for DD.Type.String is:

def validate(value, _) when not is_binary(value) do
  "should be a string"
end

def validate(value, specs) do
  validate_length(String.length(value), specs[:min], specs[:max])
  || validate_matches(value, specs[:matches])
end