metaprogrammer 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +77 -0
- data/lib/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d299382d0bbbd8d3262540fb1858d56137e4eb7021762621a50a874a501de2d
|
4
|
+
data.tar.gz: 7102fa8d9a6e9ba6944fc6072109818bc83353aff4bfd7cc9b877812079886c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f596ed55a57c676a7122fb603bba2094903445035e10eb7addd0ed081875b6c12e7424cae75baab3e7bd1506eeacaee9f8c1aaa4948cdaad8c82e15f5170d9fd
|
7
|
+
data.tar.gz: 4e981efd236ce35674193f2d30c96c9ff25c4d5b1327d4ee00951c45906309cb27a75a74841f6fe447798c701bee5b6029a4cfb07e4954ce1b3cbb62bd19fd8d
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
## Metaprogrammer
|
2
|
+
|
3
|
+
---
|
4
|
+
|
5
|
+
### Install
|
6
|
+
|
7
|
+
Install from rubygems, the gem is named 'metaprogrammer'.
|
8
|
+
|
9
|
+
### Usage
|
10
|
+
|
11
|
+
`Metaprogrammer` is a module. There are two main methods it exposes:
|
12
|
+
|
13
|
+
- `patch_class_methods`
|
14
|
+
- `patch_instance_methods`.
|
15
|
+
|
16
|
+
There is also a `patch_all_methods` method that has the same signature
|
17
|
+
as the other two, and calls them both.
|
18
|
+
|
19
|
+
An example usage looks like this:
|
20
|
+
|
21
|
+
```
|
22
|
+
class MyClass
|
23
|
+
extend Metaprogrammer
|
24
|
+
def foo
|
25
|
+
1
|
26
|
+
end
|
27
|
+
def self.foo
|
28
|
+
1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
MyClass.patch_all_methods do |orig_method, **args, &blk|
|
33
|
+
orig_method.call(**args, &blk) + 1
|
34
|
+
end
|
35
|
+
|
36
|
+
MyClass.foo # => 2
|
37
|
+
MyClass.new.foo # => 2
|
38
|
+
```
|
39
|
+
|
40
|
+
You can manipulate the arguments or the return value.
|
41
|
+
|
42
|
+
A couple example usages are included with the gem:
|
43
|
+
|
44
|
+
```
|
45
|
+
# Rescue all errors
|
46
|
+
MyClass.rescue_all_methods(RuntimeError) do |error|
|
47
|
+
puts "#{error.class} was raised!"
|
48
|
+
end
|
49
|
+
|
50
|
+
# Memoize all methods
|
51
|
+
MyClass.memoize # memoizes all methods.
|
52
|
+
```
|
53
|
+
|
54
|
+
The implementations of these methods are pretty simple
|
55
|
+
(assuming these are called in class scope of the method which extends Metaprogrammer)
|
56
|
+
|
57
|
+
```
|
58
|
+
# rescue all methods
|
59
|
+
patch_all_methods do |orig_method, *args, &caller_blk|
|
60
|
+
begin
|
61
|
+
orig_method.call *args, &caller_blk
|
62
|
+
rescue *exceptions => e
|
63
|
+
blk.call(e)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# memoize methods
|
68
|
+
patch_all_methods do |orig_method, *args, &caller_blk|
|
69
|
+
memos = @_memoized_methods ||= {}
|
70
|
+
method_memos = @_memoized_methods[orig_method] ||= {}
|
71
|
+
method_memos[args] ||= orig_method.call *args, &caller_blk
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
`patch_all_methods`, `patch_class_methods`, and `patch_instance_methods` can all
|
76
|
+
be configured with `only:` and `except:` options. These should be given
|
77
|
+
as arrays of symbols corresponding to method names.
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metaprogrammer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- max pleaner
|
@@ -17,6 +17,7 @@ executables:
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- README.md
|
20
21
|
- bin/metaprogrammer
|
21
22
|
- lib/metaprogrammer.rb
|
22
23
|
- lib/metaprogrammer/usages.rb
|