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