Module: Metaprogrammer::Core
- Included in:
- Metaprogrammer
- Defined in:
- lib/metaprogrammer.rb
Instance Method Summary collapse
- #patch_all_methods(klass: self, **opts, &blk) ⇒ Object
- #patch_class_methods(klass: self, **opts, &blk) ⇒ Object
- #patch_instance_methods(klass: self, **opts, &blk) ⇒ Object
Instance Method Details
#patch_all_methods(klass: self, **opts, &blk) ⇒ Object
6 7 8 9 |
# File 'lib/metaprogrammer.rb', line 6 def patch_all_methods(klass: self, **opts, &blk) patch_class_methods(klass: klass, **opts, &blk) patch_instance_methods(klass: klass, **opts, &blk) end |
#patch_class_methods(klass: self, **opts, &blk) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/metaprogrammer.rb', line 11 def patch_class_methods(klass: self, **opts, &blk) all_fns = ( [klass] | (klass.ancestors - Object.ancestors) ).flat_map do |klass_x| klass_x.methods(false) end fns = Metaprogrammer.filter_methods all_fns, **opts fns.each do |fn_name| orig_method = klass.method fn_name klass.singleton_class.send(:define_method, fn_name) do |*args, &caller_blk| blk.call orig_method, *args, &caller_blk end end end |
#patch_instance_methods(klass: self, **opts, &blk) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/metaprogrammer.rb', line 26 def patch_instance_methods(klass: self, **opts, &blk) all_fns = ( [klass] | (klass.ancestors - Object.ancestors) ).flat_map do |klass_x| klass_x.instance_methods(false) end fns = Metaprogrammer.filter_methods all_fns, **opts fns.each do |fn_name| orig_method = klass.instance_method fn_name klass.send(:define_method, fn_name) do |*args, &caller_blk| blk.call orig_method.bind(self), *args, &caller_blk end end end |