DataMorph v0.0.6 DataMorph.Struct

Contains from_rows/3 function that defines a struct and return structs created from rows, and defmodulestruct/2 macro to define a struct.

Summary

Functions

Defines a struct from given kind alias and list of fields

Defines a struct and returns structs created from rows list or stream, and a namespace, a name, and a list of headers

Functions

defmodulestruct(kind, fields) (macro)

Defines a struct from given kind alias and list of fields.

When called a second time with additional new fields it redefines struct, setting fields to be the union of the old and new fields.

Examples

iex> DataMorph.Struct.defmodulestruct(Foo.Bar, [:baz, :boom])
{:module, Foo.Bar, _, %Foo.Bar{baz: nil, boom: nil}}
...> %Foo.Bar{baz: "zy", boom: "boom"}
%Foo.Bar{baz: "zy", boom: "boom"}
...> DataMorph.Struct.defmodulestruct(Foo.Bar, [:bish, :bash])
{:module, Foo.Bar, _, %Foo.Bar{bash: nil, baz: nil, bish: nil, boom: nil}}
...> %Foo.Bar{bish: "zy", bash: "boom"}
%Foo.Bar{bash: "boom", baz: nil, bish: "zy", boom: nil}
from_rows(rows, namespace, name, headers)

Defines a struct and returns structs created from rows list or stream, and a namespace, a name, and a list of headers.

Redefines struct when called again with same namespace and name but different headers, sets struct fields to be the union of the old and new headers.

Examples

Defines a struct and returns stream of structs created from stream of rows.

iex> headers = ["name","ISO code"]
...> [
...>   ["New Zealand","nz"],
...>   ["United Kingdom","gb"]
...> ] \
...> |> Stream.map(& &1) \
...> |> DataMorph.Struct.from_rows(OpenRegister, "country", headers) \
...> |> Enum.to_list
[%OpenRegister.Country{iso_code: "nz", name: "New Zealand"},
%OpenRegister.Country{iso_code: "gb", name: "United Kingdom"}]

Defines a struct and returns stream of structs created from list of rows.

iex> headers = ["name","ISO code"]
...> [
...>   ["New Zealand","nz"],
...>   ["United Kingdom","gb"]
...> ] \
...> |> DataMorph.Struct.from_rows("open-register", Country, headers) \
...> |> Enum.to_list
[%OpenRegister.Country{iso_code: "nz", name: "New Zealand"},
%OpenRegister.Country{iso_code: "gb", name: "United Kingdom"}]
normalize(string)