inum 5.0.0 → 5.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 493bb30797a42fd6c51d030772bf00e079aee367
4
- data.tar.gz: 699880c62844bb20bd5ef3c470eea38cbd2fb831
3
+ metadata.gz: e23a5e942d7583617ddbcd45931e9d4850394efe
4
+ data.tar.gz: 5b2ddbd32432205959846d2199390b331ff8a123
5
5
  SHA512:
6
- metadata.gz: 6267ef53da51c33f37997c01ac10872a0c5f6d94b14445ccde25cce1fdbe7ceccac38af70520af430fc81bc0593da4cb989f8173087aaf554401026d3b652ee2
7
- data.tar.gz: 45404163209bee403e9424a4f820e0029380792a117b8f3519644b12f5ec92a26af1063a0e6ab2d5d9bc7599efae61bd007885b189cbebebfbfe12e9846b1db2
6
+ metadata.gz: 5a4753423085581b73eba9cdea2eb01310abc255f72f2385cf09b35451eb08f7e4d6503576f9e4612647e6b215975124a77b88a0feb8ee154dad466a8c71957a
7
+ data.tar.gz: 8338f1d3b44ca69bfd43e34d33828b95968d7a66decdb777f8aefba3824ea91b081bad13718f054b363e150a2fb0877aada60f0a5309545093f4ae2cf107e654
@@ -21,14 +21,13 @@ module Inum
21
21
  # @param label [Symbol] label of Enum.
22
22
  # @param value [Integer] value of Enum.
23
23
  def initialize(label, value)
24
- @label = label.freeze
25
- @label_string = label.to_s.freeze
26
- @label_downcase = @label_string.downcase.freeze
27
- @label_upcase = @label_string.upcase.freeze
28
- @value = value.freeze
29
-
30
- @underscore_class_path = self.class.name ? ActiveSupport::Inflector.underscore(self.class.name).gsub('/', '.') : ''
31
- @underscore_label = ActiveSupport::Inflector.underscore(label).gsub('/', '.')
24
+ @label = label.freeze
25
+ @label_string = label.to_s.freeze
26
+ @label_downcase = @label_string.downcase.freeze
27
+ @label_upcase = @label_string.upcase.freeze
28
+ @label_underscore = ActiveSupport::Inflector.underscore(label).gsub('/', '.').freeze
29
+ @value = value.freeze
30
+ @i18n_namespace = (self.class.name ? ActiveSupport::Inflector.underscore(self.class.name).gsub('/', '.') : '').freeze
32
31
  end
33
32
 
34
33
  # Compare object.
@@ -95,12 +94,19 @@ module Inum
95
94
  @label_upcase
96
95
  end
97
96
 
97
+ # Underscore label.
98
+ #
99
+ # @return [String] underscore label.
100
+ def underscore
101
+ @label_underscore
102
+ end
103
+
98
104
  # Translate Enum to localized string.(use i18n)
99
105
  # @note find default `Namespace.Classname.EnumMember`
100
106
  #
101
107
  # @return [String] localized string of Enum.
102
108
  def translate
103
- I18n.t(self.class.i18n_key(@underscore_class_path, @underscore_label))
109
+ I18n.t(self.class.i18n_key(@i18n_namespace, @label_underscore))
104
110
  end
105
111
  alias_method :t, :translate
106
112
 
@@ -220,12 +226,17 @@ module Inum
220
226
  new(label, value).tap do |enum|
221
227
  const_set(label, enum)
222
228
  @enums.push(enum)
229
+ @enums.sort! {|a, b| a.to_i <=> b.to_i }
223
230
  end
224
231
  end
225
232
 
226
233
  # Initialize inherited class.
227
234
  def self.inherited(child)
228
- child.instance_variable_set(:@enums, Array.new)
235
+ if self == Inum::Base
236
+ child.instance_variable_set(:@enums, Array.new)
237
+ else
238
+ child.instance_variable_set(:@enums, self.to_a)
239
+ end
229
240
  end
230
241
 
231
242
  # Validate enum args.
@@ -238,6 +249,10 @@ module Inum
238
249
  raise ArgumentError, "#{label} isn't instance of Symbol."
239
250
  end
240
251
 
252
+ unless value.kind_of?(Numeric)
253
+ raise ArgumentError, "#{value} isn't instance of Integer."
254
+ end
255
+
241
256
  if labels.include?(label)
242
257
  raise ArgumentError, "#{label} already exists label."
243
258
  end
@@ -1,3 +1,3 @@
1
1
  module Inum
2
- VERSION = '5.0.0'
2
+ VERSION = '5.1.0'
3
3
  end
@@ -37,7 +37,7 @@ describe Inum::Base do
37
37
  end
38
38
  end
39
39
 
40
- context 'When define a enum having wrong label' do
40
+ context 'When define a enum with wrong label' do
41
41
  subject do
42
42
  Class.new(Inum::Base) {
43
43
  define 'red_bull', 0
@@ -49,7 +49,7 @@ describe Inum::Base do
49
49
  end
50
50
  end
51
51
 
52
- context 'When define a enum having duplicate label' do
52
+ context 'When define a enum with duplicated label' do
53
53
  subject do
54
54
  Class.new(Inum::Base) {
55
55
  define :RED_BULL, 0
@@ -62,7 +62,19 @@ describe Inum::Base do
62
62
  end
63
63
  end
64
64
 
65
- context 'When define a enum having duplicate value' do
65
+ context 'When define a enum with nan value' do
66
+ subject do
67
+ Class.new(Inum::Base) {
68
+ define :RED_BULL, 'hahaha'
69
+ }
70
+ end
71
+
72
+ it 'Raise ArgumentError.' do
73
+ expect { subject }.to raise_error(ArgumentError)
74
+ end
75
+ end
76
+
77
+ context 'When define a enum with duplicate value' do
66
78
  subject do
67
79
  Class.new(Inum::Base) {
68
80
  define :RED_BULL, 0
@@ -83,7 +95,7 @@ describe Inum::Base do
83
95
  define :Muromisan, 1
84
96
  define :Nourin, 2
85
97
  define :KMB, 3
86
- define :Bakuon, 4
98
+ define :BakuOn, 5
87
99
  end
88
100
  end
89
101
 
@@ -176,13 +188,19 @@ describe Inum::Base do
176
188
 
177
189
  describe '#dowcase ' do
178
190
  it 'Return lowercase string.' do
179
- expect(Anime::Bakuon.downcase).to eq('bakuon')
191
+ expect(Anime::BakuOn.downcase).to eq('bakuon')
180
192
  end
181
193
  end
182
194
 
183
195
  describe '#upcase ' do
184
196
  it 'Return uppercase string.' do
185
- expect(Anime::Bakuon.upcase).to eq('BAKUON')
197
+ expect(Anime::BakuOn.upcase).to eq('BAKUON')
198
+ end
199
+ end
200
+
201
+ describe '#underscore ' do
202
+ it 'Return underscore string.' do
203
+ expect(Anime::BakuOn.underscore).to eq('baku_on')
186
204
  end
187
205
  end
188
206
 
@@ -217,7 +235,7 @@ describe Inum::Base do
217
235
  ['t', 1],
218
236
  ['t', 2],
219
237
  ['t', 3],
220
- ['t', 4]
238
+ ['t', 5]
221
239
  ])
222
240
  end
223
241
 
@@ -237,7 +255,7 @@ describe Inum::Base do
237
255
  ['t', 0],
238
256
  ['t', 1],
239
257
  ['t', 2],
240
- ['t', 4]
258
+ ['t', 5]
241
259
  ])
242
260
  end
243
261
  end
@@ -252,7 +270,7 @@ describe Inum::Base do
252
270
  describe '.each' do
253
271
  it 'Execute block with a right order.' do
254
272
  count = 0
255
- orders = [Anime::Nyaruko, Anime::Muromisan, Anime::Nourin, Anime::KMB, Anime::Bakuon]
273
+ orders = [Anime::Nyaruko, Anime::Muromisan, Anime::Nourin, Anime::KMB, Anime::BakuOn]
256
274
 
257
275
  Anime.each do |enum|
258
276
  expect(enum).to eq(orders[count])
@@ -263,7 +281,7 @@ describe Inum::Base do
263
281
 
264
282
  describe '.labels' do
265
283
  it 'Return array of label.' do
266
- expect(Anime.labels).to match_array([:Nyaruko, :Muromisan, :Nourin, :KMB, :Bakuon])
284
+ expect(Anime.labels).to match_array([:Nyaruko, :Muromisan, :Nourin, :KMB, :BakuOn])
267
285
  end
268
286
  end
269
287
 
@@ -278,7 +296,7 @@ describe Inum::Base do
278
296
  expect(Anime.parse(source)).to eq(destination)
279
297
  end
280
298
 
281
- let(:destination) { Anime::Bakuon }
299
+ let(:destination) { Anime::BakuOn }
282
300
 
283
301
  context 'When source is string' do
284
302
  let(:source) { 'Bakuon' }
@@ -295,21 +313,21 @@ describe Inum::Base do
295
313
  end
296
314
 
297
315
  context 'When source is integer' do
298
- let(:source) { 4 }
316
+ let(:source) { 5 }
299
317
  it 'success.' do
300
318
  subject
301
319
  end
302
320
  end
303
321
 
304
322
  context 'When source is integer of string' do
305
- let(:source) { '4' }
323
+ let(:source) { '5' }
306
324
  it 'Return inum.' do
307
325
  subject
308
326
  end
309
327
  end
310
328
 
311
329
  context 'When source is enum' do
312
- let(:source) { Anime::Bakuon }
330
+ let(:source) { Anime::BakuOn }
313
331
  it 'Return inum.' do
314
332
  subject
315
333
  end
@@ -349,13 +367,43 @@ describe Inum::Base do
349
367
 
350
368
  describe '.to_a' do
351
369
  it 'Return array of enum.' do
352
- expect(Anime.to_a).to match_array([Anime::Nyaruko, Anime::Muromisan, Anime::Nourin, Anime::KMB, Anime::Bakuon])
370
+ expect(Anime.to_a).to match_array([Anime::Nyaruko, Anime::Muromisan, Anime::Nourin, Anime::KMB, Anime::BakuOn])
353
371
  end
354
372
  end
355
373
 
356
374
  describe '.values' do
357
375
  it 'Return array of value.' do
358
- expect(Anime.values).to match_array([0, 1, 2, 3, 4])
376
+ expect(Anime.values).to match_array([0, 1, 2, 3, 5])
377
+ end
378
+ end
379
+
380
+ context 'After extend Anime' do
381
+ describe 'Defined enum class' do
382
+ before :each do
383
+ class Anime2016 < Anime
384
+ define :GanbaruZoi, 4
385
+ end
386
+ end
387
+
388
+ after :each do
389
+ Object.class_eval{ remove_const :Anime2016 }
390
+ end
391
+
392
+ describe '.values' do
393
+ it 'Return array of value.' do
394
+ expect(Anime2016.values).to match_array([0, 1, 2, 3, 4, 5])
395
+ end
396
+
397
+ it 'Order is valid.' do
398
+ expect(Anime2016.values[4]).to eq(4)
399
+ end
400
+ end
401
+
402
+ describe 'extend enum' do
403
+ it 'is no impact.' do
404
+ expect(Anime.length).to eq(5)
405
+ end
406
+ end
359
407
  end
360
408
  end
361
409
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inum
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alfa-jpn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-03 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: bundler