Module: Mirrors
- Extended by:
- Mirrors
- Included in:
- Mirrors
- Defined in:
- lib/mirrors.rb,
lib/mirrors/invoke.rb,
lib/mirrors/mirror.rb,
lib/mirrors/class_mirror.rb,
lib/mirrors/field_mirror.rb,
lib/mirrors/index/marker.rb,
lib/mirrors/index/indexer.rb,
lib/mirrors/method_mirror.rb,
lib/mirrors/object_mirror.rb,
lib/mirrors/package_mirror.rb,
lib/mirrors/package_inference.rb,
lib/mirrors/visitors/iseq_visitor.rb,
lib/mirrors/visitors/disasm_visitor.rb,
lib/mirrors/visitors/references_visitor.rb,
lib/mirrors/field_mirror/constant_mirror.rb,
lib/mirrors/field_mirror/class_variable_mirror.rb,
lib/mirrors/field_mirror/instance_variable_mirror.rb,
lib/mirrors/package_inference/class_to_file_resolver.rb
Defined Under Namespace
Modules: Index, PackageInference
Classes: ClassMirror, ClassVariableMirror, ConstantMirror, DisasmVisitor, FieldMirror, ISeqVisitor, InstanceVariableMirror, Marker, MethodMirror, Mirror, ObjectMirror, PackageMirror, ReferencesVisitor
Constant Summary
collapse
- ProjectRootNotFound =
Class.new(StandardError)
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.class_singleton_invoke(receiver, msg) ⇒ Object
23
24
25
|
# File 'lib/mirrors/invoke.rb', line 23
def self.class_singleton_invoke(receiver, msg)
class_singleton_method(msg).bind(receiver).call
end
|
.class_singleton_method(msg) ⇒ Object
27
28
29
|
# File 'lib/mirrors/invoke.rb', line 27
def self.class_singleton_method(msg)
@unbound_class_singleton_methods[msg] ||= Class.method(msg).unbind
end
|
.kernel_instance_invoke(receiver, msg) ⇒ Object
31
32
33
|
# File 'lib/mirrors/invoke.rb', line 31
def self.kernel_instance_invoke(receiver, msg)
kernel_instance_method(msg).bind(receiver).call
end
|
.kernel_instance_method(msg) ⇒ Object
35
36
37
|
# File 'lib/mirrors/invoke.rb', line 35
def self.kernel_instance_method(msg)
@unbound_kernel_instance_methods[msg] ||= Kernel.instance_method(msg)
end
|
.module_instance_invoke(receiver, msg) ⇒ Object
15
16
17
|
# File 'lib/mirrors/invoke.rb', line 15
def self.module_instance_invoke(receiver, msg)
module_instance_method(msg).bind(receiver).call
end
|
.module_instance_method(msg) ⇒ Object
19
20
21
|
# File 'lib/mirrors/invoke.rb', line 19
def self.module_instance_method(msg)
@unbound_module_instance_methods[msg] ||= Module.instance_method(msg)
end
|
Instance Method Details
This method can be used to query the system for known classes. It is not guaranteed that all possible classes are returned.
55
56
57
|
# File 'lib/mirrors.rb', line 55
def classes
instances_of(Class).sort! { |a, b| a.name <=> b.name }
end
|
#implementations_of(str) ⇒ Array<MethodMirror>
Query the system for implementors of a particular message
78
79
80
81
82
83
84
85
86
|
# File 'lib/mirrors.rb', line 78
def implementations_of(str)
methods = ObjectSpace.each_object(Module).collect do |m|
ims = m.instance_methods(false).collect { |s| m.instance_method(s) }
cms = m.methods(false).collect { |s| m.method(s) }
ims + cms
end.flatten
mirrors(methods.select { |m| m.name.to_s == str.to_s })
end
|
#instances_of(klass) ⇒ Array<ObjectMirror>
Query the system for objects that are direct instances of the given class.
63
64
65
|
# File 'lib/mirrors.rb', line 63
def instances_of(klass)
mirrors(ObjectSpace.each_object(klass).select { |obj| obj.class == klass })
end
|
This method can be used to query the system for known modules. It is not guaranteed that all possible modules are returned.
47
48
49
|
# File 'lib/mirrors.rb', line 47
def modules
instances_of(Module).sort! { |a, b| a.name <=> b.name }
end
|
#object_by_id(id) ⇒ ObjectMirror, NilClass
Ask the system to find the object with the given object id
70
71
72
73
|
# File 'lib/mirrors.rb', line 70
def object_by_id(id)
obj = ObjectSpace._id2ref(id)
obj ? reflect(obj) : nil
end
|
#packages ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/mirrors.rb', line 32
def packages
packages = {}
Object.constants.each do |const|
pkg = PackageInference.infer_from_toplevel(const)
packages[pkg] = true
end
toplevel_packages = packages.keys.map { |pkg| pkg.sub(/:.*/, '') }.sort
package_mirrors(toplevel_packages)
end
|
#project_root ⇒ Object
22
23
24
25
26
27
28
29
30
|
# File 'lib/mirrors.rb', line 22
def project_root
if defined?(Bundler)
return File.expand_path(Bundler.root)
end
if Dir.exist?('.git')
return File.expand_path(Dir.pwd)
end
raise ProjectRootNotFound
end
|
#references_to(str) ⇒ Object
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/mirrors.rb', line 88
def references_to(str)
filtered = {}
Mirrors.classes.each do |klass|
klass.methods.each do |m|
refs = m.references.select { |marker| marker.message.match(str) }
filtered[m] = refs unless refs.empty?
end
end
filtered
end
|
#reflect(obj) ⇒ Mirror
Create a mirror for a given object in the system under observation. This is the factory method for all mirror instances, interning and cache invalidation will be added here.