Skip to content

Commit

Permalink
Merge pull request #3 from TPei/tpei/notify_oberservers_bang
Browse files Browse the repository at this point in the history
deprecate alert_observers in favor of notify_observers!
  • Loading branch information
TPei authored Jun 15, 2020
2 parents e754473 + 085922a commit 1875ad1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This provides you with the following methods:
- `notify_observers`

and as an added bonus, because I dislike the forced `changed` call precondition:
- `alert_observers`, which notifies no matter if changed or not
- `notify_observers!`, which notifies no matter if changed or not


Then you can include observer in the class you want observing:
Expand Down
59 changes: 48 additions & 11 deletions spec/observer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ describe "Observable" do
it "adds and observer to the observers array" do
to = ToObserve.new
obs = Observing.new

to.count_observers.should eq 0
to.add_observer obs
to.count_observers.should eq 1
end
end

describe "#count_observers" do
it "returns the number of listed observers" do
to = ToObserve.new
obs = Observing.new

to.count_observers.should eq 0
to.add_observer obs
to.count_observers.should eq 1
Expand Down Expand Up @@ -127,7 +127,7 @@ describe "Observable" do
to.count_observers.should eq 2
to.changed
to.notify_observers

obs.notified?.should eq true
obs2.notified?.should eq true
end
Expand All @@ -144,14 +144,14 @@ describe "Observable" do
to.add_observer obs2
to.count_observers.should eq 2
to.notify_observers

obs.notified?.should eq false
obs2.notified?.should eq false
end
end
end
describe "#alert_observers" do

describe "#notify_observers!" do
context "changed is set" do
it "calls notify on all observing objects" do
to = ToObserve.new
Expand All @@ -163,14 +163,34 @@ describe "Observable" do
to.add_observer obs2
to.count_observers.should eq 2
to.changed
to.notify_observers
to.notify_observers!

obs.notified?.should eq true
obs2.notified?.should eq true
end
end

context "changed is not set" do
it "calls notify on all observing objects" do
to = ToObserve.new
obs = Observing.new
obs2 = Observing.new

to.count_observers.should eq 0
to.add_observer obs
to.add_observer obs2
to.count_observers.should eq 2
to.notify_observers!

obs.notified?.should eq true
obs2.notified?.should eq true
end
end
end

describe "#alert_observers" do
# #alert_observers is deprecated, use #notify_observers! instead
context "changed is set" do
it "calls notify on all observing objects" do
to = ToObserve.new
obs = Observing.new
Expand All @@ -181,8 +201,25 @@ describe "Observable" do
to.add_observer obs2
to.count_observers.should eq 2
to.changed
to.notify_observers

to.alert_observers

obs.notified?.should eq true
obs2.notified?.should eq true
end
end

context "changed is not set" do
it "calls notify on all observing objects" do
to = ToObserve.new
obs = Observing.new
obs2 = Observing.new

to.count_observers.should eq 0
to.add_observer obs
to.add_observer obs2
to.count_observers.should eq 2
to.alert_observers

obs.notified?.should eq true
obs2.notified?.should eq true
end
Expand Down
7 changes: 6 additions & 1 deletion src/observable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ module Observable
@changed = false
end

def alert_observers
def notify_observers!
@observers.each do |observer|
observer.update(self)
end
end

@[Deprecated("Use `#notify_observers!` instead")]
def alert_observers
notify_observers!
end
end

0 comments on commit 1875ad1

Please sign in to comment.