Module: MetaInstance::InstanceMethodDefine
- Extended by:
- ActiveSupport::Concern
- Included in:
- Proxy
- Defined in:
- lib/meta_instance/instance_method_define.rb
Overview
This is based on a few things from reference.jumpingmonkey.org/programming_languages/ruby/ruby-metaprogramming.html
allows the adding of methods to instances, but not the entire set of instances for a particular class
Constant Summary collapse
- METHOD_BACKUP_KEY =
when a method is stubbed with snapshot data, we stare the original method prixefixed with this:
"_mata_instance_backup_current_"
Instance Method Summary collapse
-
#backup_method(name) ⇒ Object
backs up a method in case we want to restore it later.
-
#define_method(name, &block) ⇒ Object
Adds methods to a singletonclass define_singleton_method(name, &block) is the same as doing.
-
#instance_override(name, &block) ⇒ Object
backs up and overrides a method.
-
#restore_method(name) ⇒ Object
the original method becomes reaccessible.
Instance Method Details
#backup_method(name) ⇒ Object
backs up a method in case we want to restore it later
34 35 36 37 38 |
# File 'lib/meta_instance/instance_method_define.rb', line 34 def backup_method(name) { alias_method "#{METHOD_BACKUP_KEY}#{name}", name } end |
#define_method(name, &block) ⇒ Object
Adds methods to a singletonclass define_singleton_method(name, &block) is the same as doing
meta_eval
define_method(name, &block)
29 30 31 |
# File 'lib/meta_instance/instance_method_define.rb', line 29 def define_method(name, &block) define_singleton_method(name, &block) end |
#instance_override(name, &block) ⇒ Object
backs up and overrides a method. but don’t override if we already have overridden this method
16 17 18 19 20 21 |
# File 'lib/meta_instance/instance_method_define.rb', line 16 def instance_override(name, &block) unless respond_to?("#{METHOD_BACKUP_KEY}#{name}") backup_method(name) end define_method(name, &block) end |
#restore_method(name) ⇒ Object
the original method becomes reaccessible
41 42 43 44 45 46 47 48 |
# File 'lib/meta_instance/instance_method_define.rb', line 41 def restore_method(name) if respond_to?("#{METHOD_BACKUP_KEY}#{name}") { alias_method name, "#{METHOD_BACKUP_KEY}#{name}" remove_method "#{METHOD_BACKUP_KEY}#{name}" } end end |