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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45da221cf6e20d8ac5f2066bb1ecee75ce27e322f42b26e22fcb0b1c8c1e8911
4
- data.tar.gz: a48007b7ebf85be71d45de95c656349f8bb3f95bc73639f5986bb9db582355ac
3
+ metadata.gz: c0ddc5fc81178084615b8ecefe2629365cab7cb581ef050ac510ad72f87cb48f
4
+ data.tar.gz: 6525100c126385be180955393c8eab86176606367ee49ac3d9d92220a7bd0845
5
5
  SHA512:
6
- metadata.gz: 7ccffdc70f7223a1b074d5d0c5819e72888754f25799327f69463c53d106d93225d366a0fb3be0b136793f11851213875db21b4f3a4e8b7bd6891cf7bf954898
7
- data.tar.gz: 586cb74f64befb4f126088038245a292cdec73b67149b538eaa6c6877f167b4a576ab35ef2e90a1643127d94f04f3bed9de3d60ac9ed14868a5ef9f87efea1a0
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">91%</text>
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
@@ -8,7 +8,7 @@ end
8
8
  module IS::Enum::Info
9
9
 
10
10
  NAME = 'is-enum'
11
- VERSION = '0.8.8.4'
11
+ VERSION = '0.8.10'
12
12
  SUMMARY = 'Enum types for Ruby'
13
13
  AUTHOR = 'Ivan Shikhalev'
14
14
  HOMEPAGE = 'https://github.com/inat-get/is-enum'
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] || raise ArgumentError, "Invalid value of #{ self }: #{ value.inspect }", caller_locations
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
- @values.values.sort_by { |v| v.order_no }.each { |v| yield v }
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
- result = {}
164
- result.merge! @values
165
- result.merge! @aliases
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
- protected
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 +RuntimeError+.
252
+ # After calling, {.define} will raise +FrozenError+.
248
253
  #
249
254
  # @return [void]
250
255
  def finalize!
251
- @mutex ||= Thread::Mutex::new
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: is-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.8.4
4
+ version: 0.8.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Shikhalev