regstry 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.md +21 -0
- data/README.md +23 -0
- data/lib/registry/version.rb +3 -0
- data/lib/registry.rb +110 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 88580d89a97da7be18987bc9cfa2f58ccd732441ebc8cd8425d79c452f77cb2a
|
4
|
+
data.tar.gz: a0a62267b523ae5bce7f724ab1c7ce0e2b8523ee38572890400aa66b3762c6b1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1b63293ff56e37486909227d53a9d15004fba7db60e282e79bff32aa573f3bc19e840fc1ad743688c5ac6cf92e331290cbb103bf4512e5e8715f380617ef7ffd
|
7
|
+
data.tar.gz: 75de7e7d1fbc9be9c07242ea222c49aec09aeb742a51e954a51746a15add945b43a2f9cd3ffc778db186ba3329666d164bc169c0e62515942e489040c65a6ecb
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# MIT LICENSE
|
2
|
+
|
3
|
+
Copyright (c) 2017 Sven Fuchs <me@svenfuchs.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Registry
|
2
|
+
|
3
|
+
Ruby class registry for registering, and looking up classes using a key, rather
|
4
|
+
than the class name. Decouples looking up classes from their name and namespace.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
class Obj
|
10
|
+
include Registry
|
11
|
+
end
|
12
|
+
|
13
|
+
class One < Obj
|
14
|
+
register :one
|
15
|
+
end
|
16
|
+
|
17
|
+
class Two < Obj
|
18
|
+
register :two
|
19
|
+
end
|
20
|
+
|
21
|
+
one = Obj[:one].new
|
22
|
+
two = Obj[:two].new
|
23
|
+
```
|
data/lib/registry.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module Registry
|
5
|
+
MSGS = {
|
6
|
+
unknown: 'can not use unregistered object %p. known objects are: %p'
|
7
|
+
}
|
8
|
+
|
9
|
+
class UnknownKey < StandardError
|
10
|
+
end
|
11
|
+
|
12
|
+
class Registries
|
13
|
+
extend Forwardable
|
14
|
+
include Enumerable
|
15
|
+
|
16
|
+
def_delegators :registries, :each, :keys, :values
|
17
|
+
|
18
|
+
def [](key)
|
19
|
+
registry = values.detect { |registry| registry.key?(key) }
|
20
|
+
obj = registry[key] if registry
|
21
|
+
obj || raise(UnknownKey, MSGS[:unknown] % [key, all_keys])
|
22
|
+
end
|
23
|
+
|
24
|
+
def registry(key)
|
25
|
+
registries[key]
|
26
|
+
end
|
27
|
+
|
28
|
+
def registries
|
29
|
+
@registries ||= Hash.new { |registries, key| registries[key] = Registry.new }
|
30
|
+
end
|
31
|
+
|
32
|
+
def all_keys
|
33
|
+
values.map(&:keys).flatten.sort
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Registry
|
38
|
+
extend Forwardable
|
39
|
+
include Enumerable
|
40
|
+
|
41
|
+
def_delegators :objects, :each, :key?, :keys, :values
|
42
|
+
|
43
|
+
def []=(key, object)
|
44
|
+
objects[key.to_sym] = object
|
45
|
+
end
|
46
|
+
|
47
|
+
def [](key)
|
48
|
+
key && objects[key.to_sym] || raise(UnknownKey, MSGS[:unknown] % [key, objects.keys.sort])
|
49
|
+
end
|
50
|
+
|
51
|
+
def objects
|
52
|
+
@objects ||= {}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class << self
|
57
|
+
def included(base)
|
58
|
+
base.send(:extend, ClassMethods)
|
59
|
+
base.send(:include, InstanceMethods)
|
60
|
+
|
61
|
+
name = base.name.to_s.split('::').last&.downcase&.to_sym || :default
|
62
|
+
base.instance_variable_set(:@registry_name, name)
|
63
|
+
|
64
|
+
base.instance_variable_set(:@registries, Registries.new)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
module ClassMethods
|
69
|
+
attr_reader :registry_key
|
70
|
+
|
71
|
+
def register(key, obj = self)
|
72
|
+
obj.instance_variable_set(:@registry_key, key)
|
73
|
+
registry[key] = obj
|
74
|
+
end
|
75
|
+
|
76
|
+
def registered?(key)
|
77
|
+
registry.key?(key)
|
78
|
+
end
|
79
|
+
|
80
|
+
def lookup(key)
|
81
|
+
registries[key]
|
82
|
+
end
|
83
|
+
|
84
|
+
def [](key)
|
85
|
+
registry[key&.to_sym] || fail(MSGS[:unknown] % [key, registry.keys.sort])
|
86
|
+
end
|
87
|
+
|
88
|
+
def registry
|
89
|
+
@registry ||= registries.registry(registry_name)
|
90
|
+
end
|
91
|
+
|
92
|
+
def registry_name(name = nil)
|
93
|
+
name.nil? ? @registry_name ||= superclass.registry_name : @registry_name = name
|
94
|
+
end
|
95
|
+
|
96
|
+
def registries
|
97
|
+
@registries ||= superclass.registries
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
module InstanceMethods
|
102
|
+
def registry_key
|
103
|
+
self.class.registry_key
|
104
|
+
end
|
105
|
+
|
106
|
+
def registry_name
|
107
|
+
self.class.registry_name
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: regstry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sven Fuchs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Allows registering Ruby classes for lookup using a key.
|
14
|
+
email:
|
15
|
+
- me@svenfuchs.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- Gemfile
|
21
|
+
- LICENSE.md
|
22
|
+
- README.md
|
23
|
+
- lib/registry.rb
|
24
|
+
- lib/registry/version.rb
|
25
|
+
homepage: https://github.com/svenfuchs/registry
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.0.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Ruby class registry
|
48
|
+
test_files: []
|