enumerate_it 2.0.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +25 -5
- data/.travis.yml +5 -29
- data/Appraisals +2 -2
- data/Gemfile.lock +53 -42
- data/LICENSE +1 -1
- data/README.md +5 -5
- data/Rakefile +4 -11
- data/enumerate_it.gemspec +10 -3
- data/gemfiles/rails_5.1.gemfile +1 -1
- data/gemfiles/rails_5.2.gemfile +3 -3
- data/gemfiles/rails_6.0.gemfile +9 -0
- data/lib/enumerate_it/base.rb +14 -8
- data/lib/enumerate_it/class_methods.rb +1 -0
- data/lib/enumerate_it/version.rb +1 -1
- data/spec/enumerate_it/base_spec.rb +37 -20
- data/spec/enumerate_it_spec.rb +89 -69
- data/spec/spec_helper.rb +1 -1
- metadata +28 -15
- data/.ci/prepare +0 -7
- data/gemfiles/rails_4.0.gemfile +0 -9
- data/gemfiles/rails_4.1.gemfile +0 -9
- data/gemfiles/rails_4.2.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: 71119e1fa6ccbb6d77ba404a1009c31a3174de08fb540bef2c98550bd6062a28
|
4
|
+
data.tar.gz: 4247b33c271222ca52293ae9c5ef55f8d4f9d9c8af28dfe198a23ef64ca4a6d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 696c8511a033407677a8cced966bb6bac68b56e0abd0b97b2d96f2400a71165e57f5685aa288ab25a7f7938682562a24933301126197e0ec491fbcbfbe1f717a
|
7
|
+
data.tar.gz: 59ddf99c1c9358908abc21fdc7b4fcd42513c8326ffdae8f54fba0d06ae4738720ef56827e0abdfa0041cbfe2cbab870fb1950905d9b3763095d02ef5736fd1a
|
data/.rubocop.yml
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
require:
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
- rubocop-rake
|
2
4
|
|
3
5
|
inherit_mode:
|
4
6
|
merge:
|
5
7
|
- Exclude
|
6
8
|
|
7
9
|
AllCops:
|
8
|
-
|
9
|
-
|
10
|
+
NewCops: enable
|
11
|
+
TargetRubyVersion: 2.5
|
10
12
|
|
11
13
|
Exclude:
|
12
14
|
- 'lib/generators/enumerate_it/enum/templates/**/*'
|
@@ -18,14 +20,20 @@ Gemspec/RequiredRubyVersion:
|
|
18
20
|
Layout/EndAlignment:
|
19
21
|
Enabled: false
|
20
22
|
|
21
|
-
|
23
|
+
Layout/LineLength:
|
22
24
|
Max: 100
|
23
25
|
|
26
|
+
Lint/RaiseException:
|
27
|
+
Enabled: true
|
28
|
+
|
29
|
+
Lint/StructNewOverride:
|
30
|
+
Enabled: true
|
31
|
+
|
24
32
|
Metrics/BlockLength:
|
25
33
|
Exclude:
|
26
34
|
- 'spec/**/*'
|
27
35
|
|
28
|
-
Layout/
|
36
|
+
Layout/HashAlignment:
|
29
37
|
EnforcedColonStyle: table
|
30
38
|
EnforcedLastArgumentHashStyle: ignore_implicit
|
31
39
|
|
@@ -47,10 +55,22 @@ Style/Documentation:
|
|
47
55
|
Style/GuardClause:
|
48
56
|
MinBodyLength: 3
|
49
57
|
|
58
|
+
Style/HashEachMethods:
|
59
|
+
Enabled: true
|
60
|
+
|
61
|
+
Style/HashTransformKeys:
|
62
|
+
Enabled: true
|
63
|
+
|
64
|
+
Style/HashTransformValues:
|
65
|
+
Enabled: true
|
66
|
+
|
50
67
|
Naming/PredicateName:
|
51
68
|
Exclude:
|
52
69
|
- 'lib/enumerate_it/class_methods.rb'
|
53
70
|
|
71
|
+
Naming/VariableNumber:
|
72
|
+
EnforcedStyle: snake_case
|
73
|
+
|
54
74
|
RSpec/MultipleExpectations:
|
55
75
|
Enabled: false
|
56
76
|
|
data/.travis.yml
CHANGED
@@ -4,44 +4,20 @@ 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.
|
8
|
+
- "ruby -e \"RUBY_VERSION == '2.7.2' ? system('bundle exec rubocop') : exit(0)\""
|
12
9
|
- bundle exec rake spec
|
13
10
|
|
14
11
|
rvm:
|
15
|
-
- 2.
|
16
|
-
- 2.
|
17
|
-
- 2.
|
18
|
-
- 2.5.5
|
19
|
-
- 2.6.3
|
12
|
+
- 2.5.8
|
13
|
+
- 2.6.6
|
14
|
+
- 2.7.2
|
20
15
|
|
21
16
|
gemfile:
|
22
|
-
- gemfiles/rails_4.0.gemfile
|
23
|
-
- gemfiles/rails_4.1.gemfile
|
24
|
-
- gemfiles/rails_4.2.gemfile
|
25
17
|
- gemfiles/rails_5.0.gemfile
|
26
18
|
- gemfiles/rails_5.1.gemfile
|
27
19
|
- gemfiles/rails_5.2.gemfile
|
28
|
-
|
29
|
-
matrix:
|
30
|
-
exclude:
|
31
|
-
# Rails 4.0 and 4.1 requires an old version of `json` gem, which isn't compatible with Ruby 2.4+
|
32
|
-
# https://github.com/flori/json/issues/308
|
33
|
-
- rvm: 2.4.6
|
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
|
20
|
+
- gemfiles/rails_6.0.gemfile
|
45
21
|
|
46
22
|
addons:
|
47
23
|
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[
|
7
|
+
%w[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}/) }
|
@@ -13,6 +13,6 @@ rails_versions = JSON.parse(Net::HTTP.get(URI('https://rubygems.org/api/v1/versi
|
|
13
13
|
gem 'activesupport', "~> #{current_version}"
|
14
14
|
gem 'activerecord', "~> #{current_version}"
|
15
15
|
|
16
|
-
gem 'sqlite3', Gem::Version.new(version) > Gem::Version.new(5.0) ? '~> 1.4.
|
16
|
+
gem 'sqlite3', Gem::Version.new(version) > Gem::Version.new(5.0) ? '~> 1.4.2' : '< 1.4'
|
17
17
|
end
|
18
18
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,67 +1,77 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enumerate_it (2.0
|
5
|
-
activesupport (>=
|
4
|
+
enumerate_it (3.2.0)
|
5
|
+
activesupport (>= 5.0.7.2)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (
|
10
|
+
activesupport (6.0.3.4)
|
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
|
-
|
15
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
16
|
+
appraisal (2.3.0)
|
16
17
|
bundler
|
17
18
|
rake
|
18
19
|
thor (>= 0.14.0)
|
19
|
-
ast (2.4.
|
20
|
-
coderay (1.1.
|
21
|
-
concurrent-ruby (1.1.
|
22
|
-
diff-lcs (1.
|
23
|
-
i18n (1.
|
20
|
+
ast (2.4.1)
|
21
|
+
coderay (1.1.3)
|
22
|
+
concurrent-ruby (1.1.7)
|
23
|
+
diff-lcs (1.4.4)
|
24
|
+
i18n (1.8.5)
|
24
25
|
concurrent-ruby (~> 1.0)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
method_source (~> 0.9.0)
|
26
|
+
method_source (1.0.0)
|
27
|
+
minitest (5.14.2)
|
28
|
+
parallel (1.20.1)
|
29
|
+
parser (2.7.2.0)
|
30
|
+
ast (~> 2.4.1)
|
31
|
+
pry (0.13.1)
|
32
|
+
coderay (~> 1.1)
|
33
|
+
method_source (~> 1.0)
|
34
34
|
rainbow (3.0.0)
|
35
|
-
rake (
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
rspec-
|
40
|
-
|
41
|
-
rspec-
|
42
|
-
rspec-
|
35
|
+
rake (13.0.1)
|
36
|
+
regexp_parser (2.0.0)
|
37
|
+
rexml (3.2.4)
|
38
|
+
rspec (3.10.0)
|
39
|
+
rspec-core (~> 3.10.0)
|
40
|
+
rspec-expectations (~> 3.10.0)
|
41
|
+
rspec-mocks (~> 3.10.0)
|
42
|
+
rspec-core (3.10.0)
|
43
|
+
rspec-support (~> 3.10.0)
|
44
|
+
rspec-expectations (3.10.0)
|
43
45
|
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
-
rspec-support (~> 3.
|
45
|
-
rspec-mocks (3.
|
46
|
+
rspec-support (~> 3.10.0)
|
47
|
+
rspec-mocks (3.10.0)
|
46
48
|
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
-
rspec-support (~> 3.
|
48
|
-
rspec-support (3.
|
49
|
-
rubocop (
|
50
|
-
jaro_winkler (~> 1.5.1)
|
49
|
+
rspec-support (~> 3.10.0)
|
50
|
+
rspec-support (3.10.0)
|
51
|
+
rubocop (1.5.2)
|
51
52
|
parallel (~> 1.10)
|
52
|
-
parser (>= 2.
|
53
|
+
parser (>= 2.7.1.5)
|
53
54
|
rainbow (>= 2.2.2, < 4.0)
|
55
|
+
regexp_parser (>= 1.8, < 3.0)
|
56
|
+
rexml
|
57
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
54
58
|
ruby-progressbar (~> 1.7)
|
55
|
-
unicode-display_width (>= 1.4.0, <
|
56
|
-
rubocop-
|
57
|
-
|
58
|
-
|
59
|
-
|
59
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
60
|
+
rubocop-ast (1.3.0)
|
61
|
+
parser (>= 2.7.1.5)
|
62
|
+
rubocop-rake (0.5.1)
|
63
|
+
rubocop
|
64
|
+
rubocop-rspec (2.0.1)
|
65
|
+
rubocop (~> 1.0)
|
66
|
+
rubocop-ast (>= 1.1.0)
|
67
|
+
ruby-progressbar (1.10.1)
|
68
|
+
thor (1.0.1)
|
60
69
|
thread_safe (0.3.6)
|
61
|
-
tzinfo (1.2.
|
70
|
+
tzinfo (1.2.8)
|
62
71
|
thread_safe (~> 0.1)
|
63
|
-
unicode-display_width (1.
|
64
|
-
wwtd (1.
|
72
|
+
unicode-display_width (1.7.0)
|
73
|
+
wwtd (1.4.1)
|
74
|
+
zeitwerk (2.4.2)
|
65
75
|
|
66
76
|
PLATFORMS
|
67
77
|
ruby
|
@@ -74,8 +84,9 @@ DEPENDENCIES
|
|
74
84
|
rake
|
75
85
|
rspec
|
76
86
|
rubocop
|
87
|
+
rubocop-rake
|
77
88
|
rubocop-rspec
|
78
89
|
wwtd
|
79
90
|
|
80
91
|
BUNDLED WITH
|
81
|
-
2.
|
92
|
+
2.1.4
|
data/LICENSE
CHANGED
@@ -2,7 +2,7 @@ LICENSE
|
|
2
2
|
|
3
3
|
The MIT License
|
4
4
|
|
5
|
-
Copyright (c) 2010-
|
5
|
+
Copyright (c) 2010-2020 Cássio Marques and Lucas Caton
|
6
6
|
|
7
7
|
Permission is hereby granted, free of charge, to any person obtaining
|
8
8
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
@@ -391,7 +391,7 @@ Remember that you can add validations to any kind of class and not only `ActiveR
|
|
391
391
|
|
392
392
|
## FAQ
|
393
393
|
|
394
|
-
#### Why
|
394
|
+
#### Why define enumerations outside the class that uses them?
|
395
395
|
|
396
396
|
* It's clearer.
|
397
397
|
* You can add behaviour to the enumeration class.
|
@@ -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.5+`
|
407
|
+
* **Rails** `5.0+`
|
408
408
|
|
409
409
|
All versions are tested via
|
410
410
|
[Travis](https://github.com/lucascaton/enumerate_it/blob/master/.travis.yml).
|
@@ -528,11 +528,11 @@ 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.
|
535
535
|
|
536
536
|
## Copyright
|
537
537
|
|
538
|
-
Copyright (c) 2010-
|
538
|
+
Copyright (c) 2010-2020 Cássio Marques and Lucas Caton. See `LICENSE` file for details.
|
data/Rakefile
CHANGED
@@ -1,14 +1,7 @@
|
|
1
1
|
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
2
3
|
|
3
|
-
|
4
|
-
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
6
|
|
6
|
-
|
7
|
-
RSpec::Core::RakeTask.new(:spec)
|
8
|
-
|
9
|
-
task default: :spec
|
10
|
-
else
|
11
|
-
require 'appraisal'
|
12
|
-
|
13
|
-
task default: :appraisal
|
14
|
-
end
|
7
|
+
task default: :spec
|
data/enumerate_it.gemspec
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH << File.expand_path('lib', __dir__)
|
2
|
+
require 'enumerate_it/version'
|
2
3
|
|
3
4
|
Gem::Specification.new do |gem|
|
4
5
|
gem.authors = ['Cássio Marques', 'Lucas Caton']
|
@@ -12,9 +13,14 @@ Gem::Specification.new do |gem|
|
|
12
13
|
gem.name = 'enumerate_it'
|
13
14
|
gem.require_paths = ['lib']
|
14
15
|
gem.version = EnumerateIt::VERSION
|
15
|
-
gem.required_ruby_version = '>= 2.
|
16
|
+
gem.required_ruby_version = '>= 2.5.8'
|
16
17
|
|
17
|
-
gem.
|
18
|
+
gem.metadata = {
|
19
|
+
'source_code_uri' => 'https://github.com/lucascaton/enumerate_it',
|
20
|
+
'changelog_uri' => 'https://github.com/lucascaton/enumerate_it/releases'
|
21
|
+
}
|
22
|
+
|
23
|
+
gem.add_dependency 'activesupport', '>= 5.0.7.2'
|
18
24
|
|
19
25
|
gem.add_development_dependency 'appraisal'
|
20
26
|
gem.add_development_dependency 'bundler'
|
@@ -22,6 +28,7 @@ Gem::Specification.new do |gem|
|
|
22
28
|
gem.add_development_dependency 'rake'
|
23
29
|
gem.add_development_dependency 'rspec'
|
24
30
|
gem.add_development_dependency 'rubocop'
|
31
|
+
gem.add_development_dependency 'rubocop-rake'
|
25
32
|
gem.add_development_dependency 'rubocop-rspec'
|
26
33
|
gem.add_development_dependency 'wwtd'
|
27
34
|
end
|
data/gemfiles/rails_5.1.gemfile
CHANGED
data/gemfiles/rails_5.2.gemfile
CHANGED
data/lib/enumerate_it/base.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
1
3
|
module EnumerateIt
|
2
4
|
class Base
|
3
5
|
class << self
|
6
|
+
extend Forwardable
|
7
|
+
|
4
8
|
attr_reader :sort_mode
|
5
9
|
|
10
|
+
def_delegators :enumeration, :keys, :each_key
|
11
|
+
|
6
12
|
def associate_values(*args)
|
7
13
|
values = values_hash(args)
|
8
14
|
|
@@ -21,6 +27,10 @@ module EnumerateIt
|
|
21
27
|
sorted_map.map { |_k, v| v.first }
|
22
28
|
end
|
23
29
|
|
30
|
+
def to_h
|
31
|
+
sorted_map.transform_values(&:first)
|
32
|
+
end
|
33
|
+
|
24
34
|
def enumeration
|
25
35
|
@registered_enumerations[self]
|
26
36
|
end
|
@@ -33,16 +43,16 @@ module EnumerateIt
|
|
33
43
|
list.length
|
34
44
|
end
|
35
45
|
|
36
|
-
def each_translation
|
37
|
-
each_value { |value|
|
46
|
+
def each_translation(&block)
|
47
|
+
each_value { |value| block.call t(value) }
|
38
48
|
end
|
39
49
|
|
40
50
|
def translations
|
41
51
|
list.map { |value| t(value) }
|
42
52
|
end
|
43
53
|
|
44
|
-
def each_value
|
45
|
-
list.each
|
54
|
+
def each_value(&block)
|
55
|
+
list.each(&block)
|
46
56
|
end
|
47
57
|
|
48
58
|
def to_json(options = nil)
|
@@ -70,10 +80,6 @@ module EnumerateIt
|
|
70
80
|
(enumeration[key.to_sym] || []).first
|
71
81
|
end
|
72
82
|
|
73
|
-
def keys
|
74
|
-
enumeration.keys
|
75
|
-
end
|
76
|
-
|
77
83
|
def key_for(value)
|
78
84
|
enumeration.map { |e| e[0] if e[1][0] == value }.compact.first
|
79
85
|
end
|
data/lib/enumerate_it/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe EnumerateIt::Base do
|
4
4
|
it 'creates constants for each enumeration value' do
|
@@ -85,6 +85,16 @@ describe EnumerateIt::Base do
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
+
describe '.each_key' do
|
89
|
+
it "yields each enumeration's key" do
|
90
|
+
keys = []
|
91
|
+
TestEnumeration.each_key do |key|
|
92
|
+
keys << key
|
93
|
+
end
|
94
|
+
expect(keys).to eq(%i[value_1 value_2 value_3])
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
88
98
|
describe '.each_value' do
|
89
99
|
it "yields each enumeration's value" do
|
90
100
|
values = []
|
@@ -113,6 +123,12 @@ describe EnumerateIt::Base do
|
|
113
123
|
end
|
114
124
|
end
|
115
125
|
|
126
|
+
describe '.to_h' do
|
127
|
+
it 'returns a hash' do
|
128
|
+
expect(TestEnumerationWithoutArray.to_h).to eq(value_one: '1', value_two: '2')
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
116
132
|
describe '.to_json' do
|
117
133
|
it 'gives a valid json back' do
|
118
134
|
I18n.locale = :inexsistent
|
@@ -246,12 +262,14 @@ describe EnumerateIt::Base do
|
|
246
262
|
end
|
247
263
|
|
248
264
|
context 'when included in ActiveRecord::Base' do
|
249
|
-
|
250
|
-
|
265
|
+
let(:active_record_stub_class) do
|
266
|
+
Class.new do
|
267
|
+
extend EnumerateIt
|
268
|
+
|
251
269
|
attr_accessor :bla
|
252
270
|
|
253
271
|
class << self
|
254
|
-
def validates_inclusion_of(_attribute)
|
272
|
+
def validates_inclusion_of(_attribute, _options)
|
255
273
|
true
|
256
274
|
end
|
257
275
|
|
@@ -260,42 +278,41 @@ describe EnumerateIt::Base do
|
|
260
278
|
end
|
261
279
|
end
|
262
280
|
end
|
263
|
-
|
264
|
-
allow(ActiveRecordStub).to receive(:validates_inclusion_of).and_return(true)
|
265
|
-
ActiveRecordStub.extend EnumerateIt
|
266
281
|
end
|
267
282
|
|
268
283
|
it 'creates a validation for inclusion' do
|
269
|
-
expect(
|
284
|
+
expect(active_record_stub_class)
|
270
285
|
.to receive(:validates_inclusion_of).with(:bla, in: TestEnumeration.list, allow_blank: true)
|
271
286
|
|
272
|
-
|
287
|
+
active_record_stub_class.class_eval do
|
273
288
|
has_enumeration_for :bla, with: TestEnumeration
|
274
289
|
end
|
275
290
|
end
|
276
291
|
|
277
292
|
context 'using the :required option' do
|
278
293
|
before do
|
279
|
-
allow(
|
294
|
+
allow(active_record_stub_class).to receive(:validates_presence_of).and_return(true)
|
280
295
|
end
|
281
296
|
|
282
297
|
it 'creates a validation for presence' do
|
283
|
-
expect(
|
284
|
-
|
298
|
+
expect(active_record_stub_class).to receive(:validates_presence_of)
|
299
|
+
active_record_stub_class.class_eval do
|
285
300
|
has_enumeration_for :bla, with: TestEnumeration, required: true
|
286
301
|
end
|
287
302
|
end
|
288
303
|
|
289
304
|
it 'passes the given options to the validation method' do
|
290
|
-
expect(
|
291
|
-
|
305
|
+
expect(active_record_stub_class)
|
306
|
+
.to receive(:validates_presence_of).with(:bla, if: :some_method)
|
307
|
+
|
308
|
+
active_record_stub_class.class_eval do
|
292
309
|
has_enumeration_for :bla, with: TestEnumeration, required: { if: :some_method }
|
293
310
|
end
|
294
311
|
end
|
295
312
|
|
296
313
|
it 'does not require the attribute by default' do
|
297
|
-
expect(
|
298
|
-
|
314
|
+
expect(active_record_stub_class).not_to receive(:validates_presence_of)
|
315
|
+
active_record_stub_class.class_eval do
|
299
316
|
has_enumeration_for :bla, with: TestEnumeration
|
300
317
|
end
|
301
318
|
end
|
@@ -303,15 +320,15 @@ describe EnumerateIt::Base do
|
|
303
320
|
|
304
321
|
context 'using :skip_validation option' do
|
305
322
|
it "doesn't create a validation for inclusion" do
|
306
|
-
expect(
|
307
|
-
|
323
|
+
expect(active_record_stub_class).not_to receive(:validates_inclusion_of)
|
324
|
+
active_record_stub_class.class_eval do
|
308
325
|
has_enumeration_for :bla, with: TestEnumeration, skip_validation: true
|
309
326
|
end
|
310
327
|
end
|
311
328
|
|
312
329
|
it "doesn't create a validation for presence" do
|
313
|
-
expect(
|
314
|
-
|
330
|
+
expect(active_record_stub_class).not_to receive(:validates_presence_of)
|
331
|
+
active_record_stub_class.class_eval do
|
315
332
|
has_enumeration_for :bla, with: TestEnumeration, require: true, skip_validation: true
|
316
333
|
end
|
317
334
|
end
|
data/spec/enumerate_it_spec.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require '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
9
|
has_enumeration_for :foobar, with: TestEnumeration
|
9
10
|
|
10
11
|
def initialize(foobar)
|
@@ -13,10 +14,10 @@ describe EnumerateIt do
|
|
13
14
|
|
14
15
|
I18n.locale = :en
|
15
16
|
end
|
16
|
-
|
17
|
-
TestClass.new(TestEnumeration::VALUE_2)
|
18
17
|
end
|
19
18
|
|
19
|
+
let(:target) { test_class.new(TestEnumeration::VALUE_2) }
|
20
|
+
|
20
21
|
context 'associating an enumeration with a class attribute' do
|
21
22
|
it "creates an humanized description for the attribute's value" do
|
22
23
|
expect(target.foobar_humanize).to eq('Hey, I am 2!')
|
@@ -32,17 +33,12 @@ describe EnumerateIt do
|
|
32
33
|
end
|
33
34
|
|
34
35
|
it 'stores the enumeration class in a class-level hash' do
|
35
|
-
expect(
|
36
|
+
expect(test_class.enumerations[:foobar]).to eq(TestEnumeration)
|
36
37
|
end
|
37
38
|
|
38
39
|
context 'use the same enumeration from an inherited class' do
|
39
|
-
let
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
SomeClassWithoutEnum.new
|
44
|
-
end
|
45
|
-
|
40
|
+
let(:some_class_without_enum) { Class.new(BaseClass) }
|
41
|
+
let(:target) { some_class_without_enum.new }
|
46
42
|
let(:base) { BaseClass.new }
|
47
43
|
|
48
44
|
it 'has use the correct class' do
|
@@ -52,14 +48,8 @@ describe EnumerateIt do
|
|
52
48
|
end
|
53
49
|
|
54
50
|
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
|
-
|
51
|
+
let(:some_class) { Class.new(BaseClass) { has_enumeration_for :foobar } }
|
52
|
+
let(:target) { some_class.new }
|
63
53
|
let(:base) { BaseClass.new }
|
64
54
|
|
65
55
|
it 'has use the corret class' do
|
@@ -69,18 +59,21 @@ describe EnumerateIt do
|
|
69
59
|
end
|
70
60
|
|
71
61
|
context 'passing options values without the human string (just the value, without an array)' do
|
72
|
-
let :
|
73
|
-
|
62
|
+
let :test_class_for_enumeration_without_array do
|
63
|
+
Class.new do
|
74
64
|
extend EnumerateIt
|
75
65
|
attr_accessor :foobar
|
66
|
+
|
76
67
|
has_enumeration_for :foobar, with: TestEnumerationWithoutArray
|
77
68
|
|
78
69
|
def initialize(foobar)
|
79
70
|
@foobar = foobar
|
80
71
|
end
|
81
72
|
end
|
73
|
+
end
|
82
74
|
|
83
|
-
|
75
|
+
let :target do
|
76
|
+
test_class_for_enumeration_without_array.new(TestEnumerationWithoutArray::VALUE_TWO)
|
84
77
|
end
|
85
78
|
|
86
79
|
it 'humanizes the respective hash key' do
|
@@ -97,37 +90,39 @@ describe EnumerateIt do
|
|
97
90
|
end
|
98
91
|
|
99
92
|
context 'without passing the enumeration class' do
|
100
|
-
let :
|
101
|
-
|
93
|
+
let :foo_bar_class do
|
94
|
+
Class.new do
|
102
95
|
extend EnumerateIt
|
103
96
|
attr_accessor :test_enumeration
|
97
|
+
|
104
98
|
has_enumeration_for :test_enumeration
|
105
99
|
|
106
100
|
def initialize(test_enumeration_value)
|
107
101
|
@test_enumeration = test_enumeration_value
|
108
102
|
end
|
109
103
|
end
|
110
|
-
|
111
|
-
FooBar.new(TestEnumeration::VALUE_1)
|
112
104
|
end
|
113
105
|
|
106
|
+
let(:target) { foo_bar_class.new(TestEnumeration::VALUE_1) }
|
107
|
+
|
114
108
|
it 'finds out which enumeration class to use' do
|
115
109
|
expect(target.test_enumeration_humanize).to eq('Hey, I am 1!')
|
116
110
|
end
|
117
111
|
|
118
112
|
context 'when using a nested class as the enumeration' do
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
class ClassWithNestedEnum
|
113
|
+
let :class_with_nested_enum do
|
114
|
+
Class.new do
|
115
|
+
# rubocop:disable Lint/ConstantDefinitionInBlock
|
116
|
+
# rubocop:disable RSpec/LeakyConstantDeclaration
|
125
117
|
class NestedEnum < EnumerateIt::Base
|
126
118
|
associate_values foo: %w[1 Blerrgh], bar: ['2' => 'Blarghhh']
|
127
119
|
end
|
120
|
+
# rubocop:enable RSpec/LeakyConstantDeclaration
|
121
|
+
# rubocop:enable Lint/ConstantDefinitionInBlock
|
128
122
|
|
129
123
|
extend EnumerateIt
|
130
124
|
attr_accessor :nested_enum
|
125
|
+
|
131
126
|
has_enumeration_for :nested_enum
|
132
127
|
|
133
128
|
def initialize(nested_enum_value)
|
@@ -137,17 +132,18 @@ describe EnumerateIt do
|
|
137
132
|
end
|
138
133
|
|
139
134
|
it 'uses the inner class as the enumeration class' do
|
140
|
-
expect(
|
135
|
+
expect(class_with_nested_enum.new('1').nested_enum_humanize).to eq('Blerrgh')
|
141
136
|
end
|
142
137
|
end
|
143
138
|
end
|
144
139
|
end
|
145
140
|
|
146
141
|
context 'using the :create_helpers option' do
|
147
|
-
|
148
|
-
|
142
|
+
let :test_class_with_helper do
|
143
|
+
Class.new do
|
149
144
|
extend EnumerateIt
|
150
145
|
attr_accessor :foobar
|
146
|
+
|
151
147
|
has_enumeration_for :foobar, with: TestEnumeration, create_helpers: true
|
152
148
|
|
153
149
|
def initialize(foobar)
|
@@ -157,60 +153,87 @@ describe EnumerateIt do
|
|
157
153
|
end
|
158
154
|
|
159
155
|
it 'creates helpers methods with question marks for each enumeration option' do
|
160
|
-
target =
|
156
|
+
target = test_class_with_helper.new(TestEnumeration::VALUE_2)
|
161
157
|
expect(target).to be_value_2
|
162
158
|
expect(target).not_to be_value_1
|
163
159
|
end
|
164
160
|
|
165
161
|
it 'creates a mutator method for each enumeration value' do
|
166
162
|
%i[value_1 value_2 value_3].each do |value|
|
167
|
-
expect(
|
163
|
+
expect(test_class_with_helper.new(TestEnumeration::VALUE_1)).to respond_to(:"#{value}!")
|
168
164
|
end
|
169
165
|
end
|
170
166
|
|
171
167
|
it "changes the attribute's value through mutator methods" do
|
172
|
-
target =
|
168
|
+
target = test_class_with_helper.new(TestEnumeration::VALUE_2)
|
173
169
|
target.value_3!
|
174
170
|
expect(target.foobar).to eq(TestEnumeration::VALUE_3)
|
175
171
|
end
|
176
172
|
|
173
|
+
context 'when class responds to save! method' do
|
174
|
+
it 'calls save!' do
|
175
|
+
target = test_class_with_helper.new(TestEnumeration::VALUE_2)
|
176
|
+
allow(target).to receive(:save!)
|
177
|
+
target.value_3!
|
178
|
+
expect(target).to have_received(:save!)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
177
182
|
context 'with :prefix option' do
|
178
|
-
|
179
|
-
|
183
|
+
let :test_class_with_prefixed_helper do
|
184
|
+
Class.new do
|
185
|
+
extend EnumerateIt
|
186
|
+
attr_accessor :foobar
|
187
|
+
|
180
188
|
has_enumeration_for :foobar, with: TestEnumeration, create_helpers: { prefix: true }
|
189
|
+
|
190
|
+
def initialize(foobar)
|
191
|
+
@foobar = foobar
|
192
|
+
end
|
181
193
|
end
|
182
194
|
end
|
183
195
|
|
184
196
|
it 'creates helpers methods with question marks and prefixes for each enumeration option' do
|
185
|
-
target =
|
197
|
+
target = test_class_with_prefixed_helper.new(TestEnumeration::VALUE_2)
|
186
198
|
expect(target).to be_foobar_value_2
|
187
199
|
end
|
188
200
|
|
189
201
|
it 'creates a mutator method for each enumeration value' do
|
190
202
|
%i[value_1 value_2 value_3].each do |value|
|
191
|
-
expect(
|
203
|
+
expect(test_class_with_prefixed_helper.new(TestEnumeration::VALUE_1))
|
192
204
|
.to respond_to(:"foobar_#{value}!")
|
193
205
|
end
|
194
206
|
end
|
195
207
|
|
196
208
|
it "changes the attribute's value through mutator methods" do
|
197
|
-
target =
|
209
|
+
target = test_class_with_prefixed_helper.new(TestEnumeration::VALUE_2)
|
198
210
|
target.foobar_value_3!
|
199
211
|
expect(target.foobar).to eq(TestEnumeration::VALUE_3)
|
200
212
|
end
|
213
|
+
|
214
|
+
context 'when class responds to save! method' do
|
215
|
+
it 'calls save!' do
|
216
|
+
target = test_class_with_prefixed_helper.new(TestEnumeration::VALUE_2)
|
217
|
+
allow(target).to receive(:save!)
|
218
|
+
target.foobar_value_3!
|
219
|
+
expect(target).to have_received(:save!)
|
220
|
+
end
|
221
|
+
end
|
201
222
|
end
|
202
223
|
|
203
224
|
context 'with :polymorphic option' do
|
204
|
-
|
205
|
-
|
225
|
+
let :polymorphic_class do
|
226
|
+
Class.new do
|
206
227
|
extend EnumerateIt
|
207
228
|
attr_accessor :foo
|
229
|
+
|
208
230
|
has_enumeration_for :foo, with: PolymorphicEnum, create_helpers: { polymorphic: true }
|
209
231
|
end
|
210
232
|
end
|
211
233
|
|
234
|
+
let(:target) { polymorphic_class.new }
|
235
|
+
|
212
236
|
it "calls methods on the enum constants' objects" do
|
213
|
-
target = Polymorphic.new
|
214
237
|
target.foo = PolymorphicEnum::NORMAL
|
215
238
|
|
216
239
|
expect(target.foo_object.print('Gol')).to eq("I'm Normal: Gol")
|
@@ -221,21 +244,23 @@ describe EnumerateIt do
|
|
221
244
|
end
|
222
245
|
|
223
246
|
it 'returns nil if foo is not set' do
|
224
|
-
target = Polymorphic.new
|
225
|
-
|
226
247
|
expect(target.foo_object).to be_nil
|
227
248
|
end
|
228
249
|
|
229
250
|
context 'and :suffix' do
|
230
|
-
|
231
|
-
|
251
|
+
let :polymorphic_class_with_suffix do
|
252
|
+
Class.new do
|
253
|
+
extend EnumerateIt
|
254
|
+
attr_accessor :foo
|
255
|
+
|
232
256
|
has_enumeration_for :foo, with: PolymorphicEnum,
|
233
257
|
create_helpers: { polymorphic: { suffix: '_strategy' } }
|
234
258
|
end
|
235
259
|
end
|
236
260
|
|
261
|
+
let(:target) { polymorphic_class_with_suffix.new }
|
262
|
+
|
237
263
|
it "calls methods on the enum constants' objects" do
|
238
|
-
target = Polymorphic.new
|
239
264
|
target.foo = PolymorphicEnum::NORMAL
|
240
265
|
|
241
266
|
expect(target.foo_strategy.print('Gol')).to eq("I'm Normal: Gol")
|
@@ -250,17 +275,16 @@ describe EnumerateIt do
|
|
250
275
|
|
251
276
|
describe 'using the :create_scopes option' do
|
252
277
|
context 'if the hosting class responds to :scope' do
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
class TestClassWithScope < ActiveRecord::Base
|
278
|
+
let :test_class_with_scope do
|
279
|
+
Class.new(ActiveRecord::Base) do
|
280
|
+
self.table_name = 'test_class_with_scopes'
|
257
281
|
has_enumeration_for :foobar, with: TestEnumeration, create_scopes: true
|
258
282
|
end
|
259
283
|
end
|
260
284
|
|
261
285
|
it 'creates a scope for each enumeration value' do
|
262
286
|
TestEnumeration.enumeration.each_key do |symbol|
|
263
|
-
expect(
|
287
|
+
expect(test_class_with_scope).to respond_to(symbol)
|
264
288
|
end
|
265
289
|
end
|
266
290
|
|
@@ -268,29 +292,25 @@ describe EnumerateIt do
|
|
268
292
|
ActiveRecord::Schema.define { create_table :test_class_with_scopes }
|
269
293
|
|
270
294
|
TestEnumeration.enumeration.each do |symbol, pair|
|
271
|
-
expect(
|
272
|
-
.to match(/WHERE "test_class_with_scopes"."foobar" =
|
295
|
+
expect(test_class_with_scope.public_send(symbol).to_sql)
|
296
|
+
.to match(/WHERE "test_class_with_scopes"."foobar" = '#{pair.first}'/)
|
273
297
|
end
|
274
298
|
end
|
275
299
|
end
|
276
300
|
|
277
301
|
context 'when the hosting class does not respond to :scope' do
|
278
|
-
|
279
|
-
class GenericClass
|
280
|
-
extend EnumerateIt
|
281
|
-
end
|
282
|
-
end
|
302
|
+
let(:generic_class) { Class.new { extend EnumerateIt } }
|
283
303
|
|
284
304
|
it 'raises no errors' do
|
285
305
|
expect do
|
286
|
-
|
306
|
+
generic_class.has_enumeration_for(:foobar, with: TestEnumeration, create_scopes: true)
|
287
307
|
end.not_to raise_error
|
288
308
|
end
|
289
309
|
end
|
290
310
|
|
291
311
|
context 'with :prefix option' do
|
292
|
-
|
293
|
-
|
312
|
+
let :other_test_class do
|
313
|
+
Class.new(ActiveRecord::Base) do
|
294
314
|
has_enumeration_for :foobar, with: TestEnumerationWithReservedWords,
|
295
315
|
create_scopes: { prefix: true }
|
296
316
|
end
|
@@ -298,7 +318,7 @@ describe EnumerateIt do
|
|
298
318
|
|
299
319
|
it 'creates a scope with prefix for each enumeration value' do
|
300
320
|
TestEnumerationWithReservedWords.enumeration.each_key do |symbol|
|
301
|
-
expect(
|
321
|
+
expect(other_test_class).to respond_to(:"foobar_#{symbol}")
|
302
322
|
end
|
303
323
|
end
|
304
324
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,7 +5,7 @@ require 'enumerate_it'
|
|
5
5
|
require 'active_support/all'
|
6
6
|
require 'active_record'
|
7
7
|
|
8
|
-
Dir['./spec/support/**/*.rb'].each { |f| require f }
|
8
|
+
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
|
9
9
|
|
10
10
|
I18n.config.enforce_available_locales = false
|
11
11
|
I18n.load_path = Dir['spec/i18n/*.yml']
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerate_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cássio Marques
|
8
8
|
- Lucas Caton
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2020-12-07 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:
|
20
|
+
version: 5.0.7.2
|
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:
|
27
|
+
version: 5.0.7.2
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: appraisal
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -109,6 +109,20 @@ dependencies:
|
|
109
109
|
- - ">="
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: rubocop-rake
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
112
126
|
- !ruby/object:Gem::Dependency
|
113
127
|
name: rubocop-rspec
|
114
128
|
requirement: !ruby/object:Gem::Requirement
|
@@ -138,12 +152,11 @@ dependencies:
|
|
138
152
|
- !ruby/object:Gem::Version
|
139
153
|
version: '0'
|
140
154
|
description: Enumerations for Ruby with some magic powers!
|
141
|
-
email:
|
155
|
+
email:
|
142
156
|
executables: []
|
143
157
|
extensions: []
|
144
158
|
extra_rdoc_files: []
|
145
159
|
files:
|
146
|
-
- ".ci/prepare"
|
147
160
|
- ".gitignore"
|
148
161
|
- ".rspec"
|
149
162
|
- ".rubocop.yml"
|
@@ -156,12 +169,10 @@ files:
|
|
156
169
|
- README.md
|
157
170
|
- Rakefile
|
158
171
|
- enumerate_it.gemspec
|
159
|
-
- gemfiles/rails_4.0.gemfile
|
160
|
-
- gemfiles/rails_4.1.gemfile
|
161
|
-
- gemfiles/rails_4.2.gemfile
|
162
172
|
- gemfiles/rails_5.0.gemfile
|
163
173
|
- gemfiles/rails_5.1.gemfile
|
164
174
|
- gemfiles/rails_5.2.gemfile
|
175
|
+
- gemfiles/rails_6.0.gemfile
|
165
176
|
- lib/enumerate_it.rb
|
166
177
|
- lib/enumerate_it/base.rb
|
167
178
|
- lib/enumerate_it/class_methods.rb
|
@@ -179,8 +190,10 @@ files:
|
|
179
190
|
homepage: https://github.com/lucascaton/enumerate_it
|
180
191
|
licenses:
|
181
192
|
- MIT
|
182
|
-
metadata:
|
183
|
-
|
193
|
+
metadata:
|
194
|
+
source_code_uri: https://github.com/lucascaton/enumerate_it
|
195
|
+
changelog_uri: https://github.com/lucascaton/enumerate_it/releases
|
196
|
+
post_install_message:
|
184
197
|
rdoc_options: []
|
185
198
|
require_paths:
|
186
199
|
- lib
|
@@ -188,15 +201,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
188
201
|
requirements:
|
189
202
|
- - ">="
|
190
203
|
- !ruby/object:Gem::Version
|
191
|
-
version: 2.
|
204
|
+
version: 2.5.8
|
192
205
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
193
206
|
requirements:
|
194
207
|
- - ">="
|
195
208
|
- !ruby/object:Gem::Version
|
196
209
|
version: '0'
|
197
210
|
requirements: []
|
198
|
-
rubygems_version: 3.
|
199
|
-
signing_key:
|
211
|
+
rubygems_version: 3.1.4
|
212
|
+
signing_key:
|
200
213
|
specification_version: 4
|
201
214
|
summary: Ruby Enumerations
|
202
215
|
test_files:
|
data/.ci/prepare
DELETED
data/gemfiles/rails_4.0.gemfile
DELETED
data/gemfiles/rails_4.1.gemfile
DELETED