generate_method 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -1
- data/lib/generate_method.rb +19 -5
- data/lib/generate_method/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f3ac34096274543969e535344fd20e166b9a98d
|
4
|
+
data.tar.gz: 6956afd6f173fbf24c19e3f460675a5f38645181
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c98c03142d6b79445b9fc37b7f18a4496fa6502b004c80c1ef6b366d81bdcbe99b24b0739e998c249f19a68c0f25e3c43dba0c4169c12b4641a32a344eff4fc6
|
7
|
+
data.tar.gz: 54712a7a5113ba20af0cc9bd4ee3e6aa1e4a4dfd0e535fe62483843c394d1b9b87b567210d9cabadee76db9ddcfeaf332ef43c2975144afdbe90b72854165a5d
|
data/README.md
CHANGED
@@ -58,6 +58,9 @@ class Module
|
|
58
58
|
end
|
59
59
|
```
|
60
60
|
|
61
|
+
To generate class method (singleton methods) you can use the equivalent
|
62
|
+
`generate_singleton_method` and `generate_singleton_methods`.
|
63
|
+
|
61
64
|
By using one of the above syntax, a few problems will be solved:
|
62
65
|
|
63
66
|
### Ancestors stack
|
@@ -85,7 +88,7 @@ instance.x
|
|
85
88
|
You can add `overrider: <overrider_name>` option to the `generate_method` call. This will
|
86
89
|
cause existing methods in the class to be aliased into
|
87
90
|
`<method_name>_without_<overrider_name>` (pushing ?/!,= to the end of the
|
88
|
-
method name). That way you can easily call the
|
91
|
+
method name). That way you can easily call the overridden method in your
|
89
92
|
generated method.
|
90
93
|
|
91
94
|
```ruby
|
data/lib/generate_method.rb
CHANGED
@@ -3,23 +3,37 @@ require "generate_method/version"
|
|
3
3
|
module GenerateMethod
|
4
4
|
module Generator
|
5
5
|
def generate_method(method_name, overrider: nil, &block)
|
6
|
+
include generate_method_module(method_name, overrider: overrider, &block)
|
7
|
+
end
|
8
|
+
def generate_singleton_method(method_name, overrider: nil, &block)
|
9
|
+
extend generate_method_module(method_name, overrider: overrider, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate_methods(overrider: nil, &block)
|
13
|
+
include generate_block_module(overrider: overrider, &block)
|
14
|
+
end
|
15
|
+
def generate_singleton_methods(overrider: nil, &block)
|
16
|
+
extend generate_block_module(overrider: overrider, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def generate_method_module(method_name, overrider: nil, &block)
|
6
22
|
m = Module.new do
|
7
23
|
define_method(method_name, &block)
|
8
24
|
end
|
9
25
|
alias_generated_method(method_name, overrider: overrider, m: m)
|
10
|
-
|
26
|
+
m
|
11
27
|
end
|
12
28
|
|
13
|
-
def
|
29
|
+
def generate_block_module(overrider: nil, &block)
|
14
30
|
m = Module.new(&block)
|
15
31
|
m.instance_methods.each do |method_name|
|
16
32
|
alias_generated_method(method_name, overrider: overrider, m: m)
|
17
33
|
end
|
18
|
-
|
34
|
+
m
|
19
35
|
end
|
20
36
|
|
21
|
-
private
|
22
|
-
|
23
37
|
def alias_generated_method(method_name, overrider: nil, m: nil)
|
24
38
|
return if overrider.nil?
|
25
39
|
|