Implement Access for Geef.Tree

Make Geef.Tree its own record so we can define the protocol
implementation for it. There shouldn't be any generic functions you'd
want to call on a tree record.
This commit is contained in:
Carlos Martín Nieto
2013-06-12 00:05:04 +02:00
parent 2f20e18b56
commit b671c5110e

View File

@@ -1,6 +1,6 @@
defrecord Geef.TreeEntry, Record.extract(:geef_tree_entry, from: "src/geef_records.hrl")
defmodule Geef.Tree do
defrecord Geef.Tree, Record.extract(:geef_object, from: "src/geef_records.hrl") do
alias Geef.Object
alias Geef.TreeEntry
@@ -9,13 +9,13 @@ defmodule Geef.Tree do
def lookup(repo, id) do
case :geef_tree.lookup(repo, id) do
{:ok, obj} ->
{:ok, Object.new obj}
{:ok, new obj}
error ->
error
end
end
def get(tree = Object[type: :tree], path) do
def get(tree, path) do
case :geef_tree.get(rebind(tree), path) do
{:ok, entry} ->
{:ok, TreeEntry.new entry}
@@ -25,3 +25,14 @@ defmodule Geef.Tree do
end
end
defimpl Access, for: Geef.Tree do
def access(tree, key) do
case Geef.Tree.get(tree, key) do
{:ok, entry} -> entry
{:error, _} -> nil
end
end
end