AttrAccessor (attr_accessor v0.1.0)

View Source

Ruby style accessors for Elixir structs. See README for more example usage.

Summary

Functions

Creates attr_reader/1 + attr_updater/1 + attr_writer/1 functions on struct module for attr symbol/symbols

Creates reader functions on struct module for attr symbol/symbols

Creates a "bang" updater functions on struct module for attr symbol/symbols which accept a function to read the current value, pass it to the function, and write the result back to the struct.

Creates writer functions on struct module for attr symbol/symbols.

Functions

attr_accessor(attrs)

(macro)
@spec attr_accessor(atom() | [atom()]) :: Macro.t()

Creates attr_reader/1 + attr_updater/1 + attr_writer/1 functions on struct module for attr symbol/symbols

Example

defmodule MyStruct do
  defstruct [:key1, :key2, :key3]

  import AttrAccessor
  attr_accessor :key1 # single key, or multiple is ok
  attr_accessor [:key2, :key3]
end

my_struct = %MyStruct{key1: "foo", key2: "bar", key3: "baz"}

my_struct
  |> MyStruct.key1("baz")
  |> MyStruct.key1(&String.reverse/1)
  |> MyStruct.key1()
# "zab"

Writing something like

defmodule MyStruct do
  defstruct [:some_key]
  attr_accessor :some_key
end

Is equivalent to

defmodule MyStruct do
  defstruct [:some_key]
  attr_reader :some_key
  attr_updater :some_key
  attr_writer :some_key
end

Which is also equivalent to

defmodule MyStruct do
  defstruct [:some_key]

  def some_key(st = %__MODULE__{some_key: value}) do
    value
  end

  def some_key!(st = %__MODULE__{some_key: value}, f) do
    %__MODULE__{ st | some_key: f.(value) }
  end

  def some_key(st = %__MODULE__{}, value) do
    %__MODULE__{ st | some_key: value }
  end
end

attr_reader(attrs)

(macro)
@spec attr_reader(atom() | [atom()]) :: Macro.t()

Creates reader functions on struct module for attr symbol/symbols

Example

defmodule MyStruct do
  defstruct [:key1, :key2, :key3]

  import AttrAccessor
  attr_reader :key1 # single key, or multiple is ok
  attr_reader [:key2, :key3]
end

my_struct = %MyStruct{key1: "foo", key2: "bar", key3: "baz"}

MyStruct.key1(my_struct)
# "foo"

Writing something like

defmodule MyStruct do
  defstruct [:some_key]
  attr_reader :some_key
end

Is equivalent to

defmodule MyStruct do
  defstruct [:some_key]

  def some_key(st = %__MODULE__{some_key: value}) do
    value
  end
end

attr_updater(attrs)

(macro)
@spec attr_updater(atom() | [atom()]) :: Macro.t()

Creates a "bang" updater functions on struct module for attr symbol/symbols which accept a function to read the current value, pass it to the function, and write the result back to the struct.

Example

defmodule MyStruct do
  defstruct [:key1, :key2, :key3]

  import AttrAccessor
  attr_updater :key1 # single key, or multiple is ok
  attr_updater [:key2, :key3]
end

my_struct =
  %MyStruct{key1: "foo"}
  |> MyStruct.key1!(&String.upcase/1)

my_struct.key1
# "FOO"

Writing something like

defmodule MyStruct do
  defstruct [:some_key]
  attr_updater :some_key
end

Is equivalent to

defmodule MyStruct do
  defstruct [:some_key]

  def some_key!(st = %__MODULE__{some_key: value}, f) do
    %__MODULE__{ st | some_key: f.(value) }
  end
end

attr_writer(attrs)

(macro)
@spec attr_writer(atom() | [atom()]) :: Macro.t()

Creates writer functions on struct module for attr symbol/symbols.

Example

defmodule MyStruct do
  defstruct [:key1, :key2, :key3]

  import AttrAccessor
  attr_writer :key1 # single key, or multiple is ok
  attr_writer [:key2, :key3]
end

my_struct =
  %MyStruct{key1: "foo"}
  |> MyStruct.key1("bar)

my_struct.key1
# "bar"

Writing something like

defmodule MyStruct do
  defstruct [:some_key]
  attr_writer :some_key
end

Is equivalent to

defmodule MyStruct do
  defstruct [:some_key]

  def some_key(st = %__MODULE__{}, value) do
    %__MODULE__{ st | some_key: value }
  end
end