github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

shvets / design_patterns_in_ruby

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 165
    • 5
  • Source
  • Commits
  • Network (5)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Implementation of Design Patterns in Ruby — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

added act_as_observable to make dynamic code generation for observables Comment
shvets (author)
Sun Mar 22 08:28:29 -0700 2009
commit  698dcfab8a2d89909d49f8b6207beb7df4563bf1
tree    e63925a79d07d54eb8612e034e06f535a6768960
parent  b739593f4346a72e183c0b2ec95ea3ac5d6baace
design_patterns_in_ruby / behavioral / observer.rb behavioral/observer.rb
100644 92 lines (66 sloc) 1.773 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# observer.rb
 
# Define a one-to-many dependency between objects so that when one object
# changes state, all it's dependents are notified and updated automatically.
 
# 1. observer
 
class Observer
  def initialize(name)
    @name = name
  end
 
  def update(value)
    puts "updated " + @name + ". New value: " + value
  end
end
 
# 2. Observable, serves as a container for observers and takes care of notifying them
 
module Observable
  def initialize
    @observers = []
  end
 
  def <<(observer)
    @observers << observer
  end
  
  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)
    @observers.clone.each do |observer|
      observer.update(value) if observer.kind_of? Observer
      observer.call(value) if observer.kind_of? Proc
    end
  end
end
 
# 3. test
 
class Tester
  include Observable
 
  attr_accessor :property
  act_as_observable :property
end
 
observer1 = Observer.new("n1")
observer2 = Observer.new("n2")
observer3 = Observer.new("n3")
observer4 = lambda { |value| puts "updated from lambda (update is :#{value})" }
 
tester = Tester.new
 
tester << observer1
tester << observer2
tester << observer3
tester << observer4
 
tester.property = 'red'
 
puts '----------'
 
tester >> observer3
 
tester.property = 'green'
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server