Module: RubyExt::Observable
- Defined in:
- lib/ruby_ext/more/observable.rb
Instance Method Summary collapse
- #add_observer(observer) ⇒ Object
- #delete_observer(observer) ⇒ Object
- #delete_observers ⇒ Object
- #notify_observers(method, *args) ⇒ Object
- #observers_count ⇒ Object
Instance Method Details
#add_observer(observer) ⇒ Object
2 3 4 5 |
# File 'lib/ruby_ext/more/observable.rb', line 2 def add_observer observer @observable_observers ||= [] @observable_observers << observer unless @observable_observers.include? observer end |
#delete_observer(observer) ⇒ Object
12 13 14 |
# File 'lib/ruby_ext/more/observable.rb', line 12 def delete_observer observer @observable_observers.delete observer if @observable_observers end |
#delete_observers ⇒ Object
16 17 18 |
# File 'lib/ruby_ext/more/observable.rb', line 16 def delete_observers @observable_observers.clear if @observable_observers end |
#notify_observers(method, *args) ⇒ Object
7 8 9 10 |
# File 'lib/ruby_ext/more/observable.rb', line 7 def notify_observers method, *args raise "Invalid usage, method must be Symbol or String!" unless method.is_a?(Symbol) or method.is_a?(String) @observable_observers.each{|observer| observer.respond_to method, *args} if @observable_observers end |
#observers_count ⇒ Object
20 21 22 |
# File 'lib/ruby_ext/more/observable.rb', line 20 def observers_count @observable_observers ? @observable_observers.size : 0 end |