Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added act_as_observable to make dynamic code generation for observables

  • Loading branch information...
commit 698dcfab8a2d89909d49f8b6207beb7df4563bf1 1 parent b739593
@shvets shvets authored
Showing with 23 additions and 5 deletions.
  1. +23 −5 behavioral/observer.rb
View
28 behavioral/observer.rb
@@ -30,6 +30,27 @@ def >>(observer)
@observers.delete(observer)
end
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def act_as_observable *list
+ list.each do |observable|
+ method_name = "#{observable.to_s}="
+ no_callback_method_name = "no_callback_#{observable.to_s}="
+
+ alias_method no_callback_method_name, method_name
+
+ define_method method_name do |value|
+ send no_callback_method_name, value
+
+ notify_observers(value)
+ end
+ end
+ end
+ end
+
protected
def notify_observers(value)
@@ -45,11 +66,8 @@ def notify_observers(value)
class Tester
include Observable
- def property= property
- @property = property
-
- notify_observers(property)
- end
+ attr_accessor :property
+ act_as_observable :property
end
observer1 = Observer.new("n1")
Please sign in to comment.
Something went wrong with that request. Please try again.