Module: AbstractReflection::Mirror::ClassMethods
- Included in:
- AbstractReflection::Mirror
- Defined in:
- lib/abstract_reflection/mirror.rb
Constant Summary collapse
- @@mirrors =
[]
Instance Method Summary collapse
-
#included(base) ⇒ Object
Only define this once, and always get the ClassMethods from the current module.
-
#mirror_class(obj) ⇒ Mirror, NilClass
The class to instantiate as mirror, using #new, or nil, if non is known.
-
#new(obj, reflection) ⇒ Object
The constructor, sets the @reflection instance variable before calling initialize.
-
#reflect(obj, reflection) ⇒ Object
Reflect on the passed object.
-
#reflect!(*modules) ⇒ Object
A shortcut to define reflects? behavior.
-
#reflects?(obj) ⇒ true, false
Decides whether the given class can reflect on [obj].
-
#register_mirror(klass) ⇒ Mirror
Some objects may be more useful with a specialized kind of mirror.
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.
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.
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.
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]
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.
60 61 62 63 64 |
# File 'lib/abstract_reflection/mirror.rb', line 60 def register_mirror(klass) @@mirrors.unshift klass @@mirrors.uniq! self end |