class Mirrors::ClassMirror

A specific mirror for a class, that includes all the capabilites and information we can gather about classes.

Constants

MODULE_INSPECT

to work around overridden `name` methods

Public Class Methods

new(obj) click to toggle source
Calls superclass method
# File lib/mirrors/class_mirror.rb, line 5
def initialize(obj)
  super(obj)
  @field_mirrors = {}
  @method_mirrors = {}
end

Public Instance Methods

ancestors() click to toggle source

The list of ancestors

@return [Array<ClassMirror>]

# File lib/mirrors/class_mirror.rb, line 87
def ancestors
  mirrors(@subject.ancestors)
end
class_instance_variables() click to toggle source

The known class variables. @see instance_variables @return [Array<FieldMirror>]

# File lib/mirrors/class_mirror.rb, line 33
def class_instance_variables
  field_mirrors(@subject.instance_variables)
end
class_variables() click to toggle source

The known class variables. @see instance_variables @return [Array<FieldMirror>]

# File lib/mirrors/class_mirror.rb, line 26
def class_variables
  field_mirrors(@subject.class_variables)
end
constant(str) click to toggle source

Searches for the named constant in the mirrored namespace. May include a colon (::) separated constant path. This may trigger an autoload!

@return [ClassMirror, nil] the requested constant, or nil

# File lib/mirrors/class_mirror.rb, line 104
def constant(str)
  path = str.to_s.split("::")
  c = path[0..-2].inject(@subject) { |klass, s| klass.const_get(s) }
  field_mirror (c || @subject), path.last
rescue NameError => e
  p e
  nil
end
constants() click to toggle source

The constants defined within this class. This includes nested classes and modules, but also all other kinds of constants.

@return [Array<FieldMirror>]

# File lib/mirrors/class_mirror.rb, line 95
def constants
  field_mirrors(@subject.constants)
end
demodulized_name() click to toggle source
# File lib/mirrors/class_mirror.rb, line 185
def demodulized_name
  name.split('::').last
end
fields() click to toggle source
# File lib/mirrors/class_mirror.rb, line 19
def fields
  [constants, class_variables, class_instance_variables, instance_variables].flatten
end
intern_field_mirror(mirror) click to toggle source
# File lib/mirrors/class_mirror.rb, line 193
def intern_field_mirror(mirror)
  @field_mirrors[mirror.name] ||= mirror
end
intern_method_mirror(mirror) click to toggle source
# File lib/mirrors/class_mirror.rb, line 189
def intern_method_mirror(mirror)
  @method_mirrors[mirror.name] ||= mirror
end
is_class() click to toggle source
# File lib/mirrors/class_mirror.rb, line 11
def is_class
  @subject.is_a?(Class)
end
method(name) click to toggle source

The instance method of this class or any of its superclasses that has the specified selector

@return [MethodMirror, nil] the method or nil, if none was found

# File lib/mirrors/class_mirror.rb, line 171
def method(name)
  Mirrors.reflect @subject.instance_method(name)
end
methods() click to toggle source

The instance methods of this class. To get to the class methods, ask the singleton_class for its methods.

@return [Array<MethodMirror>]

# File lib/mirrors/class_mirror.rb, line 149
def methods
  pub_names  = @subject.public_instance_methods(false)
  prot_names = @subject.protected_instance_methods(false)
  priv_names = @subject.private_instance_methods(false)

  mirrors = []
  pub_names.sort.each do |n|
    mirrors << Mirrors.reflect(@subject.instance_method(n))
  end
  prot_names.sort.each do |n|
    mirrors << Mirrors.reflect(@subject.instance_method(n))
  end
  priv_names.sort.each do |n|
    mirrors << Mirrors.reflect(@subject.instance_method(n))
  end
  mirrors
end
mixins() click to toggle source

The mixins included in the ancestors of this class.

@return [Array<ClassMirror>]

# File lib/mirrors/class_mirror.rb, line 66
def mixins
  mirrors(@subject.ancestors.reject { |m| m.is_a?(Class) })
end
name() click to toggle source
# File lib/mirrors/class_mirror.rb, line 177
def name
  MODULE_INSPECT.bind(@subject).call
rescue
  puts @subject.inspect
  puts @subject.class
  raise
end
nested_class_count() click to toggle source
# File lib/mirrors/class_mirror.rb, line 141
def nested_class_count
  nested_classes.count
end
nested_classes() click to toggle source

The classes nested within the subject. Should not trigger autloads!

@return [Array<ClassMirror>]

# File lib/mirrors/class_mirror.rb, line 131
def nested_classes
  nc = @subject.constants.collect do |c|
    # do not trigger autoloads
    if @subject.const_defined?(c) && !@subject.autoload?(c)
      @subject.const_get(c)
    end
  end
  mirrors(nc.compact.select { |c| c.is_a?(Module) }.sort_by(&:name))
end
nesting() click to toggle source

The full nesting.

@return [Array<ClassMirror>]

# File lib/mirrors/class_mirror.rb, line 116
def nesting
  ary = []
  @subject.name.split('::').inject(Object) do |klass, str|
    ary << klass.const_get(str)
    ary.last
  end
  ary.reverse
rescue NameError
  [@subject]
end
package() click to toggle source
# File lib/mirrors/class_mirror.rb, line 15
def package
  # TODO(burke)
end
singleton_class() click to toggle source

The singleton class of this class

@return [ClassMirror]

# File lib/mirrors/class_mirror.rb, line 52
def singleton_class
  Mirrors.reflect(@subject.singleton_class)
end
singleton_class?() click to toggle source

Predicate to determine whether the subject is a singleton class

@return [true,false]

# File lib/mirrors/class_mirror.rb, line 59
def singleton_class?
  name.match(/^\#<Class:.*>$/)
end
source_files() click to toggle source

The source files this class is defined and/or extended in.

@return [Array<String,File>]

# File lib/mirrors/class_mirror.rb, line 40
def source_files
  locations = @subject.instance_methods(false).collect do |name|
    method = @subject.instance_method(name)
    sl = method.source_location
    sl.first if sl
  end
  locations.compact.uniq
end
subclasses() click to toggle source

The known subclasses

@return [Array<ClassMirror>]

# File lib/mirrors/class_mirror.rb, line 80
def subclasses
  mirrors(ObjectSpace.each_object(Class).select { |a| a.superclass == @subject })
end
superclass() click to toggle source

The direct superclass

@return [ClassMirror]

# File lib/mirrors/class_mirror.rb, line 73
def superclass
  Mirrors.reflect(@subject.superclass)
end