Take the 2008 Git User's Survey and help out! [ hide ]

public
Description: Practical Ruby Projects's first chapter
Clone URL: git://github.com/rubbish/livemidi.git
Search Repo:
Added hook for initialize
rubbish (author)
Sun Jul 06 19:22:06 -0700 2008
commit  4c4da4b09ed975fd08b9336443035ddb993075a6
tree    7f0816abad2c94372638a5795a5b5ddd6f1ae1fc
parent  2f26661aeabaccd78b788a0f55123ea0f4577a85
...
3
4
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
7
8
...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -3,6 +3,22 @@ class LiveMidi
0
   OFF = 0x80
0
   PROGRAM_CHANGE = 0xC0
0
 
0
+ @@hooks = { }
0
+ def self.add_hook_for(step, method_name)
0
+ @@hooks[step] ||= []
0
+ @@hooks[step] << method_name
0
+ end
0
+
0
+ def run_hooks(step)
0
+ if @@hooks[step]
0
+ @@hooks[step].each { |method_name| self.send(method_name.to_sym) }
0
+ end
0
+ end
0
+
0
+ def initialize
0
+ run_hooks :initialize
0
+ end
0
+
0
   def note_on(channel, note, velocity = 64)
0
     message(ON | channel, note, velocity)
0
   end
...
103
104
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
107
...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
0
@@ -103,5 +103,23 @@ describe LiveMidi do
0
     end
0
       
0
   end
0
+
0
+ describe "hooks for initialize" do
0
+
0
+ it "should add a hook" do
0
+ class LiveMidi
0
+ add_hook_for :initialize, :open
0
+
0
+ attr_reader :open_called
0
+ def open
0
+ @open_called = true
0
+ end
0
+ end
0
+
0
+ livemidi = LiveMidi.new
0
+ livemidi.open_called.should be_true
0
+ end
0
+
0
+ end
0
   
0
 end

Comments

    No one has commented yet.