aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lkn/core/properties.ex
blob: 241407c84313245782db2aa09453206ae5f835b3 (plain)
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
38
39
40
#
# lkn.core: an entity-component-system (ecs) implementation for lyxan
# Copyright (C) 2017 Thomas Letan <contact@thomasletan.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
defmodule Lkn.Core.Properties do
  use Lkn.Prelude

  @moduledoc false

  @spec start_link(map, Lkn.Core.Entity.k) :: Agent.on_start
  def start_link(content, entity_key) do
    Agent.start_link(fn -> content end, name: Lkn.Core.Name.properties(entity_key))
  end

  @spec read(Lkn.Core.Entity.k, Lkn.Core.Entity.prop) :: Option.t(Lkn.Core.Entity.value)
  def read(entity_key, prop) do
    case Agent.get(Lkn.Core.Name.properties(entity_key), &Map.fetch(&1, prop)) do
      {:ok, val} -> Option.some(val)
      :error -> Option.nothing()
    end
  end

  @spec write(Lkn.Core.Entity.k, Lkn.Core.Entity.prop, Lkn.Core.Entity.value) :: :ok
  def write(entity_key, prop, v) do
    Agent.update(Lkn.Core.Name.properties(entity_key), &Map.put(&1, prop, v))
  end
end