metaprogrammer 0.0.1
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 +7 -0
- data/bin/metaprogrammer +10 -0
- data/lib/metaprogrammer.rb +63 -0
- data/lib/metaprogrammer/usages.rb +23 -0
- data/lib/version.rb +3 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2a381d9508c34f1f4f2bc6a369bd5e4fb1cdd46a4fa41396d639dacd988e7ebd
|
4
|
+
data.tar.gz: f0786bd8310cfea0f8489cc996bc6de537fcf36ec35dd3b38a80a48dafcc577d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2d5d43631cd5a3d4bd341ca06ed8aef4d8f7c5042a54656cb1b156a1c20821cc6db3906f4b6527675809e9f9fdcb8916ea423537579a7c5736cf64780dcb850
|
7
|
+
data.tar.gz: fc6101bc1fb32dda0136cedd3d2ccb86002d22935401cf8cf5a02c4e8ea480c80f4604428f04fc988433653a428c60bf91b2d2f6e9901ffaed35c1b43c0dc487
|
data/bin/metaprogrammer
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'pry'
|
2
|
+
module Metaprogrammer
|
3
|
+
|
4
|
+
module Core
|
5
|
+
|
6
|
+
def patch_all_methods(klass: self, **opts, &blk)
|
7
|
+
patch_class_methods(klass: klass, **opts, &blk)
|
8
|
+
patch_instance_methods(klass: klass, **opts, &blk)
|
9
|
+
end
|
10
|
+
|
11
|
+
def patch_class_methods(klass: self, **opts, &blk)
|
12
|
+
all_fns = (
|
13
|
+
[klass] | (klass.ancestors - Object.ancestors)
|
14
|
+
).flat_map do |klass_x|
|
15
|
+
klass_x.methods(false)
|
16
|
+
end
|
17
|
+
fns = Metaprogrammer.filter_methods all_fns, **opts
|
18
|
+
fns.each do |fn_name|
|
19
|
+
orig_method = klass.method fn_name
|
20
|
+
klass.singleton_class.send(:define_method, fn_name) do |*args, &caller_blk|
|
21
|
+
blk.call orig_method, *args, &caller_blk
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def patch_instance_methods(klass: self, **opts, &blk)
|
27
|
+
all_fns = (
|
28
|
+
[klass] | (klass.ancestors - Object.ancestors)
|
29
|
+
).flat_map do |klass_x|
|
30
|
+
klass_x.instance_methods(false)
|
31
|
+
end
|
32
|
+
fns = Metaprogrammer.filter_methods all_fns, **opts
|
33
|
+
fns.each do |fn_name|
|
34
|
+
orig_method = klass.instance_method fn_name
|
35
|
+
klass.send(:define_method, fn_name) do |*args, &caller_blk|
|
36
|
+
blk.call orig_method.bind(self), *args, &caller_blk
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
include Core
|
44
|
+
|
45
|
+
require_relative 'metaprogrammer/usages'
|
46
|
+
|
47
|
+
include Usages
|
48
|
+
|
49
|
+
def self.filter_methods(fns, only: nil, except: nil)
|
50
|
+
if only
|
51
|
+
only = Set.new(only)
|
52
|
+
fns = fns.select(&only.method(:member?))
|
53
|
+
end
|
54
|
+
if except
|
55
|
+
except = Set.new(except)
|
56
|
+
fns = fns.reject(&except.method(:member?))
|
57
|
+
end
|
58
|
+
fns
|
59
|
+
end
|
60
|
+
|
61
|
+
extend Metaprogrammer
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Metaprogrammer
|
2
|
+
module Usages
|
3
|
+
|
4
|
+
def rescue_all_methods(*exceptions, klass: self, **opts, &blk)
|
5
|
+
patch_all_methods(klass: klass, **opts) do |orig_method, *args, &caller_blk|
|
6
|
+
begin
|
7
|
+
orig_method.call *args, &caller_blk
|
8
|
+
rescue *exceptions => e
|
9
|
+
blk.call(e)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def memoize(klass: self, **opts, &blk)
|
15
|
+
patch_all_methods(klass: klass, **opts) do |orig_method, *args, &caller_blk|
|
16
|
+
memos = @_memoized_methods ||= {}
|
17
|
+
method_memos = @_memoized_methods[orig_method] ||= {}
|
18
|
+
method_memos[args] ||= orig_method.call *args, &caller_blk
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: metaprogrammer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- max pleaner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ''
|
14
|
+
email: maxpleaner@gmail.com
|
15
|
+
executables:
|
16
|
+
- metaprogrammer
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/metaprogrammer
|
21
|
+
- lib/metaprogrammer.rb
|
22
|
+
- lib/metaprogrammer/usages.rb
|
23
|
+
- lib/version.rb
|
24
|
+
homepage: http://github.com/maxp-edcast/metaprogrammer
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - "~>"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '2.3'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 2.7.3
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.7.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: metaprogramming helpers and common constructs
|
48
|
+
test_files: []
|