Class: Racket::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/registry.rb

Overview

Racket Registry namespace

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Constructor Details

#initializeRegistry

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.

Parameters:

  • key (String|Symbol)
  • proc (Proc|nil) (defaults to: nil)

Returns:

  • (nil)


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.

Parameters:

  • key (String|Symbol)
  • proc (Proc|nil) (defaults to: nil)

Returns:

  • (nil)


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