racket-registry 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/racket/registry.rb +86 -105
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31b9bb4acd5196231c25d80c686699265a067de5
|
4
|
+
data.tar.gz: 4c98a4ed25abb3e19b04ce7216f6d652342787d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0f7c55a4f64240e15a333bfe964e51df7a240d5a96372a00badb56fe8d264f0ea5e3dc95bad780e70272af002f8553af9ee9a41417c8723e75a411e7446333e
|
7
|
+
data.tar.gz: 80ae5cdebbacb32a0a15503597cea2364aa1dea17672ff5dba504f3566ba3dfd81937cb112bc633bd209bd2412d6c6fae343a41b2885f8275a44d518350ea3d3
|
data/lib/racket/registry.rb
CHANGED
@@ -25,12 +25,12 @@ module Racket
|
|
25
25
|
# @param [String|Symbol] key
|
26
26
|
# @return [nil]
|
27
27
|
def forget(key)
|
28
|
-
|
28
|
+
Helper.forget(obj: self, key: key)
|
29
29
|
end
|
30
30
|
|
31
31
|
# Removes all callbacks from the registry.
|
32
32
|
def forget_all
|
33
|
-
|
33
|
+
Helper.forget_all(obj: self)
|
34
34
|
end
|
35
35
|
|
36
36
|
# Registers a new callback in the registry. This will add a new method
|
@@ -43,9 +43,7 @@ module Racket
|
|
43
43
|
# @param [Proc|nil] proc
|
44
44
|
# @return [nil]
|
45
45
|
def register(key, proc = nil, &block)
|
46
|
-
|
47
|
-
obj: self, key: key, proc: proc, block: block
|
48
|
-
)
|
46
|
+
Helper.register(obj: self, key: key, proc: proc, block: block)
|
49
47
|
end
|
50
48
|
|
51
49
|
# Registers a new callback in the registry. This will add a new method
|
@@ -58,110 +56,93 @@ module Racket
|
|
58
56
|
# @param [Proc|nil] proc
|
59
57
|
# @return [nil]
|
60
58
|
def register_singleton(key, proc = nil, &block)
|
61
|
-
|
62
|
-
obj: self, key: key, proc: proc, block: block
|
63
|
-
)
|
59
|
+
Helper.register_singleton(obj: self, key: key, proc: proc, block: block)
|
64
60
|
end
|
65
61
|
|
66
62
|
alias singleton register_singleton
|
67
63
|
|
68
|
-
#
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
end
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
end
|
148
|
-
|
149
|
-
|
150
|
-
return proc if proc.respond_to?(:call)
|
151
|
-
return block if block.respond_to?(:call)
|
152
|
-
raise InvalidCallbackError, 'Invalid callback'
|
153
|
-
end
|
154
|
-
|
155
|
-
def self.validate_usable(options)
|
156
|
-
obj = options[:obj]
|
157
|
-
key = validate_key(options[:key], obj)
|
158
|
-
proc = validate_callback(options[:proc], options[:block])
|
159
|
-
[key, proc, proc.arity.zero? ? [] : [obj], obj]
|
160
|
-
end
|
161
|
-
|
162
|
-
private_class_method :resolved, :validate_callback,
|
163
|
-
:validate_existing, :validate_key,
|
164
|
-
:validate_usable
|
64
|
+
# Private helper module for Racket Registry
|
65
|
+
module Helper
|
66
|
+
# Private method - do not call
|
67
|
+
def self.forget(options)
|
68
|
+
obj, methods, key = validate_existing(options)
|
69
|
+
raise KeyNotRegisteredError,
|
70
|
+
"Key #{key} is not registered" unless methods.include?(key)
|
71
|
+
obj.singleton_class.instance_eval do
|
72
|
+
@resolved.delete(key) if defined?(@resolved)
|
73
|
+
remove_method(key)
|
74
|
+
end && nil
|
75
|
+
end
|
76
|
+
|
77
|
+
# Private method - do not call
|
78
|
+
def self.forget_all(options)
|
79
|
+
obj, methods, = validate_existing(options)
|
80
|
+
obj.singleton_class.instance_eval do
|
81
|
+
@resolved.clear if defined?(@resolved)
|
82
|
+
methods.each { |meth| remove_method(meth) }
|
83
|
+
end && nil
|
84
|
+
end
|
85
|
+
|
86
|
+
# Private method - do not call
|
87
|
+
def self.register(options)
|
88
|
+
key, proc, proc_args, obj = validate_usable(options)
|
89
|
+
obj.define_singleton_method(key) do
|
90
|
+
proc.call(*proc_args)
|
91
|
+
end && nil
|
92
|
+
end
|
93
|
+
|
94
|
+
# Private method - do not call
|
95
|
+
def self.register_singleton(options)
|
96
|
+
key, proc, proc_args, obj = validate_usable(options)
|
97
|
+
resolved = resolved(options[:obj])
|
98
|
+
obj.define_singleton_method(key) do
|
99
|
+
return resolved[key] if resolved.key?(key)
|
100
|
+
resolved[key] = proc.call(*proc_args)
|
101
|
+
end && nil
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.resolved(obj)
|
105
|
+
obj.singleton_class.instance_eval { @resolved ||= {} }
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.validate_existing(options)
|
109
|
+
result = [options[:obj]]
|
110
|
+
result << result.first.singleton_methods
|
111
|
+
key = options.fetch(:key, nil)
|
112
|
+
result << key.to_sym if key
|
113
|
+
result
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.validate_key(key, obj)
|
117
|
+
sym = key.to_sym
|
118
|
+
insp = key.inspect
|
119
|
+
raise KeyAlreadyRegisteredError, "Key #{insp} already registered" if
|
120
|
+
obj.singleton_methods.include?(sym)
|
121
|
+
raise InvalidKeyError, "Invalid key #{insp}" if
|
122
|
+
obj.public_methods.include?(sym) ||
|
123
|
+
/^[a-z\_]{1}[\d\w\_]*$/ !~ sym
|
124
|
+
sym
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.validate_callback(proc, block)
|
128
|
+
return proc if proc.respond_to?(:call)
|
129
|
+
return block if block.respond_to?(:call)
|
130
|
+
raise InvalidCallbackError, 'Invalid callback'
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.validate_usable(options)
|
134
|
+
obj = options[:obj]
|
135
|
+
key = validate_key(options[:key], obj)
|
136
|
+
proc = validate_callback(options[:proc], options[:block])
|
137
|
+
[key, proc, proc.arity.zero? ? [] : [obj], obj]
|
138
|
+
end
|
139
|
+
|
140
|
+
private_class_method :resolved, :validate_callback,
|
141
|
+
:validate_existing, :validate_key,
|
142
|
+
:validate_usable
|
143
|
+
end
|
144
|
+
|
145
|
+
private_constant :Helper
|
165
146
|
|
166
147
|
# Exception class used when a user tries to register an
|
167
148
|
# invalid callback.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: racket-registry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lars Olsson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bacon
|