QuickStruct v0.1.1 QuickStruct View Source

Creates a struct with enforced keys, the type of the struct and a make function to create the struct.

You have to "use" this module and give a list of fields or a keyword list with fields and specs to create a struct.

As an alternative you can create a module and a struct together; just require QuickStruct and call define_module/2.

Examples

Assume you define the following structs:

defmodule QuickStructTest.User do
  use QuickStruct, [firstname: String.t, name: String.t]
end
defmodule QuickStructTest.Pair do
  use QuickStruct, [:first, :second]
end

Or equivalent:

require QuickStruct
QuickStruct.define_module QuickStructTest.User, [firstname: String.t, name: String.t]
QuickStruct.define_module QuickStructTest.Pair, [:first, :second]

To create a struct you can either use make/1 with a keyword list to specify the fields, or use make, where each argument is one field (order matters):

iex> alias QuickStructTest.User
iex> User.make("Jon", "Adams")
%User{firstname: "Jon", name: "Adams"}
iex> User.make([name: "Adams", firstname: "Jon"])
%User{firstname: "Jon", name: "Adams"}

iex> alias QuickStructTest.Pair
iex> Pair.make(1, 0)
%Pair{a: 1, b: 0}
iex> Pair.make([a: "My", b: "String"])
%Pair{a: "My", b: "String"}

Link to this section Summary

Functions

Generates a function which will generate a struct with some given default values.

Defines a module together with a struct with the given field list.

Returns true if the given object is a struct of the given module, otherwise false.

Link to this section Functions

Link to this macro

constructor_with_defaults(defaults) View Source (macro)
constructor_with_defaults(keyword()) :: {:__block__, [], [any()]}

Generates a function which will generate a struct with some given default values.

Example

defmodule Triple do
  use QuickStruct, [:first, :second, :third]
  QuickStruct.constructor_with_defaults([third: 0])
end
pair = Triple.make_with_defaults([first: 24, second: 12])
# => %Triple{first: 24, second: 12, third: 0}
Link to this macro

define_module(modulename, fields) View Source (macro)
define_module(module(), keyword()) :: {:defmodule, keyword(), keyword()}

Defines a module together with a struct with the given field list.

Example

require QuickStruct
QuickStruct.define_module(MyDate, [day: integer(), month: integer(), year: integer()])
new_year = %MyDate{day: 1, month: 1, year: 2000}

This is equivalent to:

defmodule MyDate do
  use QuickStruct, [day: integer(), month: integer(), year: integer()]
end
new_year = %MyDate{day: 1, month: 1, year: 2000}
Link to this function

is_struct_of(arg1, module) View Source
is_struct_of(any(), module()) :: boolean()

Returns true if the given object is a struct of the given module, otherwise false.

Examples

iex> alias QuickStructTest.User
iex> QuickStruct.is_struct_of(%User{firstname: "Jon", name: "Adams"}, QuickStructTest.User)
true
iex> QuickStruct.is_struct_of(%User{firstname: "Jon", name: "Adams"}, MyModule)
false