README

hex.pm version CI GitHub code size in bytes

AttrReader

In Elixir, Module variable is often used as a constant. But I didn't want to bother to define a getter when I wanted to refer to it with Test code etc. If you use AttrReader, you can use it without having to define the getter of the module attribute.

Installation

If available in Hex, the package can be installed by adding attr_reader to your list of dependencies in mix.exs:

def deps do
  [
    {:attr_reader, "~> 0.1.0"}
  ]
end

Usage

Defines by use

iex> defmodule UseAttrReader do
...>   @foo "foo"
...>   use AttrReader
...>   @bar :bar
...> end

iex> UseAttrReader.foo()
"foo"

iex> UseAttrReader.bar()
:bar

with except

iex> defmodule UseAttrReader do
...>   @foo "foo"
...>   use AttrReader, except: [:foo]
...>   @bar :bar
...> end

iex> UseAttrReader.bar()
:bar

iex> UseAttrReader.foo()
** (UndefinedFunctionError)

with only

iex> defmodule UseAttrReader do
...>   @foo "foo"
...>   use AttrReader, only: [:foo]
...>   @bar :bar
...> end

iex> UseAttrReader.foo()
"foo"

iex> UseAttrReader.bar()
** (UndefinedFunctionError)

Defines by macro

iex> defmodule UseAttrReaderMacro do
...>   require AttrReader
...>   AttrReader.define @foo
...>   AttrReader.define @bar, "bar"
...>   AttrReader.define @baz, :baz
...> end

iex> UseAttrReader.foo()
nil

iex> UseAttrReader.bar()
"bar"

iex> UseAttrReader.baz()
:baz