summaryrefslogtreecommitdiff
path: root/lib/powerdnsex/models/record.ex
blob: 9ffc2b7c79dbd0b0f097d2cc4fec65019278f449 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defmodule PowerDNSex.Models.Record do
  defstruct [:content, :disabled]

  def build(attrs) when is_list(attrs) do
    Enum.reduce(attrs, [], &(&2 ++ [build(&1)]))
  end

  def build(attrs) when is_tuple(attrs) do
    %__MODULE__{content: elem(attrs, 0), disabled: elem(attrs, 1)}
  end

  def build(attrs) when is_map(attrs) do
    %__MODULE__{content: attrs.content, disabled: attrs.disabled}
  end

  def as_body(nil), do: []

  def as_body(content) when is_list(content) do
    Enum.reduce(content, [], &(&2 ++ [as_body(&1)]))
  end

  def as_body(%__MODULE__{} = record_attrs) do
    Map.from_struct(record_attrs)
  end

  def as_body(record_attrs) when is_tuple(record_attrs) do
    %{content: elem(record_attrs, 0), disabled: elem(record_attrs, 1)}
  end

  def find(records, attrs) when is_list(records) do
    Enum.find(records, fn(record)->
      Enum.all?(attrs, fn({attr, attr_value})->
        Map.get(record, attr) == attr_value
      end)
    end)
  end
end