Skip to content

Commit

Permalink
Merge pull request #27 from bbalser/merge_function_default
Browse files Browse the repository at this point in the history
Change for merge with a function to call the function with a value of…
  • Loading branch information
Nathaniel Scott Stevens authored Oct 29, 2019
2 parents dc06584 + b0c34f0 commit 7e96e87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/brook/view_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ defmodule Brook.ViewState do
assert_environment()

case get(instance(), collection, key) do
{:ok, nil} -> default
{:ok, nil} -> default || function.(nil)
{:ok, old_value} -> function.(old_value)
{:error, reason} -> raise RuntimeError, message: inspect(reason)
end
Expand Down
12 changes: 12 additions & 0 deletions test/unit/view_state_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ defmodule Brook.ViewStateTest do
merge(:data, data["id"], data)
merge(:data, data["id"], %{second_merge: true})
end

def handle_event(%Brook.Event{type: "function_merge", data: data}) do
merge(:data, data["id"], fn _old -> data end)
end
end

setup do
Expand Down Expand Up @@ -119,6 +123,14 @@ defmodule Brook.ViewStateTest do
assert %{"id" => 18, "name" => "Bill", second_merge: true} == Brook.get!(@instance, :data, 18)
end
end

test "data can be merged with a function and no current value" do
send_event("function_merge", %{"id" => 23, "name" => "Nathaniel"})

assert_async do
assert %{"id" => 23, "name" => "Nathaniel"} == Brook.get!(@instance, :data, 23)
end
end
end

describe "delete" do
Expand Down

0 comments on commit 7e96e87

Please sign in to comment.