Module: AbstractReflection::Mirror::ClassMethods

Included in:
AbstractReflection::Mirror
Defined in:
lib/abstract_reflection/mirror.rb

Constant Summary

@@mirrors =
[]

Instance Method Summary collapse

Instance Method Details

#included(base) ⇒ Object

Only define this once, and always get the ClassMethods from the current module. When [Mirror] is included in another module, this will enable that module to also define ClassMethods to mix in when included. Additionally, if [Mirror] had registered itself for matching specific objects, this registration is forwarded to the class.



68
69
70
# File 'lib/abstract_reflection/mirror.rb', line 68

def included(base)
  base.extend(const_get("ClassMethods")) if const_defined?("ClassMethods")
end

#mirror_class(obj) ⇒ Mirror, NilClass



58
59
60
# File 'lib/abstract_reflection/mirror.rb', line 58

def mirror_class(obj)
  self if reflects?(obj)
end

#reflect(obj) ⇒ Object

Reflect on the passed object. This is the default factory for creating new mirrors, and it will try and find the appropriate mirror from the list of registered mirrors.



20
21
22
23
24
25
# File 'lib/abstract_reflection/mirror.rb', line 20

def reflect(obj)
  target_mirror = nil
  @@mirrors.detect {|klass| target_mirror = klass.mirror_class(obj) }
  raise CapabilitiesExceeded if target_mirror.nil?
  target_mirror.new(obj)
end

#reflect!(klass) ⇒ Object

A shortcut to define reflects? behavior.



36
37
38
39
# File 'lib/abstract_reflection/mirror.rb', line 36

def reflect!(klass)
  @reflected_module = klass
  register_mirror self
end

#reflects?(obj) ⇒ true, false

Decides whether the given class can reflect on [obj]



30
31
32
# File 'lib/abstract_reflection/mirror.rb', line 30

def reflects?(obj)
  @reflected_module === obj
end

#register_mirror(klass) ⇒ Mirror

Some objects may be more useful with a specialized kind of mirror. This method can be used to register new mirror classes. If used within a module, each class that includes that specific module is registered upon inclusion.



49
50
51
52
# File 'lib/abstract_reflection/mirror.rb', line 49

def register_mirror(klass)
  @@mirrors.unshift klass
  self
end