Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into update/ecto-phoenix
- Loading branch information
Showing
18 changed files
with
157 additions
and
69 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 |
---|---|---|
@@ -1,3 +1,42 @@ | ||
defmodule Movement.Comparer do | ||
alias Movement.Comparers.{SyncSmart, SyncPassive, MergeSmart, MergeForce, MergePassive} | ||
|
||
@callback compare(map, map) :: Movement.Operation.t() | ||
|
||
@default_comparer "smart" | ||
|
||
@sync_comparers %{ | ||
"passive" => &SyncPassive.compare/2, | ||
"smart" => &SyncSmart.compare/2 | ||
} | ||
|
||
@merge_comparers %{ | ||
"passive" => &MergePassive.compare/2, | ||
"smart" => &MergeSmart.compare/2, | ||
"force" => &MergeForce.compare/2 | ||
} | ||
|
||
@doc """ | ||
A string is used to identify the algorithm used to compare incoming translations to their current form. | ||
The implementations of `Movement.Comparer` are accessible from this function. | ||
## Examples | ||
iex> Movement.Comparer.comparer(:sync, "smart") | ||
&Movement.Comparers.SyncSmart.compare/2 | ||
iex> Movement.Comparer.comparer(:sync, "passive") | ||
&Movement.Comparers.SyncPassive.compare/2 | ||
iex> Movement.Comparer.comparer(:merge, "passive") | ||
&Movement.Comparers.MergePassive.compare/2 | ||
iex> Movement.Comparer.comparer(:merge, "smart") | ||
&Movement.Comparers.MergeSmart.compare/2 | ||
iex> Movement.Comparer.comparer(:merge, "force") | ||
&Movement.Comparers.MergeForce.compare/2 | ||
""" | ||
def comparer(:sync, type) do | ||
Map.get(@sync_comparers, type, @sync_comparers[@default_comparer]) | ||
end | ||
|
||
def comparer(:merge, type) do | ||
Map.get(@merge_comparers, type, @merge_comparers[@default_comparer]) | ||
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,39 @@ | ||
defmodule Movement.Comparers.SyncPassive do | ||
@behaviour Movement.Comparer | ||
|
||
alias Movement.Mappers.Operation, as: OperationMapper | ||
|
||
alias Movement.{ | ||
Operation, | ||
TranslationComparer | ||
} | ||
|
||
@doc """ | ||
## Examples | ||
iex> suggested_translation = %Movement.SuggestedTranslation{key: "a", text: "foo"} | ||
iex> Movement.Comparers.SyncPassive.compare(nil, suggested_translation).action | ||
"new" | ||
iex> translation = %Accent.Translation{removed: true, key: "a", proposed_text: "bar", corrected_text: "baz"} | ||
iex> suggested_translation = %Movement.SuggestedTranslation{key: "a", text: "foo"} | ||
iex> Movement.Comparers.SyncPassive.compare(translation, suggested_translation).action | ||
"renew" | ||
iex> translation = %Accent.Translation{marked_as_removed: true, key: "a", proposed_text: "bar", corrected_text: "baz"} | ||
iex> suggested_translation = %Movement.SuggestedTranslation{key: "a", text: "foo"} | ||
iex> Movement.Comparers.SyncPassive.compare(translation, suggested_translation).action | ||
"noop" | ||
""" | ||
def compare(%{key: key, marked_as_removed: true}, _), do: %Operation{action: "noop", key: key} | ||
|
||
def compare(translation, suggested_translation) do | ||
case TranslationComparer.compare(translation, suggested_translation.text) do | ||
{action, text} when action in ~w(new renew remove) -> | ||
suggested_translation = %{suggested_translation | text: text} | ||
|
||
OperationMapper.map(action, translation, suggested_translation) | ||
|
||
_ -> | ||
%Operation{action: "noop", key: suggested_translation.key} | ||
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
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
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,4 @@ | ||
defmodule AccentTest.Movement.Comparer do | ||
use ExUnit.Case, async: true | ||
doctest(Movement.Comparer) | ||
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,4 @@ | ||
defmodule AccentTest.Movement.Comparers.SyncPassive do | ||
use ExUnit.Case | ||
doctest Movement.Comparers.SyncPassive | ||
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,4 @@ | ||
defmodule AccentTest.Movement.Comparers.SyncSmart do | ||
use ExUnit.Case | ||
doctest Movement.Comparers.SyncSmart | ||
end |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.