ExCraft v0.2.1 ExCraft View Source

Link to this section Summary

Functions

Macro is expanded to structure definition and constructor

Boolean function, checks type of first argument

Link to this section Functions

Link to this macro craft(quoted_fields) View Source (macro)

Macro is expanded to structure definition and constructor.

Usage

  defmodule ExCraft.Car do
    import ExCraft
    craft [
      %ExCraft.Field{name: :brand,  type: :string,        required: false,   default: "custom",  enforce: false},
      %ExCraft.Field{name: :year,   type: :pos_integer,   required: true,    default: nil,       enforce: true},
      %ExCraft.Field{name: :used,   type: :boolean,       required: false,   default: true,      enforce: false},
    ]
  end

Examples

  iex> alias ExCraft.Car
  ExCraft.Car
  iex> %Car{year: 1990}
  %Car{brand: "custom", used: true, year: 1990}
  iex> Car.new(%{"year" => "1990"})
  %Car{brand: "custom", used: true, year: 1990}
  iex> Car.new(%{year: "1990"})
  %Car{brand: "custom", used: true, year: 1990}
  iex> Car.new([year: "1990"])
  %Car{brand: "custom", used: true, year: 1990}
  iex> Car.new(%{"year" => 1990.0})
  %Car{brand: "custom", used: true, year: 1990}
  iex> Car.new(%Car{brand: "custom", used: true, year: 1990})
  %Car{brand: "custom", used: true, year: 1990}
  iex> Car.new(%{"year" => "1990.1"})
  ** (RuntimeError) Elixir.ExCraft.Car ExCraft error. Type of "1990.1" is not pos_integer. Error in field %ExCraft.Field{default: nil, enforce: true, name: :year, required: true, type: :pos_integer} of data source %{"year" => "1990.1"}.

Boolean function, checks type of first argument.

Examples

  iex> ExCraft.is_type("hello", :string)
  true
  iex> ExCraft.is_type("hello", :binary)
  true
  iex> ExCraft.is_type(<<200, 200, 200>>, :string)
  false
  iex> ExCraft.is_type(<<200, 200, 200>>, :binary)
  true