Module: AbstractReflection::Mirror::ClassMethods

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

Constant Summary collapse

@@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.



80
81
82
83
# File 'lib/abstract_reflection/mirror.rb', line 80

def included(base)
  base.extend(const_get("ClassMethods")) if const_defined?("ClassMethods")
  base.const_set(:CapabilitiesExceeded, CapabilitiesExceeded)
end

#mirror_class(obj) ⇒ Mirror, NilClass

Returns the class to instantiate as mirror, using #new, or nil, if non is known.

Parameters:

  • the (Object)

    object to reflect upon

Returns:

  • (Mirror, NilClass)

    the class to instantiate as mirror, using #new, or nil, if non is known



70
71
72
# File 'lib/abstract_reflection/mirror.rb', line 70

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

#new(obj, reflection) ⇒ Object

The constructor, sets the @reflection instance variable before calling initialize.



31
32
33
34
35
36
# File 'lib/abstract_reflection/mirror.rb', line 31

def new(obj, reflection)
  basic_new_object = allocate
  basic_new_object.reflection = reflection
  basic_new_object.send(:initialize, obj)
  basic_new_object
end

#reflect(obj, reflection) ⇒ 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.

Parameters:

  • the (Object)

    object to reflect upon. This need not be the actual object represented - it can itself be just a representation. It is really up to the mirror to decide what to do with it

  • the (Reflection)

    instance of a Reflection this mirror was spawned in.

Raises:



22
23
24
25
26
27
# File 'lib/abstract_reflection/mirror.rb', line 22

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

#reflect!(*modules) ⇒ Object

A shortcut to define reflects? behavior.

Parameters:

  • the (Module)

    module whose instances this mirror reflects



47
48
49
50
# File 'lib/abstract_reflection/mirror.rb', line 47

def reflect!(*modules)
  @reflected_modules = modules
  register_mirror self
end

#reflects?(obj) ⇒ true, false

Decides whether the given class can reflect on [obj]

Parameters:

  • the (Object)

    object to reflect upon

Returns:

  • (true, false)


41
42
43
# File 'lib/abstract_reflection/mirror.rb', line 41

def reflects?(obj)
  @reflected_modules.any? { |mod| mod === 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.

Parameters:

  • The (Module)

    class or module to register

Returns:



60
61
62
63
64
# File 'lib/abstract_reflection/mirror.rb', line 60

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