Skip to content

tashirosota/attr_reader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hex.pm version CI GitHub code size in bytes

AttrReader

In Elixir, the 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.2"}
  ]
end

Usage

Defined 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)

Defined 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

About

🎓 Handle readers easier

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages