Module: Registry::ClassMethods
- Defined in:
- lib/registry.rb
Instance Attribute Summary collapse
-
#registry_key ⇒ Object
readonly
Returns the value of attribute registry_key.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #lookup(key) ⇒ Object
- #register(*args) ⇒ Object
- #registered?(key) ⇒ Boolean
- #registries ⇒ Object
- #registry(name = nil) ⇒ Object
- #registry_name(name = nil) ⇒ Object
- #unregister(obj = self) ⇒ Object
Instance Attribute Details
#registry_key ⇒ Object (readonly)
Returns the value of attribute registry_key.
78 79 80 |
# File 'lib/registry.rb', line 78 def registry_key @registry_key end |
Instance Method Details
#[](key) ⇒ Object
102 103 104 |
# File 'lib/registry.rb', line 102 def [](key) registry[key && key.to_sym] || fail(MSGS[:unknown] % [key, registry.keys.sort]) end |
#lookup(key) ⇒ Object
98 99 100 |
# File 'lib/registry.rb', line 98 def lookup(key) registered?(key) ? self[key] : registries[key] end |
#register(*args) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/registry.rb', line 80 def register(*args) obj = !args.last.is_a?(Symbol) ? args.pop : self args.reverse.map(&:to_sym).each do |key| obj.instance_variable_set(:@registry_key, key) registry[key] = obj end end |
#registered?(key) ⇒ Boolean
94 95 96 |
# File 'lib/registry.rb', line 94 def registered?(key) registry.key?(key && key.to_sym) end |
#registries ⇒ Object
114 115 116 |
# File 'lib/registry.rb', line 114 def registries @registries ||= superclass.registries end |
#registry(name = nil) ⇒ Object
106 107 108 |
# File 'lib/registry.rb', line 106 def registry(name = nil) @registry ||= registries.registry(registry_name(name)) end |
#registry_name(name = nil) ⇒ Object
110 111 112 |
# File 'lib/registry.rb', line 110 def registry_name(name = nil) name.nil? ? @registry_name ||= superclass.registry_name : @registry_name = name end |
#unregister(obj = self) ⇒ Object
88 89 90 91 92 |
# File 'lib/registry.rb', line 88 def unregister(obj = self) return unless obj.respond_to?(:registry_key) && obj.registry_key registry.delete(obj.registry_key) obj.remove_instance_variable(:@registry_key) end |