racket-registry 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/racket/registry.rb +86 -105
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efdb66903525e8c25d4c9254fabeec1ba8171650
4
- data.tar.gz: 30839afe56b0fd154c0cb1b99b4baa1e026055be
3
+ metadata.gz: 31b9bb4acd5196231c25d80c686699265a067de5
4
+ data.tar.gz: 4c98a4ed25abb3e19b04ce7216f6d652342787d7
5
5
  SHA512:
6
- metadata.gz: 6c1f7bf54264adcbb648b9c3c322a7cce347caea3d9c84d14b6a880c6636d16b6e11c398cb5eb3cc9ca980fede07c638e2c8518ddaaf66ec70975e97b4c94de6
7
- data.tar.gz: 76634886929c092c1f44c412a2443c94f7fafe63e925ef20501e6bb501ec79cff0573fba5445faa2a348a5ecc05edfe6ecdc5cb3963ae2cd429d76f650c8beb3
6
+ metadata.gz: a0f7c55a4f64240e15a333bfe964e51df7a240d5a96372a00badb56fe8d264f0ea5e3dc95bad780e70272af002f8553af9ee9a41417c8723e75a411e7446333e
7
+ data.tar.gz: 80ae5cdebbacb32a0a15503597cea2364aa1dea17672ff5dba504f3566ba3dfd81937cb112bc633bd209bd2412d6c6fae343a41b2885f8275a44d518350ea3d3
@@ -25,12 +25,12 @@ module Racket
25
25
  # @param [String|Symbol] key
26
26
  # @return [nil]
27
27
  def forget(key)
28
- self.class.forget(obj: self, key: key)
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
- self.class.forget_all(obj: self)
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
- self.class.register(
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
- self.class.register_singleton(
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
- # Removes a callback from a registry.
69
- #
70
- # @param [Hash] options
71
- # @return [nil]
72
- def self.forget(options)
73
- obj, methods, key = validate_existing(options)
74
- raise KeyNotRegisteredError,
75
- "Key #{key} is not registered" unless methods.include?(key)
76
- obj.singleton_class.instance_eval do
77
- @resolved.delete(key) if defined?(@resolved)
78
- remove_method(key)
79
- end && nil
80
- end
81
-
82
- # Removes all callbacks from a registry.
83
- #
84
- # @param [Hash] options
85
- # @return [nil]
86
- def self.forget_all(options)
87
- obj, methods, = validate_existing(options)
88
- obj.singleton_class.instance_eval do
89
- @resolved.clear if defined?(@resolved)
90
- methods.each { |meth| remove_method(meth) }
91
- end && nil
92
- end
93
-
94
- # Registers a new callback to a registry. This will add a new method
95
- # matching +key+ to the registry that can be used both outside the
96
- # registry and when registering other callbacks dependant of the
97
- # current entry. Results from the callback will not be cached, meaning
98
- # that the callback may return a different object every time.
99
- #
100
- # @param [Hash] options
101
- # @return [nil]
102
- def self.register(options)
103
- key, proc, proc_args, obj = validate_usable(options)
104
- obj.define_singleton_method(key) do
105
- proc.call(*proc_args)
106
- end && nil
107
- end
108
-
109
- # Registers a new callback to a registry. This will add a new method
110
- # matching +key+ to the registry that can be used both outside the
111
- # registry and when registering other callbacks dependant of the
112
- # current entry. Results from the callback will be cached, meaning
113
- # that the callback will return the same object every time.
114
- #
115
- # @param [Hash] options
116
- # @return [nil]
117
- def self.register_singleton(options)
118
- key, proc, proc_args, obj = validate_usable(options)
119
- resolved = resolved(options[:obj])
120
- obj.define_singleton_method(key) do
121
- return resolved[key] if resolved.key?(key)
122
- resolved[key] = proc.call(*proc_args)
123
- end && nil
124
- end
125
-
126
- def self.resolved(obj)
127
- obj.singleton_class.instance_eval { @resolved ||= {} }
128
- end
129
-
130
- def self.validate_existing(options)
131
- result = [options[:obj]]
132
- result << result.first.singleton_methods
133
- key = options.fetch(:key, nil)
134
- result << key.to_sym if key
135
- result
136
- end
137
-
138
- def self.validate_key(key, obj)
139
- sym = key.to_sym
140
- insp = key.inspect
141
- raise KeyAlreadyRegisteredError, "Key #{insp} already registered" if
142
- obj.singleton_methods.include?(sym)
143
- raise InvalidKeyError, "Invalid key #{insp}" if
144
- obj.public_methods.include?(sym) ||
145
- /^[a-z\_]{1}[\d\w\_]*$/ !~ sym
146
- sym
147
- end
148
-
149
- def self.validate_callback(proc, block)
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.3.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-06-26 00:00:00.000000000 Z
11
+ date: 2016-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bacon