Skip to content

A package to create constants in elixir modules.

License

Notifications You must be signed in to change notification settings

vasuadari/constant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Constant

Build Status Hex.pm version

A package to define constants in your module.

Installation

The package can be installed by adding constant to your list of dependencies in mix.exs:

def deps do
  [
    {:constant, "~> 0.0.1"}
  ]
end

Usage

defmodule HTTPStatusCode do
  import Constant

  defconst ok: 200, created: 201
end

iex> HTTPStatusCode.ok()
200
iex> HTTPStatusCode.created()
201

defmodule ColorCode do
  import Constant

  defconst [black: "FFFFFF", white: "000000"], atomize: true
end

iex> ColorCode.black()
"FFFFFF"
iex> ColorCode.black_atom()
:FFFFFF

The constant can be used in the format const_<key> after requiring the module, and which enables the constant to be directly used in argument pattern matches and guards.

defmodule ColorCodeMatcher do
  require ColorCode

  def is_black(_color = ColorCode.const_black()), do: true
  def is_black(_), do: false
end

License

Constant source code is licensed under the MIT License.

About

A package to create constants in elixir modules.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages