Class: VirtualKeywords::ClassReflection
- Inherits:
-
Object
- Object
- VirtualKeywords::ClassReflection
- Defined in:
- lib/virtual_keywords/virtualizer.rb
Overview
Utility functions used to inspect the class hierarchy, and to view and modify methods of classes.
Class Method Summary collapse
-
.install_method_on_class(klass, method_code) ⇒ Object
Install a method on a class.
-
.install_method_on_instance(object, method_code) ⇒ Object
Install a method on an object.
-
.instance_methods_of(klass) ⇒ Object
Get the instance_methods of a class.
-
.subclasses_of_class(parent) ⇒ Object
Get the subclasses of a given class.
-
.subclasses_of_classes(klasses) ⇒ Object
Given an array of base classes, return a flat array of all their subclasses.
Class Method Details
.install_method_on_class(klass, method_code) ⇒ Object
Install a method on a class. When object.method_name is called (for objects in the class), have them run the given code. TODO Should it be possible to recover the old method? How would that API look?
Arguments:
klass: (Class) the class which should be modified.
method_code: (String) the code for the method to install, of the format:
def method_name(args)
...
end
64 65 66 |
# File 'lib/virtual_keywords/virtualizer.rb', line 64 def self.install_method_on_class(klass, method_code) klass.class_eval method_code end |
.install_method_on_instance(object, method_code) ⇒ Object
Install a method on an object. When object.method_name is called, runs the given code.
This function can also be used for classmethods. For example, if you want to rewrite Klass.method_name (a method on Klass, a singleton Class), call this method (NOT install_method_on_class, that will modifiy objects created through Klass.new!)
Arguments:
object: (Object) the object instance that should be modified.
method_code: (String) the code for the method to install, of the format:
def method_name(args)
...
end
82 83 84 |
# File 'lib/virtual_keywords/virtualizer.rb', line 82 def self.install_method_on_instance(object, method_code) object.instance_eval method_code end |
.instance_methods_of(klass) ⇒ Object
Get the instance_methods of a class.
Arguments:
klass: (Class) the class.
Returns:
(Hash[Symbol, Array]) A hash, mapping method names to the results of
ParseTree.translate.
43 44 45 46 47 48 49 50 51 |
# File 'lib/virtual_keywords/virtualizer.rb', line 43 def self.instance_methods_of(klass) methods = {} klass.instance_methods(false).each do |method_name| translated = ParseTree.translate(klass, method_name) methods[method_name] = translated end methods end |
.subclasses_of_class(parent) ⇒ Object
Get the subclasses of a given class.
Arguments:
parent: (Class) the class whose subclasses to find.
Returns:
(Array) all classes which are subclasses of parent.
14 15 16 17 18 |
# File 'lib/virtual_keywords/virtualizer.rb', line 14 def self.subclasses_of_class(parent) ObjectSpace.each_object(Class).select { |klass| klass < parent } end |
.subclasses_of_classes(klasses) ⇒ Object
Given an array of base classes, return a flat array of all their subclasses.
Arguments:
klasses: (Array[Class]) an array of classes
Returns:
(Array) All classes that are subclasses of one of the classes in klasses,
in a flattened array.
29 30 31 32 33 |
# File 'lib/virtual_keywords/virtualizer.rb', line 29 def self.subclasses_of_classes(klasses) klasses.map { |klass| subclasses_of_class klass }.flatten end |