-
-
Save maccman/150269 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'observable' | |
class ObservableArray | |
include Observable | |
def initialize(array = []) | |
@array = array | |
end | |
protected | |
def method_missing(method, *args, &block) | |
if @array.respond_to?(method) | |
current_hash = @array.hash | |
@array.send(method, *args, &block) | |
notify_changes if @array.hash != current_hash | |
else | |
super | |
end | |
end | |
private | |
def notify_changes | |
changed(true) and notify_observers(self) | |
end | |
end | |
# class MyObserver | |
# def update(array) | |
# puts "Array #{array} has changed" | |
# end | |
# end | |
# | |
# a = ObservableArray.new | |
# o = MyObserver.new | |
# a.add_observer(o) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment