enumerate_it 4.0.0 → 4.2.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.
@@ -2,10 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe EnumerateIt::Base do
4
4
  it 'creates constants for each enumeration value' do
5
- constants = [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3]
5
+ constants = [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3,
6
+ TestEnumeration::VALUE_4]
6
7
 
7
- constants.each_with_index do |constant, idx|
8
- expect(constant).to eq((idx + 1).to_s)
8
+ constants.each.with_index(1) do |constant, index|
9
+ expect(constant).to eq(index.to_s)
9
10
  end
10
11
  end
11
12
 
@@ -23,7 +24,7 @@ describe EnumerateIt::Base do
23
24
 
24
25
  describe '.list' do
25
26
  it "creates a method that returns the allowed values in the enumeration's class" do
26
- expect(TestEnumeration.list).to eq(%w[1 2 3])
27
+ expect(TestEnumeration.list).to eq(%w[1 2 3 4])
27
28
  end
28
29
 
29
30
  context 'specifying a default sort mode' do
@@ -32,40 +33,45 @@ describe EnumerateIt::Base do
32
33
  context 'by value' do
33
34
  let(:sort_mode) { :value }
34
35
 
35
- it { is_expected.to eq(%w[0 1 2 3]) }
36
+ it { is_expected.to eq(%w[0 1 2 3 4]) }
36
37
  end
37
38
 
38
39
  context 'by name' do
39
40
  let(:sort_mode) { :name }
40
41
 
41
- it { is_expected.to eq(%w[2 1 3 0]) }
42
+ it { is_expected.to eq(%w[2 4 1 3 0]) }
42
43
  end
43
44
 
44
45
  context 'by translation' do
45
46
  let(:sort_mode) { :translation }
46
47
 
47
- it { is_expected.to eq(%w[3 2 0 1]) }
48
+ it { is_expected.to eq(%w[3 2 0 1 4]) }
49
+ end
50
+
51
+ context 'by normalize translation' do
52
+ let(:sort_mode) { :normalize_translation }
53
+
54
+ it { is_expected.to eq(%w[3 4 2 0 1]) }
48
55
  end
49
56
 
50
57
  context 'by nothing' do
51
58
  let(:sort_mode) { :none }
52
59
 
53
- it { is_expected.to eq(%w[1 2 3 0]) }
60
+ it { is_expected.to eq(%w[1 2 3 4 0]) }
54
61
  end
55
62
  end
56
63
  end
57
64
 
58
65
  it 'creates a method that returns the enumeration specification' do
59
66
  expect(TestEnumeration.enumeration).to eq(
60
- value_1: ['1', 'Hey, I am 1!'],
61
- value_2: ['2', 'Hey, I am 2!'],
62
- value_3: ['3', 'Hey, I am 3!']
67
+ value_1: ['1', 'Hey, I am 1!'], value_2: ['2', 'Hey, I am 2!'],
68
+ value_3: ['3', 'Hey, I am 3!'], value_4: ['4', 'Héy, I ãm 2!']
63
69
  )
64
70
  end
65
71
 
66
72
  describe '.length' do
67
73
  it 'returns the length of the enumeration' do
68
- expect(TestEnumeration.length).to eq(3)
74
+ expect(TestEnumeration.length).to eq(4)
69
75
  end
70
76
  end
71
77
 
@@ -75,13 +81,14 @@ describe EnumerateIt::Base do
75
81
  TestEnumeration.each_translation do |translation|
76
82
  translations << translation
77
83
  end
78
- expect(translations).to eq(['Hey, I am 1!', 'Hey, I am 2!', 'Hey, I am 3!'])
84
+ expect(translations).to eq(['Hey, I am 1!', 'Hey, I am 2!', 'Hey, I am 3!', 'Héy, I ãm 2!'])
79
85
  end
80
86
  end
81
87
 
82
88
  describe '.translations' do
83
89
  it 'returns all translations' do
84
- expect(TestEnumeration.translations).to eq(['Hey, I am 1!', 'Hey, I am 2!', 'Hey, I am 3!'])
90
+ expect(TestEnumeration.translations).to eq(['Hey, I am 1!', 'Hey, I am 2!', 'Hey, I am 3!',
91
+ 'Héy, I ãm 2!'])
85
92
  end
86
93
  end
87
94
 
@@ -91,7 +98,7 @@ describe EnumerateIt::Base do
91
98
  TestEnumeration.each_key do |key|
92
99
  keys << key
93
100
  end
94
- expect(keys).to eq(%i[value_1 value_2 value_3])
101
+ expect(keys).to eq(%i[value_1 value_2 value_3 value_4])
95
102
  end
96
103
  end
97
104
 
@@ -108,7 +115,8 @@ describe EnumerateIt::Base do
108
115
  describe '.to_a' do
109
116
  it 'returns an array with the values and human representations' do
110
117
  expect(TestEnumeration.to_a)
111
- .to eq([['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']])
118
+ .to eq([['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3'],
119
+ ['Héy, I ãm 2!', '4']])
112
120
  end
113
121
 
114
122
  it 'translates the available values' do
@@ -152,7 +160,7 @@ describe EnumerateIt::Base do
152
160
 
153
161
  describe '.to_range' do
154
162
  it "returns a Range object containing the enumeration's value interval" do
155
- expect(TestEnumeration.to_range).to eq('1'..'3')
163
+ expect(TestEnumeration.to_range).to eq('1'..'4')
156
164
  end
157
165
  end
158
166
 
@@ -206,7 +214,7 @@ describe EnumerateIt::Base do
206
214
 
207
215
  describe '.keys' do
208
216
  it 'returns a list with the keys used to define the enumeration' do
209
- expect(TestEnumeration.keys).to eq(%i[value_1 value_2 value_3])
217
+ expect(TestEnumeration.keys).to eq(%i[value_1 value_2 value_3 value_4])
210
218
  end
211
219
  end
212
220
 
@@ -220,6 +228,55 @@ describe EnumerateIt::Base do
220
228
  end
221
229
  end
222
230
 
231
+ describe '.custom_helpers' do
232
+ context 'when methods are defined inside the block' do
233
+ it 'become class methods on the enum' do
234
+ expect(TestEnumerationWithCustomHelpers.lookup('1')).to eq(:one)
235
+ expect(TestEnumerationWithCustomHelpers.lookup('2')).to eq(:two)
236
+ expect(TestEnumerationWithCustomHelpers.boolean?('1')).to be(true)
237
+ expect(TestEnumerationWithCustomHelpers.boolean?('2')).to be(false)
238
+ end
239
+ end
240
+
241
+ context 'when a custom helper name collides with a built-in method' do
242
+ let(:build_colliding_enum) do
243
+ proc do
244
+ Class.new(EnumerateIt::Base) do
245
+ associate_values :collision
246
+
247
+ custom_helpers do
248
+ def list
249
+ '...'
250
+ end
251
+ end
252
+ end
253
+ end
254
+ end
255
+
256
+ it 'raises an ArgumentError' do
257
+ expect(&build_colliding_enum).to raise_error(
258
+ ArgumentError,
259
+ "Custom helper(s) 'list' would override existing EnumerateIt::Base methods"
260
+ )
261
+ end
262
+ end
263
+ end
264
+
265
+ describe '.custom_helper_methods' do
266
+ context 'when methods are defined inside custom_helpers block' do
267
+ it 'returns the registered names' do
268
+ expect(TestEnumerationWithCustomHelpers.custom_helper_methods)
269
+ .to eq(%i[lookup boolean?])
270
+ end
271
+ end
272
+
273
+ context 'when there is no .custom_helpers block on an enum' do
274
+ it 'returns an empty array' do
275
+ expect(TestEnumeration.custom_helper_methods).to eq([])
276
+ end
277
+ end
278
+ end
279
+
223
280
  context 'associate values with a list' do
224
281
  it 'creates constants for each enumeration value' do
225
282
  expect(TestEnumerationWithList::FIRST).to eq('first')
@@ -235,7 +292,7 @@ describe EnumerateIt::Base do
235
292
  subject(:enumeration) { create_enumeration_class_with_sort_mode(nil) }
236
293
 
237
294
  it 'does not sort' do
238
- expect(enumeration.to_a).to eq([%w[xyz 1], %w[fgh 2], %w[abc 3], %w[jkl 0]])
295
+ expect(enumeration.to_a).to eq([%w[xyz 1], %w[fgh 2], %w[abc 3], %w[ábc 4], %w[jkl 0]])
239
296
  end
240
297
  end
241
298
 
@@ -245,25 +302,31 @@ describe EnumerateIt::Base do
245
302
  context 'by value' do
246
303
  let(:sort_mode) { :value }
247
304
 
248
- it { expect(enumeration.to_a).to eq([%w[jkl 0], %w[xyz 1], %w[fgh 2], %w[abc 3]]) }
305
+ it { expect(enumeration.to_a).to eq([%w[jkl 0], %w[xyz 1], %w[fgh 2], %w[abc 3], %w[ábc 4]]) }
249
306
  end
250
307
 
251
308
  context 'by name' do
252
309
  let(:sort_mode) { :name }
253
310
 
254
- it { expect(enumeration.to_a).to eq([%w[fgh 2], %w[xyz 1], %w[abc 3], %w[jkl 0]]) }
311
+ it { expect(enumeration.to_a).to eq([%w[fgh 2], %w[ábc 4], %w[xyz 1], %w[abc 3], %w[jkl 0]]) }
255
312
  end
256
313
 
257
314
  context 'by translation' do
258
315
  let(:sort_mode) { :translation }
259
316
 
260
- it { expect(enumeration.to_a).to eq([%w[abc 3], %w[fgh 2], %w[jkl 0], %w[xyz 1]]) }
317
+ it { expect(enumeration.to_a).to eq([%w[abc 3], %w[fgh 2], %w[jkl 0], %w[xyz 1], %w[ábc 4]]) }
318
+ end
319
+
320
+ context 'by normalize translation' do
321
+ let(:sort_mode) { :normalize_translation }
322
+
323
+ it { expect(enumeration.to_a).to eq([%w[abc 3], %w[ábc 4], %w[fgh 2], %w[jkl 0], %w[xyz 1]]) }
261
324
  end
262
325
 
263
326
  context 'by nothing' do
264
327
  let(:sort_mode) { :none }
265
328
 
266
- it { expect(enumeration.to_a).to eq([%w[xyz 1], %w[fgh 2], %w[abc 3], %w[jkl 0]]) }
329
+ it { expect(enumeration.to_a).to eq([%w[xyz 1], %w[fgh 2], %w[abc 3], %w[ábc 4], %w[jkl 0]]) }
267
330
  end
268
331
  end
269
332
 
@@ -275,11 +338,11 @@ describe EnumerateIt::Base do
275
338
  attr_accessor :bla
276
339
 
277
340
  class << self
278
- def validates_inclusion_of(_attribute, _options)
341
+ def validates_inclusion_of(_attribute, _options) # rubocop:disable Naming/PredicateMethod
279
342
  true
280
343
  end
281
344
 
282
- def validates_presence_of
345
+ def validates_presence_of # rubocop:disable Naming/PredicateMethod
283
346
  true
284
347
  end
285
348
  end
@@ -4,6 +4,7 @@ describe EnumerateIt do
4
4
  let :test_class do
5
5
  Class.new do
6
6
  extend EnumerateIt
7
+
7
8
  attr_accessor :foobar
8
9
 
9
10
  has_enumeration_for :foobar, with: TestEnumeration
@@ -41,6 +42,15 @@ describe EnumerateIt do
41
42
  expect(target).not_to respond_to(:value_1?)
42
43
  end
43
44
 
45
+ it 'defaults to not creating custom helper methods' do
46
+ klass = Class.new do
47
+ extend EnumerateIt
48
+
49
+ has_enumeration_for :foobar, with: TestEnumerationWithCustomHelpers
50
+ end
51
+ expect(klass.new).not_to respond_to(:lookup)
52
+ end
53
+
44
54
  it 'stores the enumeration class in a class-level hash' do
45
55
  expect(test_class.enumerations[:foobar]).to eq(TestEnumeration)
46
56
  end
@@ -71,6 +81,7 @@ describe EnumerateIt do
71
81
  let :test_class_for_enumeration_without_array do
72
82
  Class.new do
73
83
  extend EnumerateIt
84
+
74
85
  attr_accessor :foobar
75
86
 
76
87
  has_enumeration_for :foobar, with: TestEnumerationWithoutArray
@@ -102,6 +113,7 @@ describe EnumerateIt do
102
113
  let :foo_bar_class do
103
114
  Class.new do
104
115
  extend EnumerateIt
116
+
105
117
  attr_accessor :test_enumeration
106
118
 
107
119
  has_enumeration_for :test_enumeration
@@ -124,12 +136,13 @@ describe EnumerateIt do
124
136
  # rubocop:disable Lint/ConstantDefinitionInBlock
125
137
  # rubocop:disable RSpec/LeakyConstantDeclaration
126
138
  class NestedEnum < EnumerateIt::Base
127
- associate_values foo: %w[1 Blerrgh], bar: ['2' => 'Blarghhh']
139
+ associate_values foo: %w[1 Blerrgh], bar: [{ '2' => 'Blarghhh' }]
128
140
  end
129
141
  # rubocop:enable RSpec/LeakyConstantDeclaration
130
142
  # rubocop:enable Lint/ConstantDefinitionInBlock
131
143
 
132
144
  extend EnumerateIt
145
+
133
146
  attr_accessor :nested_enum
134
147
 
135
148
  has_enumeration_for :nested_enum
@@ -151,6 +164,7 @@ describe EnumerateIt do
151
164
  let :test_class_with_helper do
152
165
  Class.new do
153
166
  extend EnumerateIt
167
+
154
168
  attr_accessor :foobar
155
169
 
156
170
  has_enumeration_for :foobar, with: TestEnumeration, create_helpers: true
@@ -192,6 +206,7 @@ describe EnumerateIt do
192
206
  let :test_class_with_prefixed_helper do
193
207
  Class.new do
194
208
  extend EnumerateIt
209
+
195
210
  attr_accessor :foobar
196
211
 
197
212
  has_enumeration_for :foobar, with: TestEnumeration, create_helpers: { prefix: true }
@@ -234,6 +249,7 @@ describe EnumerateIt do
234
249
  let :polymorphic_class do
235
250
  Class.new do
236
251
  extend EnumerateIt
252
+
237
253
  attr_accessor :foo
238
254
 
239
255
  has_enumeration_for :foo, with: PolymorphicEnum, create_helpers: { polymorphic: true }
@@ -260,6 +276,7 @@ describe EnumerateIt do
260
276
  let :polymorphic_class_with_suffix do
261
277
  Class.new do
262
278
  extend EnumerateIt
279
+
263
280
  attr_accessor :foo
264
281
 
265
282
  has_enumeration_for :foo, with: PolymorphicEnum,
@@ -280,6 +297,62 @@ describe EnumerateIt do
280
297
  end
281
298
  end
282
299
  end
300
+
301
+ context 'with custom_helpers defined on the enumeration' do
302
+ context 'creates methods that delegates to the enum' do
303
+ let :test_class_with_custom_helpers do
304
+ Class.new do
305
+ extend EnumerateIt
306
+
307
+ attr_accessor :foobar
308
+
309
+ has_enumeration_for :foobar, with: TestEnumerationWithCustomHelpers,
310
+ create_helpers: true
311
+
312
+ def initialize(foobar)
313
+ @foobar = foobar
314
+ end
315
+ end
316
+ end
317
+
318
+ it 'returns value respective to attribute value' do
319
+ target = test_class_with_custom_helpers.new(TestEnumerationWithCustomHelpers::VALUE_ONE)
320
+ expect(target.lookup).to eq(:one)
321
+ expect(target.boolean?).to be(true)
322
+ end
323
+
324
+ it 'returns nil when attribute is nil' do
325
+ target = test_class_with_custom_helpers.new(nil)
326
+ expect(target.lookup).to be_nil
327
+ expect(target.boolean?).to be_nil
328
+ end
329
+ end
330
+
331
+ context 'create methods with prefix that delegates to the enum' do
332
+ let :test_class_with_prefixed_custom_helpers do
333
+ Class.new do
334
+ extend EnumerateIt
335
+
336
+ attr_accessor :foobar
337
+
338
+ has_enumeration_for :foobar, with: TestEnumerationWithCustomHelpers,
339
+ create_helpers: { prefix: true }
340
+
341
+ def initialize(foobar)
342
+ @foobar = foobar
343
+ end
344
+ end
345
+ end
346
+
347
+ it 'method is prefixed with the attribute name' do
348
+ target = test_class_with_prefixed_custom_helpers.new(TestEnumerationWithCustomHelpers::VALUE_ONE)
349
+ expect(target.foobar_lookup).to eq(:one)
350
+ expect(target.foobar_boolean?).to be(true)
351
+ expect(target).not_to respond_to(:lookup)
352
+ expect(target).not_to respond_to(:boolean?)
353
+ end
354
+ end
355
+ end
283
356
  end
284
357
 
285
358
  describe 'using the :create_scopes option' do
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
2
2
 
3
3
  require 'enumerate_it'
4
4
 
5
+ require 'logger' # Required by Rails 7.0 or older - https://stackoverflow.com/a/79385484/1445184
5
6
  require 'active_support/all'
6
7
  require 'active_record'
7
8
 
@@ -2,7 +2,8 @@ class TestEnumeration < EnumerateIt::Base
2
2
  associate_values(
3
3
  value_1: ['1', 'Hey, I am 1!'],
4
4
  value_2: ['2', 'Hey, I am 2!'],
5
- value_3: ['3', 'Hey, I am 3!']
5
+ value_3: ['3', 'Hey, I am 3!'],
6
+ value_4: ['4', 'Héy, I ãm 2!']
6
7
  )
7
8
  end
8
9
 
@@ -72,7 +73,22 @@ def create_enumeration_class_with_sort_mode(sort_mode)
72
73
  foo: %w[1 xyz],
73
74
  bar: %w[2 fgh],
74
75
  omg: %w[3 abc],
76
+ bra: %w[4 ábc],
75
77
  zomg: %w[0 jkl]
76
78
  )
77
79
  end
78
80
  end
81
+
82
+ class TestEnumerationWithCustomHelpers < EnumerateIt::Base
83
+ associate_values value_one: '1', value_two: '2'
84
+
85
+ custom_helpers do
86
+ def lookup(value)
87
+ { '1' => :one, '2' => :two }[value]
88
+ end
89
+
90
+ def boolean?(value)
91
+ value == '1'
92
+ end
93
+ end
94
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cássio Marques
8
8
  - Lucas Caton
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2024-04-26 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
@@ -155,18 +154,17 @@ dependencies:
155
154
  name: sqlite3
156
155
  requirement: !ruby/object:Gem::Requirement
157
156
  requirements:
158
- - - "<"
157
+ - - ">="
159
158
  - !ruby/object:Gem::Version
160
- version: '2'
159
+ version: '0'
161
160
  type: :development
162
161
  prerelease: false
163
162
  version_requirements: !ruby/object:Gem::Requirement
164
163
  requirements:
165
- - - "<"
164
+ - - ">="
166
165
  - !ruby/object:Gem::Version
167
- version: '2'
166
+ version: '0'
168
167
  description: Enumerations for Ruby with some magic powers!
169
- email:
170
168
  executables: []
171
169
  extensions: []
172
170
  extra_rdoc_files: []
@@ -174,6 +172,7 @@ files:
174
172
  - ".github/dependabot.yml"
175
173
  - ".github/workflows/ci.yml"
176
174
  - ".gitignore"
175
+ - ".prettierrc.json"
177
176
  - ".rspec"
178
177
  - ".rubocop.yml"
179
178
  - Appraisals
@@ -188,6 +187,9 @@ files:
188
187
  - gemfiles/rails_6.1.gemfile
189
188
  - gemfiles/rails_7.0.gemfile
190
189
  - gemfiles/rails_7.1.gemfile
190
+ - gemfiles/rails_7.2.gemfile
191
+ - gemfiles/rails_8.0.gemfile
192
+ - gemfiles/rails_8.1.gemfile
191
193
  - lib/enumerate_it.rb
192
194
  - lib/enumerate_it/base.rb
193
195
  - lib/enumerate_it/class_methods.rb
@@ -209,7 +211,6 @@ metadata:
209
211
  source_code_uri: https://github.com/lucascaton/enumerate_it
210
212
  changelog_uri: https://github.com/lucascaton/enumerate_it/releases
211
213
  rubygems_mfa_required: 'true'
212
- post_install_message:
213
214
  rdoc_options: []
214
215
  require_paths:
215
216
  - lib
@@ -224,8 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
225
  - !ruby/object:Gem::Version
225
226
  version: '0'
226
227
  requirements: []
227
- rubygems_version: 3.5.9
228
- signing_key:
228
+ rubygems_version: 4.0.3
229
229
  specification_version: 4
230
230
  summary: Ruby Enumerations
231
231
  test_files: []