Packages

Ecto Observable brings observable functionality to an Ecto Repo.

Current section

5 Versions

Jump to

Compare versions

4 files changed
+38 additions
-11 deletions
  @@ -12,7 +12,7 @@ The package can be installed by adding `ecto_observable` to your list of depende
12 12 ```elixir
13 13 def deps do
14 14 [
15 - {:ecto_observable, "~> 0.2.0"}
15 + {:ecto_observable, "~> 0.3.0"}
16 16 ]
17 17 end
18 18 ```
  @@ -55,6 +55,11 @@ defmodule SubscribersObserver do
55 55 def handle_notify({:update, [old_post, new_post]}) do
56 56 :ok
57 57 end
58 +
59 + # Defined for the sake of example. Ignore me!
60 + def handle_notify({:delete, post}) do
61 + :ok
62 + end
58 63 end
59 64 ```
60 65
  @@ -74,8 +79,9 @@ defmodule Post do
74 79 end
75 80
76 81 observations do
77 - on_action(:insert, SubscribersObserver)
78 - on_action(:update, SubscribersObserver)
82 + action(:insert, [SubscribersObserver])
83 + action(:update, [SubscribersObserver])
84 + action(:delete, [OtherObserverOne, OtherObserverTwo]) # Defined for the sake of example.
79 85 end
80 86 end
81 87 ```
  @@ -92,3 +98,17 @@ end
92 98 ```
93 99
94 100 Our users will now be informed of any new posts with topics they are interested in!
101 +
102 + The same behaviour can be replicated for `:update` and `:delete` actions.
103 +
104 + ```elixir
105 + Repo.update_and_notify(post)
106 + Repo.delete_and_notify(post)
107 + ```
108 +
109 + Each of the new observer-based actions also has equivalent bang function available
110 + that will raise on error.
111 +
112 + ```elixir
113 + Repo.insert_and_notify!(post)
114 + ```
  @@ -16,5 +16,5 @@
16 16 {<<"name">>,<<"observable">>},
17 17 {<<"optional">>,false},
18 18 {<<"repository">>,<<"hexpm">>},
19 - {<<"requirement">>,<<"~> 0.1.0">>}]]}.
20 - {<<"version">>,<<"0.2.0">>}.
19 + {<<"requirement">>,<<"~> 0.2.0">>}]]}.
20 + {<<"version">>,<<"0.3.0">>}.
  @@ -48,10 +48,17 @@ defmodule Observable.Repo do
48 48 end
49 49
50 50 observations do
51 - on_action(:insert, SubscribersObserver)
51 + action(:insert, [SubscribersObserver])
52 52 end
53 53 end
54 54
55 + The actions must be either `:insert`, `:update`, or `:delete`. We can add as
56 + many observers to a given action as needed. Simply add them to the list. For
57 + example, we can define an observation for a `:delete` action - which will notify
58 + 2 observers:
59 +
60 + action(:delete, [ObserverOne, ObserverTwo])
61 +
55 62 Now that we are starting to use "observable" behaviour, we must modify the way
56 63 in which we insert posts with our repo.
57 64
  @@ -82,8 +89,8 @@ defmodule Observable.Repo do
82 89 Now, lets modify our schema to reflect the updates to our observer.
83 90
84 91 observations do
85 - on_action(:insert, SubscribersObserver)
86 - on_action(:update, SubscribersObserver)
92 + action(:insert, [SubscribersObserver])
93 + action(:update, [SubscribersObserver])
87 94 end
88 95
89 96 Given the above, we can now notify users during updates.
  @@ -94,7 +101,7 @@ defmodule Observable.Repo do
94 101 |> Repo.update_and_notify()
95 102 end
96 103
97 - All of the functionality above can be carried over with a `on_action(:delete, SubscribersObserver)`
104 + All of the functionality above can be carried over with a `action(:delete, [SubscribersObserver])`
98 105 observation and the `c:delete_and_notify/2` function being invoked.
99 106 """
  @@ -1,7 +1,7 @@
1 1 defmodule EctoObserver.MixProject do
2 2 use Mix.Project
3 3
4 - @version "0.2.0"
4 + @version "0.3.0"
5 5
6 6 def project do
7 7 [
  @@ -70,7 +70,7 @@ defmodule EctoObserver.MixProject do
70 70 # Run "mix help deps" to learn about dependencies.
71 71 defp deps do
72 72 [
73 - {:observable, "~> 0.1.0"},
73 + {:observable, "~> 0.2.0"},
74 74 {:ecto, "~> 2.1", only: [:dev, :test]},
75 75 {:postgrex, "~> 0.13.0", only: :test},
76 76 {:ex_doc, ">= 0.0.0", only: :dev}