-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
148 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Fsmx.Fsm do | ||
defmacro __using__(opts \\ []) do | ||
quote do | ||
@before_compile unquote(__MODULE__) | ||
|
||
@fsm Keyword.get(unquote(opts), :fsm, __MODULE__) | ||
|
||
def __fsmx__(:transitions), do: Keyword.fetch!(unquote(opts), :transitions) | ||
def __fsmx__(:fsm), do: @fsm | ||
end | ||
end | ||
|
||
defmacro __before_compile__(_env) do | ||
quote generated: false do | ||
def before_transition(struct, _from, _to), do: {:ok, struct} | ||
def after_transition(struct, _from, _to), do: {:ok, struct} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
defmodule Fsmx.Struct do | ||
defmacro __using__(opts) do | ||
defmacro __using__(opts \\ []) do | ||
quote do | ||
def __fsmx__(:transitions), do: Keyword.fetch!(unquote(opts), :transitions) | ||
@fsm Keyword.get(unquote(opts), :fsm, __MODULE__) | ||
|
||
if @fsm == __MODULE__ do | ||
use Fsmx.Fsm, unquote(opts) | ||
end | ||
|
||
def __fsmx__, do: @fsm | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule Fsmx.StructTest do | ||
use ExUnit.Case | ||
|
||
alias Fsmx.Struct | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,51 @@ | ||
defmodule Fsmx.TestStructs.WithCallbacks do | ||
defstruct [:state] | ||
defmodule ValidBefore do | ||
defstruct state: "1", before: false, after: false | ||
|
||
use Fsmx.Struct, transitions: %{"1" => ["2"], "2" => ["3"]} | ||
use Fsmx.Struct, transitions: %{"1" => "2"} | ||
|
||
def before_transition(struct, state) do | ||
IO.inspect(struct, state) | ||
def before_transition(struct, _old_state, _new_state) do | ||
{:ok, %{struct | before: true}} | ||
end | ||
end | ||
|
||
defmodule ValidAfter do | ||
defstruct state: "1", before: false, after: false | ||
|
||
use Fsmx.Struct, transitions: %{"1" => "2"} | ||
|
||
def after_transition(struct, _old_state, _new_state) do | ||
{:ok, %{struct | after: true}} | ||
end | ||
end | ||
|
||
defmodule InvalidBefore do | ||
defstruct state: "1", before: false, after: false | ||
|
||
use Fsmx.Struct, transitions: %{"1" => "2"} | ||
|
||
def before_transition(struct, _old_state, _new_state) do | ||
{:error, :before_failed} | ||
end | ||
end | ||
|
||
defmodule InvalidAfter do | ||
defstruct state: "1", before: false, after: false | ||
|
||
use Fsmx.Struct, transitions: %{"1" => "2"} | ||
|
||
def after_transition(struct, _old_state, _new_state) do | ||
{:error, :after_failed} | ||
end | ||
end | ||
|
||
defmodule PartialCallback do | ||
defstruct state: "1", before: false, after: false | ||
|
||
use Fsmx.Struct, transitions: %{"1" => "2", "2" => "3"} | ||
|
||
def after_transition(struct, "1", "2") do | ||
{:ok, struct} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Fsmx.TestStructs.WithSeparateFsm do | ||
defstruct [:state, before: false, after: false] | ||
|
||
use Fsmx.Struct, fsm: __MODULE__.Fsm | ||
|
||
defmodule Fsm do | ||
use Fsmx.Fsm, transitions: %{"1" => "2"} | ||
|
||
def before_transition(struct, _, _), do: {:ok, %{struct | before: true}} | ||
|
||
def after_transition(struct, _, _), do: {:ok, %{struct | after: true}} | ||
end | ||
end |