Skip to content

Commit a61728c

Browse files
authored
Merge pull request #7 from lsxliron/string_as_name_6
Support strings as job names
2 parents bf42975 + 21c80f5 commit a61728c

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

coveralls.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"skip_files": [
33
"test",
4-
"lib/quantum_storage_ecto/migrations",
5-
"lib/quantum_storage_ecto/types"
4+
"lib/quantum_storage_ecto/migrations"
65
]
76
}

lib/quantum_storage_ecto/types/atom_or_ref.ex

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ defmodule QuantumStorageEcto.Types.AtomOrRef do
1919
Types.Atom.cast(value)
2020
end
2121

22+
def cast(value) when is_binary(value) do
23+
if value |> String.starts_with?("#Reference<") do
24+
Types.Reference.cast(value)
25+
else
26+
Types.Atom.cast(value)
27+
end
28+
end
29+
2230
def cast(_), do: :error
2331

2432
def load(value) when is_binary(value) do

lib/quantum_storage_ecto/types/reference.ex

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ defmodule QuantumStorageEcto.Types.Reference do
1111
def cast(value) when is_reference(value), do: {:ok, value}
1212

1313
def cast(value) when is_binary(value) do
14-
{:ok, value |> String.to_charlist() |> :erlang.list_to_ref()}
14+
{:ok,
15+
value
16+
|> String.replace("#Reference<", "#Ref<")
17+
|> String.to_charlist()
18+
|> :erlang.list_to_ref()}
1519
end
1620

1721
def cast(_), do: :error

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule QuantumStorageEcto.MixProject do
22
use Mix.Project
33

4-
@version "0.1.0"
4+
@version "0.1.1"
55

66
def project do
77
[
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule QuantumStorageEcto.Types.AtomOrRefTest do
2+
use ExUnit.Case, async: true
3+
use QuantumStorageEcto.DataCase
4+
import Crontab.CronExpression
5+
alias QuantumStorageEcto.Job
6+
alias QuantumStorageEcto.Repo
7+
8+
setup _ do
9+
params = %{
10+
schedule: ~e[1 * * * *],
11+
task: fn -> :ok end,
12+
run_strategy: %Quantum.RunStrategy.Local{}
13+
}
14+
15+
{:ok, %{params: params}}
16+
end
17+
18+
def validate(attrs) do
19+
{:ok, _} =
20+
%Job{}
21+
|> Job.changeset(attrs)
22+
|> QuantumStorageEcto.Repo.insert()
23+
24+
refute Job |> Repo.get(attrs.name) |> is_nil()
25+
end
26+
27+
test "can store atom properly", %{params: attrs} do
28+
attrs |> Map.put(:name, :test_job_reference) |> validate()
29+
end
30+
31+
test "can store string properly", %{params: attrs} do
32+
attrs |> Map.put(:name, "test_job_reference") |> validate()
33+
end
34+
35+
test "can store reference properly", %{params: attrs} do
36+
attrs |> Map.put(:name, make_ref()) |> validate()
37+
end
38+
39+
test "can store string reference properly", %{params: attrs} do
40+
attrs |> Map.put(:name, "#Reference<0.946883141.702808066.133335>") |> validate()
41+
end
42+
end

0 commit comments

Comments
 (0)