chainable 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -6
- data/lib/chainable.rb +10 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -3,13 +3,13 @@
|
|
3
3
|
This is heavy ruby abuse. It even got the Evil of the Day Award™ from zenspider.
|
4
4
|
|
5
5
|
== Thou shalt not use alias_method_chain!
|
6
|
-
- http://yehudakatz.com/2009/03/06/alias_method_chain-in-models
|
7
|
-
- http://yehudakatz.com/2009/01/18/other-ways-to-wrap-a-method
|
6
|
+
- http://yehudakatz.com/2009/03/06/alias_method_chain-in-models
|
7
|
+
- http://yehudakatz.com/2009/01/18/other-ways-to-wrap-a-method
|
8
8
|
- http://www.codefluency.com/articles/2009/01/03/wrapping-a-method-in-ruby
|
9
9
|
|
10
10
|
== What it does
|
11
11
|
Chainable is an alternative to alias_method_chain, that uses inheritance, rather
|
12
|
-
than aliasing.
|
12
|
+
than aliasing. It does the following when "chaining" a method:
|
13
13
|
|
14
14
|
- copy the original method to a new model
|
15
15
|
- include the model
|
@@ -17,8 +17,8 @@ than aliasing. Instead it does the following when "chaining" a method:
|
|
17
17
|
|
18
18
|
Thus you can use super and keep your method list clean, too!
|
19
19
|
It even supports a (rather dangerous) auto chaining mode, so you do not have
|
20
|
-
to explicitly chain a method, but chain a method
|
21
|
-
overwritten.
|
20
|
+
to explicitly chain a method, but chain a method whenever it would be
|
21
|
+
overwritten instead.
|
22
22
|
|
23
23
|
|
24
24
|
Example:
|
@@ -64,4 +64,11 @@ Of course you can do this with any class (or module):
|
|
64
64
|
return super if block_given? or RUBY_VERSION >= "1.8.7"
|
65
65
|
MyStuff::Enumerator.new self, :each
|
66
66
|
end
|
67
|
-
end
|
67
|
+
end
|
68
|
+
|
69
|
+
== Installation
|
70
|
+
Add github gems, if you haven't already:
|
71
|
+
gem sources -a http://gems.github.com
|
72
|
+
|
73
|
+
Install gem:
|
74
|
+
gem install rkh-chainable
|
data/lib/chainable.rb
CHANGED
@@ -9,6 +9,11 @@ module Chainable
|
|
9
9
|
@auto_chain = false
|
10
10
|
end
|
11
11
|
|
12
|
+
# This will "chain" a method (read: push it to a module and include it).
|
13
|
+
# If a block is given, it will do a define_method(name, &block).
|
14
|
+
# Maybe that is not what you want, as methods defined by def tend to be
|
15
|
+
# faster. If that is the case, simply don't pass the block and call def
|
16
|
+
# after chain_method instead.
|
12
17
|
def chain_method(name, &block)
|
13
18
|
name = name.to_s
|
14
19
|
if instance_methods(false).include? name
|
@@ -24,7 +29,11 @@ module Chainable
|
|
24
29
|
define_method(name, &block)
|
25
30
|
end
|
26
31
|
|
27
|
-
|
32
|
+
# If you define a method inside a block passed to auto_chain, chain_method
|
33
|
+
# will be called on that method right after it has been defined. This will
|
34
|
+
# only affect methods defined for the class (or module) auto_chain has been
|
35
|
+
# send to. See README.rdoc or spec/chainable/auto_chain_spec.rb for examples.
|
36
|
+
def auto_chain
|
28
37
|
class << self
|
29
38
|
chain_method :method_added do |name|
|
30
39
|
Chainable.skip_chain { chain_method name }
|