Skip to content

codedge-llc/chimera

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Hex.pm Hex.pm

Chimera

Dead-simple conversion between Elixir structs

Installation

Add chimera as a mix.exs dependency:

def deps do
  [
    {:chimera, "~> 0.3.0"}
  ]
end

Usage

Add use Chimera to your struct's module. It adds new/1 and new/2 functions that will create a new struct from any given map, struct or keyword list.

defmodule User do
  defstruct id: nil, name: nil, email: nil
  use Chimera
end

defmodule Profile do
  defstruct id: nil, name: "Person", avatar: nil
  use Chimera
end

iex> User.new(id: 1234, name: "Person")
%User{id: 1234, name: "Person", email: nil}

iex> user = %User{id: 1234, name: "Person", email: "[email protected]"}
iex> Profile.new(user)
%Profile{id: 1234, name: "Person", avatar: nil}

Custom Mappings

Use the optional :map argument to specify custom key mappings. :map is a keyword list whose keys correspond to the keys of the destination struct.

iex> user = %User{id: 1234, name: "Person", email: "[email protected]"}
iex> Profile.new(user, map: [name: nil])
%Profile{id: 1234, name: nil, avatar: nil}

Specify a function of arity 1 that takes the source struct as a parameter:

iex> user = %User{id: 1234, name: "Person", email: "[email protected]"}
iex> Profile.new(user, map: [name: fn user -> String.upcase(user.email) end])
%Profile{id: 1234, name: "PERSON", avatar: nil}

About

Dead-simple conversion between Elixir structs

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages