is-enum 0.8.8.4 → 0.8.10
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/coverage-badge.svg +1 -1
- data/lib/is-enum/info.rb +1 -1
- data/lib/is-enum.rb +25 -12
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c0ddc5fc81178084615b8ecefe2629365cab7cb581ef050ac510ad72f87cb48f
|
|
4
|
+
data.tar.gz: 6525100c126385be180955393c8eab86176606367ee49ac3d9d92220a7bd0845
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 23782da0f945251e7e6c1dc075d525baf07950f2b94332238017d60bc51c00985e6d982bf85e232708b4bb664cc6e8772f8f6be09049ffd4a39047f1d167f46f
|
|
7
|
+
data.tar.gz: e70bdf7bebf9743200bbce139ca85d6889d23eefa9f2dbe87ba0dd1db374e409c5c42670144a15e116593cf0f61f013edaead5ad36be8a36b9b129448b2a1c78
|
data/coverage-badge.svg
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
<rect width="100" height="20" fill="#555"/>
|
|
3
3
|
<rect x="63" width="37" height="20" fill="green"/>
|
|
4
4
|
<text x="8" y="14" fill="#fff" font-family="Verdana" font-size="11">coverage</text>
|
|
5
|
-
<text x="66" y="14" fill="#fff" font-family="Verdana" font-size="11">
|
|
5
|
+
<text x="66" y="14" fill="#fff" font-family="Verdana" font-size="11">92%</text>
|
|
6
6
|
</svg>
|
data/lib/is-enum/info.rb
CHANGED
data/lib/is-enum.rb
CHANGED
|
@@ -99,7 +99,9 @@ class IS::Enum
|
|
|
99
99
|
when Enumerable
|
|
100
100
|
value.map { |v| from(v) }
|
|
101
101
|
else
|
|
102
|
-
self[value]
|
|
102
|
+
result = self[value]
|
|
103
|
+
raise ArgumentError, "Invalid value of #{ self }: #{ value.inspect }", caller_locations unless result
|
|
104
|
+
result
|
|
103
105
|
end
|
|
104
106
|
end
|
|
105
107
|
|
|
@@ -127,7 +129,7 @@ class IS::Enum
|
|
|
127
129
|
# @return [Enumerator, self]
|
|
128
130
|
def each
|
|
129
131
|
return to_enum(__method__) unless block_given?
|
|
130
|
-
|
|
132
|
+
values.each { |v| yield v }
|
|
131
133
|
self
|
|
132
134
|
end
|
|
133
135
|
|
|
@@ -160,14 +162,20 @@ class IS::Enum
|
|
|
160
162
|
# @note Both canonical names and aliases are included. To distinguish,
|
|
161
163
|
# check {.aliases} for alias keys.
|
|
162
164
|
def to_h
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
165
|
+
@values.merge(@aliases)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# @param [Range] range
|
|
169
|
+
# @return [Array<IS::Enum>]
|
|
170
|
+
def to_a range = nil
|
|
171
|
+
return values unless range
|
|
172
|
+
raise ArgumentError, "Invalid 'range' argument: #{ range.inspect }", caller_locations unless range.is_a?(Range)
|
|
173
|
+
values.select { |item| (range.begin.nil? || item >= range.begin) && (range.end.nil? || item < range.end || (!range.exclude_end? && item == range.end)) }
|
|
166
174
|
end
|
|
167
175
|
|
|
168
176
|
# @endgroup
|
|
169
177
|
|
|
170
|
-
|
|
178
|
+
private
|
|
171
179
|
|
|
172
180
|
# @group DSL
|
|
173
181
|
|
|
@@ -188,11 +196,8 @@ class IS::Enum
|
|
|
188
196
|
# define :archived, alias: :active
|
|
189
197
|
# end
|
|
190
198
|
def define name, order_no = nil, **attrs
|
|
191
|
-
@mutex ||= Thread::Mutex::new
|
|
192
199
|
@mutex.synchronize do
|
|
193
200
|
@sorted = nil
|
|
194
|
-
@values ||= {}
|
|
195
|
-
@aliases ||= {}
|
|
196
201
|
case name
|
|
197
202
|
when String
|
|
198
203
|
name = name.to_sym
|
|
@@ -244,26 +249,34 @@ class IS::Enum
|
|
|
244
249
|
end
|
|
245
250
|
|
|
246
251
|
# Freezes internal structures, preventing further modifications.
|
|
247
|
-
# After calling, {.define} will raise +
|
|
252
|
+
# After calling, {.define} will raise +FrozenError+.
|
|
248
253
|
#
|
|
249
254
|
# @return [void]
|
|
250
255
|
def finalize!
|
|
251
|
-
|
|
256
|
+
return if finalized?
|
|
252
257
|
@mutex.synchronize do
|
|
253
258
|
@values.freeze
|
|
254
259
|
@aliases.freeze
|
|
255
260
|
end
|
|
256
261
|
end
|
|
257
262
|
|
|
263
|
+
def finalized?
|
|
264
|
+
@values.frozen?
|
|
265
|
+
end
|
|
266
|
+
|
|
258
267
|
# @endgroup
|
|
259
268
|
|
|
260
269
|
# @private
|
|
261
270
|
def inherited subclass
|
|
271
|
+
super
|
|
262
272
|
@@mutex ||= Thread::Mutex::new
|
|
263
273
|
@@mutex.synchronize do
|
|
264
274
|
@@enums ||= {}
|
|
265
|
-
@@enums[subclass.name] = subclass
|
|
275
|
+
@@enums[subclass.name] = subclass if subclass.name
|
|
266
276
|
end
|
|
277
|
+
subclass.instance_variable_set(:@mutex, Thread::Mutex::new)
|
|
278
|
+
subclass.instance_variable_set(:@values, {})
|
|
279
|
+
subclass.instance_variable_set(:@aliases, {})
|
|
267
280
|
end
|
|
268
281
|
|
|
269
282
|
private :new
|