enumerate_it 2.0.0 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -25
- data/Appraisals +1 -1
- data/Gemfile.lock +17 -15
- data/README.md +3 -3
- data/enumerate_it.gemspec +7 -2
- data/gemfiles/{rails_4.0.gemfile → rails_6.0.gemfile} +3 -3
- data/lib/enumerate_it/base.rb +4 -0
- data/lib/enumerate_it/class_methods.rb +1 -0
- data/lib/enumerate_it/version.rb +1 -1
- data/spec/enumerate_it/base_spec.rb +26 -19
- data/spec/enumerate_it_spec.rb +77 -67
- metadata +11 -11
- data/.ci/prepare +0 -7
- data/gemfiles/rails_4.1.gemfile +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea9fe8ef6bc85658654ff6cbf417dfdda5be1f721af22961bc4a2a1426b08650
|
4
|
+
data.tar.gz: 51cd4a6ddcf4ce40a61750b8030e9cdd2691ab2f6b25734e49496a24c2fa23aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97d39ade6cf19cac788ac7e9257d17a5f90f6e5af75d5008bfa18939ad863ad875ab3811ddc67064146aba540ac4f27e3eccec5f4cc4674aa0f628e941a81a81
|
7
|
+
data.tar.gz: b94f944605f30a61d17ad38b7f1f771a6dd0501af3d7879d07d45319dad794ba41681aa4fc75b6e935e075ff239167f257c29aa94886d8b1b2f1b7ab4666ef5d
|
data/.travis.yml
CHANGED
@@ -4,44 +4,27 @@ sudo: false
|
|
4
4
|
|
5
5
|
cache: bundler
|
6
6
|
|
7
|
-
before_install:
|
8
|
-
- ruby .ci/prepare
|
9
|
-
|
10
7
|
script:
|
11
|
-
- "ruby -e \"RUBY_VERSION == '2.6.
|
8
|
+
- "ruby -e \"RUBY_VERSION == '2.6.4' ? system('bundle exec rubocop') : exit(0)\""
|
12
9
|
- bundle exec rake spec
|
13
10
|
|
14
11
|
rvm:
|
15
|
-
- 2.
|
16
|
-
- 2.
|
17
|
-
- 2.4
|
18
|
-
- 2.5.5
|
19
|
-
- 2.6.3
|
12
|
+
- 2.4.7
|
13
|
+
- 2.5.6
|
14
|
+
- 2.6.4
|
20
15
|
|
21
16
|
gemfile:
|
22
|
-
- gemfiles/rails_4.0.gemfile
|
23
|
-
- gemfiles/rails_4.1.gemfile
|
24
17
|
- gemfiles/rails_4.2.gemfile
|
25
18
|
- gemfiles/rails_5.0.gemfile
|
26
19
|
- gemfiles/rails_5.1.gemfile
|
27
20
|
- gemfiles/rails_5.2.gemfile
|
21
|
+
- gemfiles/rails_6.0.gemfile
|
28
22
|
|
29
23
|
matrix:
|
30
24
|
exclude:
|
31
|
-
# Rails
|
32
|
-
|
33
|
-
|
34
|
-
gemfile: gemfiles/rails_4.0.gemfile
|
35
|
-
- rvm: 2.4.6
|
36
|
-
gemfile: gemfiles/rails_4.1.gemfile
|
37
|
-
- rvm: 2.5.5
|
38
|
-
gemfile: gemfiles/rails_4.0.gemfile
|
39
|
-
- rvm: 2.5.5
|
40
|
-
gemfile: gemfiles/rails_4.1.gemfile
|
41
|
-
- rvm: 2.6.3
|
42
|
-
gemfile: gemfiles/rails_4.0.gemfile
|
43
|
-
- rvm: 2.6.3
|
44
|
-
gemfile: gemfiles/rails_4.1.gemfile
|
25
|
+
# Rails 6 requires Ruby 2.5+
|
26
|
+
- rvm: 2.4.7
|
27
|
+
gemfile: gemfiles/rails_6.0.gemfile
|
45
28
|
|
46
29
|
addons:
|
47
30
|
code_climate:
|
data/Appraisals
CHANGED
@@ -4,7 +4,7 @@ require 'json'
|
|
4
4
|
rails_versions = JSON.parse(Net::HTTP.get(URI('https://rubygems.org/api/v1/versions/rails.json')))
|
5
5
|
.group_by { |version| version['number'] }.keys.reject { |key| key =~ /rc|racecar|beta|pre/ }
|
6
6
|
|
7
|
-
%w[4.
|
7
|
+
%w[4.2 5.0 5.1 5.2 6.0].each do |version|
|
8
8
|
appraise "rails_#{version}" do
|
9
9
|
current_version = rails_versions
|
10
10
|
.select { |key| key.match(/\A#{version}/) }
|
data/Gemfile.lock
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enumerate_it (
|
5
|
-
activesupport (>= 4.
|
4
|
+
enumerate_it (3.0.0.rc1)
|
5
|
+
activesupport (>= 4.2.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (
|
10
|
+
activesupport (6.0.0)
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
12
|
i18n (>= 0.7, < 2)
|
13
13
|
minitest (~> 5.1)
|
14
14
|
tzinfo (~> 1.1)
|
15
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
15
16
|
appraisal (2.2.0)
|
16
17
|
bundler
|
17
18
|
rake
|
@@ -22,46 +23,47 @@ GEM
|
|
22
23
|
diff-lcs (1.3)
|
23
24
|
i18n (1.6.0)
|
24
25
|
concurrent-ruby (~> 1.0)
|
25
|
-
jaro_winkler (1.5.
|
26
|
+
jaro_winkler (1.5.3)
|
26
27
|
method_source (0.9.2)
|
27
28
|
minitest (5.11.3)
|
28
29
|
parallel (1.17.0)
|
29
|
-
parser (2.6.
|
30
|
+
parser (2.6.4.1)
|
30
31
|
ast (~> 2.4.0)
|
31
32
|
pry (0.12.2)
|
32
33
|
coderay (~> 1.1.0)
|
33
34
|
method_source (~> 0.9.0)
|
34
35
|
rainbow (3.0.0)
|
35
|
-
rake (12.3.
|
36
|
+
rake (12.3.3)
|
36
37
|
rspec (3.8.0)
|
37
38
|
rspec-core (~> 3.8.0)
|
38
39
|
rspec-expectations (~> 3.8.0)
|
39
40
|
rspec-mocks (~> 3.8.0)
|
40
|
-
rspec-core (3.8.
|
41
|
+
rspec-core (3.8.2)
|
41
42
|
rspec-support (~> 3.8.0)
|
42
|
-
rspec-expectations (3.8.
|
43
|
+
rspec-expectations (3.8.4)
|
43
44
|
diff-lcs (>= 1.2.0, < 2.0)
|
44
45
|
rspec-support (~> 3.8.0)
|
45
|
-
rspec-mocks (3.8.
|
46
|
+
rspec-mocks (3.8.1)
|
46
47
|
diff-lcs (>= 1.2.0, < 2.0)
|
47
48
|
rspec-support (~> 3.8.0)
|
48
|
-
rspec-support (3.8.
|
49
|
-
rubocop (0.
|
49
|
+
rspec-support (3.8.2)
|
50
|
+
rubocop (0.74.0)
|
50
51
|
jaro_winkler (~> 1.5.1)
|
51
52
|
parallel (~> 1.10)
|
52
53
|
parser (>= 2.6)
|
53
54
|
rainbow (>= 2.2.2, < 4.0)
|
54
55
|
ruby-progressbar (~> 1.7)
|
55
56
|
unicode-display_width (>= 1.4.0, < 1.7)
|
56
|
-
rubocop-rspec (1.
|
57
|
+
rubocop-rspec (1.35.0)
|
57
58
|
rubocop (>= 0.60.0)
|
58
|
-
ruby-progressbar (1.10.
|
59
|
+
ruby-progressbar (1.10.1)
|
59
60
|
thor (0.20.3)
|
60
61
|
thread_safe (0.3.6)
|
61
62
|
tzinfo (1.2.5)
|
62
63
|
thread_safe (~> 0.1)
|
63
64
|
unicode-display_width (1.6.0)
|
64
|
-
wwtd (1.
|
65
|
+
wwtd (1.4.0)
|
66
|
+
zeitwerk (2.1.10)
|
65
67
|
|
66
68
|
PLATFORMS
|
67
69
|
ruby
|
@@ -78,4 +80,4 @@ DEPENDENCIES
|
|
78
80
|
wwtd
|
79
81
|
|
80
82
|
BUNDLED WITH
|
81
|
-
2.0.
|
83
|
+
2.0.2
|
data/README.md
CHANGED
@@ -403,8 +403,8 @@ You sure can! 😄
|
|
403
403
|
|
404
404
|
#### What versions of Ruby and Rails are supported?
|
405
405
|
|
406
|
-
* **Ruby**: `2.
|
407
|
-
* **Rails** `
|
406
|
+
* **Ruby**: `2.4+`
|
407
|
+
* **Rails** `4.2+`
|
408
408
|
|
409
409
|
All versions are tested via
|
410
410
|
[Travis](https://github.com/lucascaton/enumerate_it/blob/master/.travis.yml).
|
@@ -528,7 +528,7 @@ Changes are maintained under [Releases page](https://github.com/lucascaton/enume
|
|
528
528
|
* Fork the project.
|
529
529
|
* Make your feature addition or bug fix.
|
530
530
|
* Add tests for it. This is important so we don't break it in a future version unintentionally.
|
531
|
-
* [Optional] Run the tests agaist a specific Gemfile: `$ bundle exec appraisal
|
531
|
+
* [Optional] Run the tests agaist a specific Gemfile: `$ bundle exec appraisal rails_6.0 rake spec`.
|
532
532
|
* Run the tests agaist all supported versions: `$ bundle exec rake` (or `$ bundle exec wwtd`)
|
533
533
|
* Commit, but please do not mess with `Rakefile`, version, or history.
|
534
534
|
* Send a Pull Request. Bonus points for topic branches.
|
data/enumerate_it.gemspec
CHANGED
@@ -12,9 +12,14 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.name = 'enumerate_it'
|
13
13
|
gem.require_paths = ['lib']
|
14
14
|
gem.version = EnumerateIt::VERSION
|
15
|
-
gem.required_ruby_version = '>= 2.
|
15
|
+
gem.required_ruby_version = '>= 2.4.7'
|
16
16
|
|
17
|
-
gem.
|
17
|
+
gem.metadata = {
|
18
|
+
'source_code_uri' => 'https://github.com/lucascaton/enumerate_it',
|
19
|
+
'changelog_uri' => 'https://github.com/lucascaton/enumerate_it/releases'
|
20
|
+
}
|
21
|
+
|
22
|
+
gem.add_dependency 'activesupport', '>= 4.2.0'
|
18
23
|
|
19
24
|
gem.add_development_dependency 'appraisal'
|
20
25
|
gem.add_development_dependency 'bundler'
|
data/lib/enumerate_it/base.rb
CHANGED
data/lib/enumerate_it/version.rb
CHANGED
@@ -113,6 +113,12 @@ describe EnumerateIt::Base do
|
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
+
describe '.to_h' do
|
117
|
+
it 'returns a hash' do
|
118
|
+
expect(TestEnumerationWithoutArray.to_h).to eq(value_one: '1', value_two: '2')
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
116
122
|
describe '.to_json' do
|
117
123
|
it 'gives a valid json back' do
|
118
124
|
I18n.locale = :inexsistent
|
@@ -246,12 +252,14 @@ describe EnumerateIt::Base do
|
|
246
252
|
end
|
247
253
|
|
248
254
|
context 'when included in ActiveRecord::Base' do
|
249
|
-
|
250
|
-
|
255
|
+
let(:active_record_stub_class) do
|
256
|
+
Class.new do
|
257
|
+
extend EnumerateIt
|
258
|
+
|
251
259
|
attr_accessor :bla
|
252
260
|
|
253
261
|
class << self
|
254
|
-
def validates_inclusion_of(_attribute)
|
262
|
+
def validates_inclusion_of(_attribute, _options)
|
255
263
|
true
|
256
264
|
end
|
257
265
|
|
@@ -260,42 +268,41 @@ describe EnumerateIt::Base do
|
|
260
268
|
end
|
261
269
|
end
|
262
270
|
end
|
263
|
-
|
264
|
-
allow(ActiveRecordStub).to receive(:validates_inclusion_of).and_return(true)
|
265
|
-
ActiveRecordStub.extend EnumerateIt
|
266
271
|
end
|
267
272
|
|
268
273
|
it 'creates a validation for inclusion' do
|
269
|
-
expect(
|
274
|
+
expect(active_record_stub_class)
|
270
275
|
.to receive(:validates_inclusion_of).with(:bla, in: TestEnumeration.list, allow_blank: true)
|
271
276
|
|
272
|
-
|
277
|
+
active_record_stub_class.class_eval do
|
273
278
|
has_enumeration_for :bla, with: TestEnumeration
|
274
279
|
end
|
275
280
|
end
|
276
281
|
|
277
282
|
context 'using the :required option' do
|
278
283
|
before do
|
279
|
-
allow(
|
284
|
+
allow(active_record_stub_class).to receive(:validates_presence_of).and_return(true)
|
280
285
|
end
|
281
286
|
|
282
287
|
it 'creates a validation for presence' do
|
283
|
-
expect(
|
284
|
-
|
288
|
+
expect(active_record_stub_class).to receive(:validates_presence_of)
|
289
|
+
active_record_stub_class.class_eval do
|
285
290
|
has_enumeration_for :bla, with: TestEnumeration, required: true
|
286
291
|
end
|
287
292
|
end
|
288
293
|
|
289
294
|
it 'passes the given options to the validation method' do
|
290
|
-
expect(
|
291
|
-
|
295
|
+
expect(active_record_stub_class)
|
296
|
+
.to receive(:validates_presence_of).with(:bla, if: :some_method)
|
297
|
+
|
298
|
+
active_record_stub_class.class_eval do
|
292
299
|
has_enumeration_for :bla, with: TestEnumeration, required: { if: :some_method }
|
293
300
|
end
|
294
301
|
end
|
295
302
|
|
296
303
|
it 'does not require the attribute by default' do
|
297
|
-
expect(
|
298
|
-
|
304
|
+
expect(active_record_stub_class).not_to receive(:validates_presence_of)
|
305
|
+
active_record_stub_class.class_eval do
|
299
306
|
has_enumeration_for :bla, with: TestEnumeration
|
300
307
|
end
|
301
308
|
end
|
@@ -303,15 +310,15 @@ describe EnumerateIt::Base do
|
|
303
310
|
|
304
311
|
context 'using :skip_validation option' do
|
305
312
|
it "doesn't create a validation for inclusion" do
|
306
|
-
expect(
|
307
|
-
|
313
|
+
expect(active_record_stub_class).not_to receive(:validates_inclusion_of)
|
314
|
+
active_record_stub_class.class_eval do
|
308
315
|
has_enumeration_for :bla, with: TestEnumeration, skip_validation: true
|
309
316
|
end
|
310
317
|
end
|
311
318
|
|
312
319
|
it "doesn't create a validation for presence" do
|
313
|
-
expect(
|
314
|
-
|
320
|
+
expect(active_record_stub_class).not_to receive(:validates_presence_of)
|
321
|
+
active_record_stub_class.class_eval do
|
315
322
|
has_enumeration_for :bla, with: TestEnumeration, require: true, skip_validation: true
|
316
323
|
end
|
317
324
|
end
|
data/spec/enumerate_it_spec.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe EnumerateIt do
|
4
|
-
let :
|
5
|
-
|
4
|
+
let :test_class do
|
5
|
+
Class.new do
|
6
6
|
extend EnumerateIt
|
7
7
|
attr_accessor :foobar
|
8
8
|
has_enumeration_for :foobar, with: TestEnumeration
|
@@ -13,10 +13,10 @@ describe EnumerateIt do
|
|
13
13
|
|
14
14
|
I18n.locale = :en
|
15
15
|
end
|
16
|
-
|
17
|
-
TestClass.new(TestEnumeration::VALUE_2)
|
18
16
|
end
|
19
17
|
|
18
|
+
let(:target) { test_class.new(TestEnumeration::VALUE_2) }
|
19
|
+
|
20
20
|
context 'associating an enumeration with a class attribute' do
|
21
21
|
it "creates an humanized description for the attribute's value" do
|
22
22
|
expect(target.foobar_humanize).to eq('Hey, I am 2!')
|
@@ -32,17 +32,12 @@ describe EnumerateIt do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'stores the enumeration class in a class-level hash' do
|
35
|
-
expect(
|
35
|
+
expect(test_class.enumerations[:foobar]).to eq(TestEnumeration)
|
36
36
|
end
|
37
37
|
|
38
38
|
context 'use the same enumeration from an inherited class' do
|
39
|
-
let
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
SomeClassWithoutEnum.new
|
44
|
-
end
|
45
|
-
|
39
|
+
let(:some_class_without_enum) { Class.new(BaseClass) }
|
40
|
+
let(:target) { some_class_without_enum.new }
|
46
41
|
let(:base) { BaseClass.new }
|
47
42
|
|
48
43
|
it 'has use the correct class' do
|
@@ -52,14 +47,8 @@ describe EnumerateIt do
|
|
52
47
|
end
|
53
48
|
|
54
49
|
context 'declaring a simple enum on an inherited class' do
|
55
|
-
let :
|
56
|
-
|
57
|
-
has_enumeration_for :foobar
|
58
|
-
end
|
59
|
-
|
60
|
-
SomeClass.new
|
61
|
-
end
|
62
|
-
|
50
|
+
let(:some_class) { Class.new(BaseClass) { has_enumeration_for :foobar } }
|
51
|
+
let(:target) { some_class.new }
|
63
52
|
let(:base) { BaseClass.new }
|
64
53
|
|
65
54
|
it 'has use the corret class' do
|
@@ -69,8 +58,8 @@ describe EnumerateIt do
|
|
69
58
|
end
|
70
59
|
|
71
60
|
context 'passing options values without the human string (just the value, without an array)' do
|
72
|
-
let :
|
73
|
-
|
61
|
+
let :test_class_for_enumeration_without_array do
|
62
|
+
Class.new do
|
74
63
|
extend EnumerateIt
|
75
64
|
attr_accessor :foobar
|
76
65
|
has_enumeration_for :foobar, with: TestEnumerationWithoutArray
|
@@ -79,8 +68,10 @@ describe EnumerateIt do
|
|
79
68
|
@foobar = foobar
|
80
69
|
end
|
81
70
|
end
|
71
|
+
end
|
82
72
|
|
83
|
-
|
73
|
+
let :target do
|
74
|
+
test_class_for_enumeration_without_array.new(TestEnumerationWithoutArray::VALUE_TWO)
|
84
75
|
end
|
85
76
|
|
86
77
|
it 'humanizes the respective hash key' do
|
@@ -97,8 +88,8 @@ describe EnumerateIt do
|
|
97
88
|
end
|
98
89
|
|
99
90
|
context 'without passing the enumeration class' do
|
100
|
-
let :
|
101
|
-
|
91
|
+
let :foo_bar_class do
|
92
|
+
Class.new do
|
102
93
|
extend EnumerateIt
|
103
94
|
attr_accessor :test_enumeration
|
104
95
|
has_enumeration_for :test_enumeration
|
@@ -107,24 +98,22 @@ describe EnumerateIt do
|
|
107
98
|
@test_enumeration = test_enumeration_value
|
108
99
|
end
|
109
100
|
end
|
110
|
-
|
111
|
-
FooBar.new(TestEnumeration::VALUE_1)
|
112
101
|
end
|
113
102
|
|
103
|
+
let(:target) { foo_bar_class.new(TestEnumeration::VALUE_1) }
|
104
|
+
|
114
105
|
it 'finds out which enumeration class to use' do
|
115
106
|
expect(target.test_enumeration_humanize).to eq('Hey, I am 1!')
|
116
107
|
end
|
117
108
|
|
118
109
|
context 'when using a nested class as the enumeration' do
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
|
-
class ClassWithNestedEnum
|
110
|
+
let :class_with_nested_enum do
|
111
|
+
Class.new do
|
112
|
+
# rubocop:disable RSpec/LeakyConstantDeclaration
|
125
113
|
class NestedEnum < EnumerateIt::Base
|
126
114
|
associate_values foo: %w[1 Blerrgh], bar: ['2' => 'Blarghhh']
|
127
115
|
end
|
116
|
+
# rubocop:enable RSpec/LeakyConstantDeclaration
|
128
117
|
|
129
118
|
extend EnumerateIt
|
130
119
|
attr_accessor :nested_enum
|
@@ -137,15 +126,15 @@ describe EnumerateIt do
|
|
137
126
|
end
|
138
127
|
|
139
128
|
it 'uses the inner class as the enumeration class' do
|
140
|
-
expect(
|
129
|
+
expect(class_with_nested_enum.new('1').nested_enum_humanize).to eq('Blerrgh')
|
141
130
|
end
|
142
131
|
end
|
143
132
|
end
|
144
133
|
end
|
145
134
|
|
146
135
|
context 'using the :create_helpers option' do
|
147
|
-
|
148
|
-
|
136
|
+
let :test_class_with_helper do
|
137
|
+
Class.new do
|
149
138
|
extend EnumerateIt
|
150
139
|
attr_accessor :foobar
|
151
140
|
has_enumeration_for :foobar, with: TestEnumeration, create_helpers: true
|
@@ -157,60 +146,85 @@ describe EnumerateIt do
|
|
157
146
|
end
|
158
147
|
|
159
148
|
it 'creates helpers methods with question marks for each enumeration option' do
|
160
|
-
target =
|
149
|
+
target = test_class_with_helper.new(TestEnumeration::VALUE_2)
|
161
150
|
expect(target).to be_value_2
|
162
151
|
expect(target).not_to be_value_1
|
163
152
|
end
|
164
153
|
|
165
154
|
it 'creates a mutator method for each enumeration value' do
|
166
155
|
%i[value_1 value_2 value_3].each do |value|
|
167
|
-
expect(
|
156
|
+
expect(test_class_with_helper.new(TestEnumeration::VALUE_1)).to respond_to(:"#{value}!")
|
168
157
|
end
|
169
158
|
end
|
170
159
|
|
171
160
|
it "changes the attribute's value through mutator methods" do
|
172
|
-
target =
|
161
|
+
target = test_class_with_helper.new(TestEnumeration::VALUE_2)
|
173
162
|
target.value_3!
|
174
163
|
expect(target.foobar).to eq(TestEnumeration::VALUE_3)
|
175
164
|
end
|
176
165
|
|
166
|
+
context 'when class responds to save! method' do
|
167
|
+
it 'calls save!' do
|
168
|
+
target = test_class_with_helper.new(TestEnumeration::VALUE_2)
|
169
|
+
allow(target).to receive(:save!)
|
170
|
+
target.value_3!
|
171
|
+
expect(target).to have_received(:save!)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
177
175
|
context 'with :prefix option' do
|
178
|
-
|
179
|
-
|
176
|
+
let :test_class_with_prefixed_helper do
|
177
|
+
Class.new do
|
178
|
+
extend EnumerateIt
|
179
|
+
attr_accessor :foobar
|
180
180
|
has_enumeration_for :foobar, with: TestEnumeration, create_helpers: { prefix: true }
|
181
|
+
|
182
|
+
def initialize(foobar)
|
183
|
+
@foobar = foobar
|
184
|
+
end
|
181
185
|
end
|
182
186
|
end
|
183
187
|
|
184
188
|
it 'creates helpers methods with question marks and prefixes for each enumeration option' do
|
185
|
-
target =
|
189
|
+
target = test_class_with_prefixed_helper.new(TestEnumeration::VALUE_2)
|
186
190
|
expect(target).to be_foobar_value_2
|
187
191
|
end
|
188
192
|
|
189
193
|
it 'creates a mutator method for each enumeration value' do
|
190
194
|
%i[value_1 value_2 value_3].each do |value|
|
191
|
-
expect(
|
195
|
+
expect(test_class_with_prefixed_helper.new(TestEnumeration::VALUE_1))
|
192
196
|
.to respond_to(:"foobar_#{value}!")
|
193
197
|
end
|
194
198
|
end
|
195
199
|
|
196
200
|
it "changes the attribute's value through mutator methods" do
|
197
|
-
target =
|
201
|
+
target = test_class_with_prefixed_helper.new(TestEnumeration::VALUE_2)
|
198
202
|
target.foobar_value_3!
|
199
203
|
expect(target.foobar).to eq(TestEnumeration::VALUE_3)
|
200
204
|
end
|
205
|
+
|
206
|
+
context 'when class responds to save! method' do
|
207
|
+
it 'calls save!' do
|
208
|
+
target = test_class_with_prefixed_helper.new(TestEnumeration::VALUE_2)
|
209
|
+
allow(target).to receive(:save!)
|
210
|
+
target.foobar_value_3!
|
211
|
+
expect(target).to have_received(:save!)
|
212
|
+
end
|
213
|
+
end
|
201
214
|
end
|
202
215
|
|
203
216
|
context 'with :polymorphic option' do
|
204
|
-
|
205
|
-
|
217
|
+
let :polymorphic_class do
|
218
|
+
Class.new do
|
206
219
|
extend EnumerateIt
|
207
220
|
attr_accessor :foo
|
208
221
|
has_enumeration_for :foo, with: PolymorphicEnum, create_helpers: { polymorphic: true }
|
209
222
|
end
|
210
223
|
end
|
211
224
|
|
225
|
+
let(:target) { polymorphic_class.new }
|
226
|
+
|
212
227
|
it "calls methods on the enum constants' objects" do
|
213
|
-
target = Polymorphic.new
|
214
228
|
target.foo = PolymorphicEnum::NORMAL
|
215
229
|
|
216
230
|
expect(target.foo_object.print('Gol')).to eq("I'm Normal: Gol")
|
@@ -221,21 +235,22 @@ describe EnumerateIt do
|
|
221
235
|
end
|
222
236
|
|
223
237
|
it 'returns nil if foo is not set' do
|
224
|
-
target = Polymorphic.new
|
225
|
-
|
226
238
|
expect(target.foo_object).to be_nil
|
227
239
|
end
|
228
240
|
|
229
241
|
context 'and :suffix' do
|
230
|
-
|
231
|
-
|
242
|
+
let :polymorphic_class_with_suffix do
|
243
|
+
Class.new do
|
244
|
+
extend EnumerateIt
|
245
|
+
attr_accessor :foo
|
232
246
|
has_enumeration_for :foo, with: PolymorphicEnum,
|
233
247
|
create_helpers: { polymorphic: { suffix: '_strategy' } }
|
234
248
|
end
|
235
249
|
end
|
236
250
|
|
251
|
+
let(:target) { polymorphic_class_with_suffix.new }
|
252
|
+
|
237
253
|
it "calls methods on the enum constants' objects" do
|
238
|
-
target = Polymorphic.new
|
239
254
|
target.foo = PolymorphicEnum::NORMAL
|
240
255
|
|
241
256
|
expect(target.foo_strategy.print('Gol')).to eq("I'm Normal: Gol")
|
@@ -250,17 +265,16 @@ describe EnumerateIt do
|
|
250
265
|
|
251
266
|
describe 'using the :create_scopes option' do
|
252
267
|
context 'if the hosting class responds to :scope' do
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
class TestClassWithScope < ActiveRecord::Base
|
268
|
+
let :test_class_with_scope do
|
269
|
+
Class.new(ActiveRecord::Base) do
|
270
|
+
self.table_name = 'test_class_with_scopes'
|
257
271
|
has_enumeration_for :foobar, with: TestEnumeration, create_scopes: true
|
258
272
|
end
|
259
273
|
end
|
260
274
|
|
261
275
|
it 'creates a scope for each enumeration value' do
|
262
276
|
TestEnumeration.enumeration.each_key do |symbol|
|
263
|
-
expect(
|
277
|
+
expect(test_class_with_scope).to respond_to(symbol)
|
264
278
|
end
|
265
279
|
end
|
266
280
|
|
@@ -268,29 +282,25 @@ describe EnumerateIt do
|
|
268
282
|
ActiveRecord::Schema.define { create_table :test_class_with_scopes }
|
269
283
|
|
270
284
|
TestEnumeration.enumeration.each do |symbol, pair|
|
271
|
-
expect(
|
285
|
+
expect(test_class_with_scope.public_send(symbol).to_sql)
|
272
286
|
.to match(/WHERE "test_class_with_scopes"."foobar" = \'#{pair.first}\'/)
|
273
287
|
end
|
274
288
|
end
|
275
289
|
end
|
276
290
|
|
277
291
|
context 'when the hosting class does not respond to :scope' do
|
278
|
-
|
279
|
-
class GenericClass
|
280
|
-
extend EnumerateIt
|
281
|
-
end
|
282
|
-
end
|
292
|
+
let(:generic_class) { Class.new { extend EnumerateIt } }
|
283
293
|
|
284
294
|
it 'raises no errors' do
|
285
295
|
expect do
|
286
|
-
|
296
|
+
generic_class.has_enumeration_for(:foobar, with: TestEnumeration, create_scopes: true)
|
287
297
|
end.not_to raise_error
|
288
298
|
end
|
289
299
|
end
|
290
300
|
|
291
301
|
context 'with :prefix option' do
|
292
|
-
|
293
|
-
|
302
|
+
let :other_test_class do
|
303
|
+
Class.new(ActiveRecord::Base) do
|
294
304
|
has_enumeration_for :foobar, with: TestEnumerationWithReservedWords,
|
295
305
|
create_scopes: { prefix: true }
|
296
306
|
end
|
@@ -298,7 +308,7 @@ describe EnumerateIt do
|
|
298
308
|
|
299
309
|
it 'creates a scope with prefix for each enumeration value' do
|
300
310
|
TestEnumerationWithReservedWords.enumeration.each_key do |symbol|
|
301
|
-
expect(
|
311
|
+
expect(other_test_class).to respond_to(:"foobar_#{symbol}")
|
302
312
|
end
|
303
313
|
end
|
304
314
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerate_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cássio Marques
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-09-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 4.
|
20
|
+
version: 4.2.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 4.
|
27
|
+
version: 4.2.0
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: appraisal
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -143,7 +143,6 @@ executables: []
|
|
143
143
|
extensions: []
|
144
144
|
extra_rdoc_files: []
|
145
145
|
files:
|
146
|
-
- ".ci/prepare"
|
147
146
|
- ".gitignore"
|
148
147
|
- ".rspec"
|
149
148
|
- ".rubocop.yml"
|
@@ -156,12 +155,11 @@ files:
|
|
156
155
|
- README.md
|
157
156
|
- Rakefile
|
158
157
|
- enumerate_it.gemspec
|
159
|
-
- gemfiles/rails_4.0.gemfile
|
160
|
-
- gemfiles/rails_4.1.gemfile
|
161
158
|
- gemfiles/rails_4.2.gemfile
|
162
159
|
- gemfiles/rails_5.0.gemfile
|
163
160
|
- gemfiles/rails_5.1.gemfile
|
164
161
|
- gemfiles/rails_5.2.gemfile
|
162
|
+
- gemfiles/rails_6.0.gemfile
|
165
163
|
- lib/enumerate_it.rb
|
166
164
|
- lib/enumerate_it/base.rb
|
167
165
|
- lib/enumerate_it/class_methods.rb
|
@@ -179,7 +177,9 @@ files:
|
|
179
177
|
homepage: https://github.com/lucascaton/enumerate_it
|
180
178
|
licenses:
|
181
179
|
- MIT
|
182
|
-
metadata:
|
180
|
+
metadata:
|
181
|
+
source_code_uri: https://github.com/lucascaton/enumerate_it
|
182
|
+
changelog_uri: https://github.com/lucascaton/enumerate_it/releases
|
183
183
|
post_install_message:
|
184
184
|
rdoc_options: []
|
185
185
|
require_paths:
|
@@ -188,12 +188,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
188
188
|
requirements:
|
189
189
|
- - ">="
|
190
190
|
- !ruby/object:Gem::Version
|
191
|
-
version: 2.
|
191
|
+
version: 2.4.7
|
192
192
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
193
193
|
requirements:
|
194
|
-
- - "
|
194
|
+
- - ">"
|
195
195
|
- !ruby/object:Gem::Version
|
196
|
-
version:
|
196
|
+
version: 1.3.1
|
197
197
|
requirements: []
|
198
198
|
rubygems_version: 3.0.3
|
199
199
|
signing_key:
|
data/.ci/prepare
DELETED