Class: Ruby::Reflection::ClassMirror

Inherits:
ObjectMirror show all
Includes:
AbstractReflection::ClassMirror
Defined in:
lib/ruby/reflection/class_mirror.rb

Instance Attribute Summary

Attributes included from AbstractReflection::Mirror

#reflection

Instance Method Summary collapse

Methods included from AbstractReflection::ClassMirror

#instance_variables, #method_dictionary, #namespace, #singleton_instance

Methods included from AbstractReflection::ObjectMirror

#instance_eval, #objects_with_references, #path_to, #target_class, #transitive_closure, #variables

Methods included from AbstractReflection::Mirror

#initialize, #mirrors?, #name, #reflectee

Methods included from AbstractReflection::Mirror::ClassMethods

#included, #mirror_class, #new, #reflect, #reflect!, #reflects?, #register_mirror

Methods inherited from ObjectMirror

#target_class, #variables

Instance Method Details

#ancestorsObject



45
46
47
# File 'lib/ruby/reflection/class_mirror.rb', line 45

def ancestors
  mirrors @subject.ancestors
end

#class_instance_variablesObject



11
12
13
# File 'lib/ruby/reflection/class_mirror.rb', line 11

def class_instance_variables
  field_mirrors @subject.instance_variables
end

#class_variablesObject



7
8
9
# File 'lib/ruby/reflection/class_mirror.rb', line 7

def class_variables
  field_mirrors @subject.class_variables
end

#constant(str) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/ruby/reflection/class_mirror.rb', line 53

def constant(str)
  path = str.to_s.split("::")
  c = path[0..-2].inject(@subject) {|klass,str| klass.const_get(str) }
  field_mirror (c || @subject), path.last
rescue NameError => e
  p e
  nil
end

#constantsObject



49
50
51
# File 'lib/ruby/reflection/class_mirror.rb', line 49

def constants
  field_mirrors @subject.constants
end

#method(name) ⇒ Object



87
88
89
# File 'lib/ruby/reflection/class_mirror.rb', line 87

def method(name)
  reflection.reflect @subject.instance_method(name)
end

#methodsObject



83
84
85
# File 'lib/ruby/reflection/class_mirror.rb', line 83

def methods
  @subject.instance_methods(false).collect(&:to_s)
end

#mixinsObject



32
33
34
# File 'lib/ruby/reflection/class_mirror.rb', line 32

def mixins
  mirrors @subject.ancestors.reject {|m| m.is_a? Class }
end

#nested_classesObject



73
74
75
76
77
78
79
80
81
# File 'lib/ruby/reflection/class_mirror.rb', line 73

def nested_classes
  nc = @subject.constants.collect do |c|
    # do not trigger autoloads
    if @subject.const_defined?(c) and not @subject.autoload?(c)
      @subject.const_get(c)
    end
  end.compact.select {|c| Module === c }
  mirrors nc
end

#nestingObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby/reflection/class_mirror.rb', line 62

def nesting
  ary = []
  @subject.name.split("::").inject(Object) do |klass,str|
    ary << klass.const_get(str)
    ary.last
  end
  ary.reverse
rescue NameError => e
  [@subject]
end

#singleton_classObject



24
25
26
# File 'lib/ruby/reflection/class_mirror.rb', line 24

def singleton_class
  reflection.reflect @subject.singleton_class
end

#singleton_class?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ruby/reflection/class_mirror.rb', line 28

def singleton_class?
  self.name =~ /^\#<Class:.*>$/
end

#source_filesObject



15
16
17
18
19
20
21
22
# File 'lib/ruby/reflection/class_mirror.rb', line 15

def source_files
  locations = @subject.instance_methods(false).collect do |name|
    method = @subject.instance_method(name)
    file   = method.source_location if method.respond_to? :source_location
    file.first if file
  end
  locations.compact.uniq
end

#subclassesObject



40
41
42
43
# File 'lib/ruby/reflection/class_mirror.rb', line 40

def subclasses
  l = ObjectSpace.each_object(Class).select {|a| a.superclass == @subject }
  mirrors l
end

#superclassObject



36
37
38
# File 'lib/ruby/reflection/class_mirror.rb', line 36

def superclass
  reflection.reflect @subject.superclass
end