add Geef.Config module for Elixir

This commit is contained in:
Ramon Snir
2015-07-18 14:24:15 +03:00
committed by Carlos Martín Nieto
parent 8c39463095
commit 01254f2f90
6 changed files with 76 additions and 9 deletions

View File

@@ -2,17 +2,20 @@ defmodule Geef do
defmacro __using__(_) do defmacro __using__(_) do
quote do quote do
alias Geef.Repository alias Geef.Blob
alias Geef.Commit
alias Geef.Config
alias Geef.Index
alias Geef.Index.Tree
alias Geef.Object
alias Geef.Odb alias Geef.Odb
alias Geef.Oid alias Geef.Oid
alias Geef.Reference alias Geef.Reference
alias Geef.Object alias Geef.Repository
alias Geef.Commit
alias Geef.Tree
alias Geef.TreeEntry
alias Geef.Blob
alias Geef.Tag
alias Geef.Signature alias Geef.Signature
alias Geef.Tree
alias Geef.Tag
alias Geef.TreeEntry
end end
end end

25
lib/geef/config.ex Normal file
View File

@@ -0,0 +1,25 @@
defmodule Geef.Config do
import Geef
alias Geef.Repository
def stop(config), do: :geef_config.stop(config)
def open(repo) when is_pid(repo), do: Repository.config(repo)
def open(path), do: :geef_config.open(path)
def open!(arg), do: open(arg) |> assert_ok
def set(config, key, value) do
:geef_config.set(config, make_config_key(key), value)
end
def get_bool(config, key) do
:geef_config.get_bool(config, make_config_key(key))
end
def get_string(config, key) do
:geef_config.get_string(config, make_config_key(key))
end
defp make_config_key(key) when is_binary(key), do: String.to_char_list(key)
defp make_config_key(key), do: key
end

View File

@@ -15,7 +15,9 @@ defmodule Geef.Repository do
def discover(path), do: :geef_repo.discover(path) def discover(path), do: :geef_repo.discover(path)
def bare?(repo), do: :geef_repo.is_bare(repo) def bare?(repo), do: :geef_repo.is_bare(repo)
def gitdir(repo), do: :geef_repo.path(repo)
def workdir(repo), do: :geef_repo.workdir(repo) def workdir(repo), do: :geef_repo.workdir(repo)
def config(repo), do: :geef_repo.config(repo)
def reference_names(repo), do: :geef_repo.references(repo) def reference_names(repo), do: :geef_repo.references(repo)
end end

View File

@@ -16,6 +16,7 @@
-export([set/3]). -export([set/3]).
-export([get_bool/2]). -export([get_bool/2]).
-export([get_string/2]). -export([get_string/2]).
-export([stop/1]).
%% gen_server callbacks %% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).
@@ -60,6 +61,9 @@ get_bool(Pid, Name) ->
get_string(Pid, Name) -> get_string(Pid, Name) ->
gen_server:call(Pid, {get_string, Name}). gen_server:call(Pid, {get_string, Name}).
stop(Pid) ->
gen_server:call(Pid, stop).
%%%=================================================================== %%%===================================================================
%%% gen_server callbacks %%% gen_server callbacks
%%%=================================================================== %%%===================================================================
@@ -96,7 +100,10 @@ handle_call({set_string, Name, Val}, _From, State = #state{handle=Handle}) ->
handle_call({get_string, Name}, _From, State = #state{handle=Handle}) -> handle_call({get_string, Name}, _From, State = #state{handle=Handle}) ->
Reply = geef_nif:config_get_string(Handle, Name), Reply = geef_nif:config_get_string(Handle, Name),
{reply, Reply, State}. {reply, Reply, State};
handle_call(stop, _From, State) ->
{stop, normal, ok, State}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @private %% @private

30
test/config_test.exs Normal file
View File

@@ -0,0 +1,30 @@
defmodule ConfigTest do
use ExUnit.Case
use Geef
import RepoHelpers
setup do
{repo, path} = tmp_bare
Process.link(repo)
{:ok, config} = Repository.config(repo)
on_exit(fn -> File.rm_rf!(path) end)
{:ok, [config: config, path: path]}
end
test "write and get a boolean", meta do
config = meta[:config]
var = "core.logallrefupdates"
assert :ok = Config.set(config, var, true)
assert {:ok, true} = Config.get_bool(config, var)
end
test "write and get a string", meta do
config = meta[:config]
var = "user.name"
val = "Random J. Hacker"
assert :ok = Config.set(config, var, val)
assert {:ok, ^val} = Config.get_string(config, var)
end
end

View File

@@ -2,7 +2,7 @@ ExUnit.start
defmodule RepoHelpers do defmodule RepoHelpers do
def tmp_bare do def tmp_bare do
{a, b, c} = :erlang.now() {a, b, c} = :erlang.timestamp()
n = node() n = node()
dir = :io_lib.format("geef-~p~p~p~p.git", [n, a, b, c]) dir = :io_lib.format("geef-~p~p~p~p.git", [n, a, b, c])
path = Path.join(System.tmp_dir!, dir) path = Path.join(System.tmp_dir!, dir)