Class: Racket::Registry
- Inherits:
-
Object
- Object
- Racket::Registry
- Defined in:
- lib/racket/registry.rb
Overview
Racket Registry namespace
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#register(key, proc = nil, &block) ⇒ nil
Registers a new proc in the registry.
-
#register_singleton(key, proc = nil, &block) ⇒ nil
(also: #singleton)
Registers a new proc in the registry.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
23 24 25 |
# File 'lib/racket/registry.rb', line 23 def initialize @resolved = {} end |
Instance Method Details
#register(key, proc = nil, &block) ⇒ nil
Registers a new proc in the registry. This will add a new method matching key
to the registry that can be used both outside the registry and when registering other procs dependant of the current entry. Results from the proc will not be cached, meaning that the proc may return a different object every time.
36 37 38 39 40 41 42 |
# File 'lib/racket/registry.rb', line 36 def register(key, proc = nil, &block) key, proc, proc_args, = ClassMethods.validate_usable([key, self, proc, block, @resolved]) singleton_class.instance_eval do define_method(key) { proc.call(*proc_args) } end && nil end |
#register_singleton(key, proc = nil, &block) ⇒ nil Also known as: singleton
Registers a new proc in the registry. This will add a new method matching key
to the registry that can be used both outside the registry and when registering other procs dependant of the current entry. Results from the proc will be cached, meaning that the proc will return the same object every time.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/racket/registry.rb', line 53 def register_singleton(key, proc = nil, &block) key, proc, proc_args, resolved = ClassMethods.validate_usable([key, self, proc, block, @resolved]) singleton_class.instance_eval do define_method(key) do return resolved[key] if resolved.key?(key) resolved[key] = proc.call(*proc_args) end end && nil end |