Skip to content
This repository has been archived by the owner on Sep 30, 2018. It is now read-only.

Commit

Permalink
Rename observe/unobserve to add_observer/remove_observer
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Sep 13, 2013
1 parent ea44b59 commit 7ca95e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -218,6 +218,33 @@ end
# => need to show user: 5
```

## Observable

Adds KVO style attribute observing.

```ruby
class MyObject
include Vienna::Observable

attr_accessor :name
attr_reader :age

def age=(age)
@age = age + 10
end
end

obj = MyObject.new
obj.add_observer(:name) { |new_val| puts "name changed to #{new_val}" }
obj.add_observer(:age) { |new_age| puts "age changed to #{new_age}" }

obj.name = "bob"
obj.age = 42

# => "name changed to bob"
# => "age changed to 52"
```

#### Todo

* Support older browsers which do not support onhashchange.
Expand Down
4 changes: 2 additions & 2 deletions opal/vienna/observable.rb
@@ -1,7 +1,7 @@
module Vienna
module Observable

def observe(attribute, &handler)
def add_observer(attribute, &handler)
unless observers = @attr_observers
observers = @attr_observers = {}
old_values = @attr_old_values = {}
Expand All @@ -15,7 +15,7 @@ def observe(attribute, &handler)
handlers << handler
end

def unobserve(attribute, handler)
def remove_observer(attribute, handler)
return unless @attr_observers

if handlers = @attr_observers[attribute]
Expand Down
24 changes: 12 additions & 12 deletions spec/observable_spec.rb
Expand Up @@ -47,10 +47,10 @@ def object.attribute_did_change(attribute); @_called = attribute; end
end
end

describe "#observe" do
describe "#add_observer" do
it "handlers can be added to observe specific attributes" do
count = 0
object.observe(:foo) { count += 1 }
object.add_observer(:foo) { count += 1 }

object.foo = 100
count.should eq(1)
Expand All @@ -61,8 +61,8 @@ def object.attribute_did_change(attribute); @_called = attribute; end

it "allows more than one handler to be added for an attribute" do
result = []
object.observe(:foo) { result << :first }
object.observe(:foo) { result << :second }
object.add_observer(:foo) { result << :first }
object.add_observer(:foo) { result << :second }

object.foo = 42
result.should eq([:first, :second])
Expand All @@ -78,13 +78,13 @@ def object.attribute_did_change(attribute); @_called = attribute; end
end

it "does not break when given an attriubte with no observers (but another observable exists)" do
object.observe(:foo) {}
object.add_observer(:foo) {}
object.attribute_did_change(:bar)
end

it "passes new value to each handler" do
result = nil
object.observe(:foo) { |val| result = val }
object.add_observer(:foo) { |val| result = val }

object.foo = 42
result.should eq(42)
Expand All @@ -94,26 +94,26 @@ def object.attribute_did_change(attribute); @_called = attribute; end
end
end

describe "#unobserve" do
describe "#remove_observer" do
it "has no effect when no observers setup for attribute" do
object.unobserve(:foo, proc {})
object.remove_observer(:foo, proc {})
end

it "has no effect if the handler doesnt exist for attribute" do
p = proc {}
object.observe(:foo) {}
object.unobserve(:foo, p)
object.add_observer(:foo) {}
object.remove_observer(:foo, p)
end

it "removes an existing handler for given attribute" do
count = 0
handler = proc { count += 1 }
object.observe(:foo, &handler)
object.add_observer(:foo, &handler)

object.foo = 42
count.should eq(1)

object.unobserve(:foo, handler)
object.remove_observer(:foo, handler)
object.foo = 49
count.should eq(1)
end
Expand Down

0 comments on commit 7ca95e1

Please sign in to comment.