no_backsies 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -25,24 +25,27 @@ project.
25
25
  include NoBacksies
26
26
 
27
27
  def self.doc(string)
28
- callback :method_added do |method|
28
+ callback :method_added, :once=>true do |method|
29
29
  self.ann(method, :doc=>string)
30
30
  end
31
31
  end
32
32
 
33
- doc "here"
34
-
35
- def foo; end
33
+ doc "foo is cool"
34
+ def foo
35
+ # ...
36
+ end
36
37
  end
37
38
 
38
- Y.ann(:foo, :doc) #=> "here"
39
+ Y.ann(:foo, :doc) #=> "foo is cool"
40
+
41
+ See the QED documentation for more examples.
39
42
 
40
43
 
41
44
  == INSTALLATION
42
45
 
43
- Install the RubyGems package in the usual fahsion.
46
+ Install the RubyGems package in the usual fashion.
44
47
 
45
- $ gem install nobacksies
48
+ $ gem install no_backsies
46
49
 
47
50
 
48
51
  == LEGAL
@@ -53,4 +56,4 @@ Copyright (c) 2011 Thomas Sawyer
53
56
 
54
57
  Unless otherwise negotiated with the original author, NoBacksies is
55
58
  distributable under the terms of the Apache 2.0 license. See the
56
- Apache2.txt file for details.
59
+ APACHE2.txt file for details.
data/lib/no_backsies.rb CHANGED
@@ -82,8 +82,8 @@ module NoBacksies
82
82
 
83
83
  module CallbackMethods
84
84
  # Define a callback.
85
- def callback(name, express={}, &block)
86
- callbacks[name.to_sym] << block
85
+ def callback(name, options={}, &block)
86
+ callbacks[name.to_sym] << [block, options]
87
87
  end
88
88
 
89
89
  #
@@ -113,6 +113,24 @@ module NoBacksies
113
113
 
114
114
  @_callback_express
115
115
  end
116
+
117
+ # Invoke a callback.
118
+ def callback_invoke(name, *args)
119
+ name = name.to_sym
120
+ return unless callback_express[name]
121
+ callbacks[name].each do |block, options|
122
+ if options[:safe]
123
+ callback_express(name=>false) do
124
+ block.call(*args)
125
+ end
126
+ else
127
+ block.call(*args)
128
+ end
129
+ if options[:once]
130
+ callbacks[name].delete([block, options])
131
+ end
132
+ end
133
+ end
116
134
  end
117
135
 
118
136
  # Callback system for #method_added.
@@ -125,10 +143,7 @@ module NoBacksies
125
143
 
126
144
  #
127
145
  def method_added(method)
128
- return unless callback_express[:method_added]
129
- callbacks[:method_added].each do |block|
130
- block.call(method)
131
- end
146
+ callback_invoke(:method_added, method)
132
147
  end
133
148
  end
134
149
 
@@ -142,10 +157,7 @@ module NoBacksies
142
157
 
143
158
  #
144
159
  def method_removed(method)
145
- return unless callback_express[:method_removed]
146
- callbacks[:method_removed].each do |block|
147
- block.call(method)
148
- end
160
+ callback_invoke(:method_removed, method)
149
161
  end
150
162
  end
151
163
 
@@ -159,10 +171,7 @@ module NoBacksies
159
171
 
160
172
  #
161
173
  def method_undefined(method)
162
- return unless callback_express[:method_undefined]
163
- callbacks[:method_undefined].each do |block|
164
- block.call(method)
165
- end
174
+ callback_invoke(:method_undefined, method)
166
175
  end
167
176
  end
168
177
 
@@ -176,10 +185,7 @@ module NoBacksies
176
185
 
177
186
  #
178
187
  def singleton_method_added(method)
179
- return unless callback_express[:singleton_method_added]
180
- callbacks[:singleton_method_added].each do |block|
181
- block.call(method)
182
- end
188
+ callback_invoke(:singleton_method_added, method)
183
189
  end
184
190
  end
185
191
 
@@ -193,10 +199,7 @@ module NoBacksies
193
199
 
194
200
  #
195
201
  def singleton_method_removed(method)
196
- return unless callback_express[:singleton_method_removed]
197
- callbacks[:singleton_method_removed].each do |block|
198
- block.call(method)
199
- end
202
+ callback_invoke(:singleton_method_removed, method)
200
203
  end
201
204
  end
202
205
 
@@ -210,10 +213,7 @@ module NoBacksies
210
213
 
211
214
  #
212
215
  def singleton_method_undefined(method)
213
- return unless callback_express[:singleton_method_undefined]
214
- callbacks[:singleton_method_undefined].each do |block|
215
- block.call(method)
216
- end
216
+ callback_invoke(:singleton_method_undefined, method)
217
217
  end
218
218
  end
219
219
 
@@ -227,10 +227,7 @@ module NoBacksies
227
227
 
228
228
  #
229
229
  def const_missing(const)
230
- return unless callback_express[:cont_missing]
231
- callbacks[:const_missing].each do |block|
232
- block.call(const)
233
- end
230
+ callback_invoke(:const_missing, method)
234
231
  end
235
232
  end
236
233
 
@@ -244,10 +241,7 @@ module NoBacksies
244
241
 
245
242
  #
246
243
  def included(mod)
247
- return unless callback_express[:included]
248
- callbacks[:included].each do |block|
249
- block.call(mod)
250
- end
244
+ callback_invoke(:included, method)
251
245
  end
252
246
  end
253
247
 
@@ -261,10 +255,7 @@ module NoBacksies
261
255
 
262
256
  #
263
257
  def extended(mod)
264
- return unless callback_express[:extended]
265
- callbacks[:extended].each do |block|
266
- block.call(mod)
267
- end
258
+ callback_invoke(:extended, method)
268
259
  end
269
260
  end
270
261
 
@@ -278,10 +269,7 @@ module NoBacksies
278
269
 
279
270
  #
280
271
  def extended(mod)
281
- return unless callback_express[:inherited]
282
- callbacks[:inherited].each do |block|
283
- block.call(mod)
284
- end
272
+ callback_invoke(:inherited, method)
285
273
  end
286
274
  end
287
275
 
data/qed/01_example.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = Basic Example
2
2
 
3
- First require the 'nobacksies' library.
3
+ First require the NoBacksies library.
4
4
 
5
5
  require 'no_backsies'
6
6
 
data/qed/02_express.rdoc CHANGED
@@ -11,10 +11,6 @@ using the +callback_express+ method.
11
11
  class Z
12
12
  include NoBacksies::Callbacks
13
13
 
14
- def self.list
15
- @list ||= []
16
- end
17
-
18
14
  callback :method_added do |method|
19
15
  callback_express :method_added=>false do
20
16
  define_method("#{method}!") do
@@ -0,0 +1,56 @@
1
+ = Callback Options
2
+
3
+ == Once Callback Option
4
+
5
+ NoBacksies supports special callback options. The first is +:once+.
6
+ By setting +:once+ to +true+, the callback will be used one time and
7
+ then removed.
8
+
9
+ class Q
10
+ include NoBacksies::Callbacks
11
+
12
+ def self.list
13
+ @list ||= []
14
+ end
15
+
16
+ callback :method_added, :once=>true do |method|
17
+ list << method
18
+ end
19
+
20
+ def foo; "foo"; end
21
+ def bar; "bar"; end
22
+ end
23
+
24
+ Here we see that only :foo has been added to the list.
25
+
26
+ Q.list #=> [:foo]
27
+
28
+ == Safe Callback Option
29
+
30
+ The other option supported is +:safe+. When +:safe+ is set to true
31
+ the callback is automatically wrapped in an #callback_express block
32
+ that sets the expression of the callback to false while it is being
33
+ processed. This means we can rewrite our first example more succinctly.
34
+
35
+ class Z2
36
+ include NoBacksies::Callbacks
37
+
38
+ callback :method_added, :safe=>true do |method|
39
+ define_method("#{method}!") do
40
+ send(method) + "!"
41
+ end
42
+ end
43
+
44
+ def foo; "foo"; end
45
+ def bar; "bar"; end
46
+ end
47
+
48
+ In this example, a new `Z` object will get an automatically defined bang method
49
+ for every explicitly defined method.
50
+
51
+ z = Z2.new
52
+ z.foo #=> "foo"
53
+ z.foo! #=> "foo!"
54
+ z.bar #=> "bar"
55
+ z.bar! #=> "bar!"
56
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: no_backsies
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Thomas Sawyer
@@ -62,6 +62,7 @@ files:
62
62
  - lib/no_backsies.rb
63
63
  - qed/01_example.rdoc
64
64
  - qed/02_express.rdoc
65
+ - qed/03_options.rdoc
65
66
  - qed/applique/no_backsies.rb
66
67
  - README.rdoc
67
68
  has_rdoc: true