enumerate_it 1.3.0 → 1.3.1.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +40 -0
- data/.travis.yml +23 -8
- data/Appraisals +11 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +33 -31
- data/README.md +7 -12
- data/Rakefile +12 -8
- data/enumerate_it.gemspec +6 -2
- data/gemfiles/{activesupport_4_0.gemfile → rails_3.0.gemfile} +2 -2
- data/gemfiles/{activesupport_3_0.gemfile → rails_3.1.gemfile} +2 -2
- data/gemfiles/{activesupport_3_1.gemfile → rails_3.2.gemfile} +2 -2
- data/gemfiles/{activesupport_3_2.gemfile → rails_4.0.gemfile} +2 -2
- data/gemfiles/rails_4.1.gemfile +8 -0
- data/gemfiles/rails_4.2.gemfile +8 -0
- data/gemfiles/rails_5.0.gemfile +8 -0
- data/lib/enumerate_it/base.rb +98 -86
- data/lib/enumerate_it/class_methods.rb +33 -25
- data/lib/enumerate_it/version.rb +1 -1
- data/lib/generators/enumerate_it/enum/enum_generator.rb +5 -7
- data/lib/generators/enumerate_it/install/install_generator.rb +1 -1
- data/spec/enumerate_it/base_spec.rb +100 -86
- data/spec/enumerate_it_spec.rb +77 -73
- data/spec/spec_helper.rb +1 -1
- data/spec/support/test_classes.rb +4 -4
- metadata +27 -12
- data/.document +0 -5
- data/gemfiles/activesupport_4_1.gemfile +0 -8
- data/gemfiles/activesupport_4_2.gemfile +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa84a89fcca8d6194d41aaf25a1b08ae33f0f61d
|
4
|
+
data.tar.gz: 3fa95bf16fe87f5fb99b21667747a21d8d48c9fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78fdd11e5fbaf213ae694925d9014127c24037b77771a66e997a61907a7d207ae9ac0b72170fe904087747f10c8498aaf013d86c34fff16fc5884e676c1cb3e0
|
7
|
+
data.tar.gz: 2ad8706ed2aee354ee17e3cadb24b5c74e6ac5bf429c858da80e075c7b06f01672ea3cf706683e6efdb093eb86900e217a748886d204b9ac38bebc2cda945e53
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
DisplayCopNames: true
|
5
|
+
|
6
|
+
Include:
|
7
|
+
- '**/Appraisals'
|
8
|
+
- '**/Gemfile'
|
9
|
+
- '**/Rakefile'
|
10
|
+
Exclude:
|
11
|
+
- 'lib/generators/enumerate_it/enum/templates/**/*'
|
12
|
+
- 'gemfiles/vendor/**/*'
|
13
|
+
|
14
|
+
Lint/EndAlignment:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Metrics/LineLength:
|
18
|
+
Max: 100
|
19
|
+
|
20
|
+
Style/AlignHash:
|
21
|
+
EnforcedLastArgumentHashStyle: ignore_implicit
|
22
|
+
|
23
|
+
Style/Documentation:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Style/ElseAlignment:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/FrozenStringLiteralComment:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/IndentationWidth:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/MultilineMethodCallIndentation:
|
36
|
+
EnforcedStyle: indented
|
37
|
+
|
38
|
+
Style/PredicateName:
|
39
|
+
Exclude:
|
40
|
+
- 'lib/enumerate_it/class_methods.rb'
|
data/.travis.yml
CHANGED
@@ -4,17 +4,32 @@ sudo: false
|
|
4
4
|
|
5
5
|
cache: bundler
|
6
6
|
|
7
|
+
script:
|
8
|
+
- "ruby -e \"RUBY_VERSION == '2.3.1' ? system('bundle exec rubocop') : exit(0)\""
|
9
|
+
- bundle exec rake spec
|
10
|
+
|
7
11
|
rvm:
|
8
12
|
- 1.9.3
|
9
13
|
- 2.0.0
|
10
|
-
- 2.1.
|
11
|
-
- 2.2.
|
14
|
+
- 2.1.10
|
15
|
+
- 2.2.5
|
12
16
|
- 2.3.1
|
13
17
|
|
14
18
|
gemfile:
|
15
|
-
- gemfiles/
|
16
|
-
- gemfiles/
|
17
|
-
- gemfiles/
|
18
|
-
- gemfiles/
|
19
|
-
- gemfiles/
|
20
|
-
- gemfiles/
|
19
|
+
- gemfiles/rails_3.0.gemfile
|
20
|
+
- gemfiles/rails_3.1.gemfile
|
21
|
+
- gemfiles/rails_3.2.gemfile
|
22
|
+
- gemfiles/rails_4.0.gemfile
|
23
|
+
- gemfiles/rails_4.1.gemfile
|
24
|
+
- gemfiles/rails_4.2.gemfile
|
25
|
+
- gemfiles/rails_5.0.gemfile
|
26
|
+
|
27
|
+
matrix:
|
28
|
+
# Rails 5 requires Ruby version 2.2+
|
29
|
+
exclude:
|
30
|
+
- rvm: 1.9.3
|
31
|
+
gemfile: gemfiles/rails_5.0.gemfile
|
32
|
+
- rvm: 2.0.0
|
33
|
+
gemfile: gemfiles/rails_5.0.gemfile
|
34
|
+
- rvm: 2.1.10
|
35
|
+
gemfile: gemfiles/rails_5.0.gemfile
|
data/Appraisals
CHANGED
@@ -1,6 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
rails_versions = JSON.parse(Net::HTTP.get(URI('https://rubygems.org/api/v1/versions/rails.json')))
|
4
|
+
.group_by { |version| version['number'] }.keys.select { |key| key !~ /rc|racecar|beta|pre/ }
|
5
|
+
|
6
|
+
%w(3.0 3.1 3.2 4.0 4.1 4.2 5.0).each do |version|
|
7
|
+
appraise "rails_#{version}" do
|
8
|
+
current_version = rails_versions.select { |key| key.match(/\A#{version}/) }.max
|
9
|
+
|
10
|
+
gem 'activesupport', "~> #{current_version}"
|
11
|
+
gem 'activerecord', "~> #{current_version}"
|
5
12
|
end
|
6
13
|
end
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,64 +1,65 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enumerate_it (1.3.
|
4
|
+
enumerate_it (1.3.1.rc1)
|
5
5
|
activesupport (>= 3.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (
|
10
|
+
activesupport (5.0.0.1)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
11
12
|
i18n (~> 0.7)
|
12
|
-
json (~> 1.7, >= 1.7.7)
|
13
13
|
minitest (~> 5.1)
|
14
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
15
14
|
tzinfo (~> 1.1)
|
16
15
|
appraisal (2.1.0)
|
17
16
|
bundler
|
18
17
|
rake
|
19
18
|
thor (>= 0.14.0)
|
20
|
-
ast (2.
|
21
|
-
coderay (1.
|
19
|
+
ast (2.3.0)
|
20
|
+
coderay (1.1.1)
|
21
|
+
concurrent-ruby (1.0.2)
|
22
22
|
diff-lcs (1.2.5)
|
23
23
|
i18n (0.7.0)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
parser (2.3.0.7)
|
24
|
+
method_source (0.8.2)
|
25
|
+
minitest (5.9.0)
|
26
|
+
parser (2.3.1.2)
|
28
27
|
ast (~> 2.2)
|
29
28
|
powerpack (0.1.1)
|
30
|
-
pry (0.
|
31
|
-
coderay (~> 1.0
|
32
|
-
method_source (~> 0.8)
|
29
|
+
pry (0.10.4)
|
30
|
+
coderay (~> 1.1.0)
|
31
|
+
method_source (~> 0.8.1)
|
33
32
|
slop (~> 3.4)
|
34
33
|
rainbow (2.1.0)
|
35
|
-
rake (11.
|
36
|
-
rspec (3.
|
37
|
-
rspec-core (~> 3.
|
38
|
-
rspec-expectations (~> 3.
|
39
|
-
rspec-mocks (~> 3.
|
40
|
-
rspec-core (3.
|
41
|
-
rspec-support (~> 3.
|
42
|
-
rspec-expectations (3.
|
34
|
+
rake (11.2.2)
|
35
|
+
rspec (3.5.0)
|
36
|
+
rspec-core (~> 3.5.0)
|
37
|
+
rspec-expectations (~> 3.5.0)
|
38
|
+
rspec-mocks (~> 3.5.0)
|
39
|
+
rspec-core (3.5.2)
|
40
|
+
rspec-support (~> 3.5.0)
|
41
|
+
rspec-expectations (3.5.0)
|
43
42
|
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
-
rspec-support (~> 3.
|
45
|
-
rspec-mocks (3.
|
43
|
+
rspec-support (~> 3.5.0)
|
44
|
+
rspec-mocks (3.5.0)
|
46
45
|
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
-
rspec-support (~> 3.
|
48
|
-
rspec-support (3.
|
49
|
-
rubocop (0.
|
50
|
-
parser (>= 2.3.
|
46
|
+
rspec-support (~> 3.5.0)
|
47
|
+
rspec-support (3.5.0)
|
48
|
+
rubocop (0.42.0)
|
49
|
+
parser (>= 2.3.1.1, < 3.0)
|
51
50
|
powerpack (~> 0.1)
|
52
51
|
rainbow (>= 1.99.1, < 3.0)
|
53
52
|
ruby-progressbar (~> 1.7)
|
54
53
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
55
|
-
|
56
|
-
|
54
|
+
rubocop-rspec (1.6.0)
|
55
|
+
rubocop (>= 0.42.0)
|
56
|
+
ruby-progressbar (1.8.1)
|
57
|
+
slop (3.6.0)
|
57
58
|
thor (0.19.1)
|
58
59
|
thread_safe (0.3.5)
|
59
60
|
tzinfo (1.2.2)
|
60
61
|
thread_safe (~> 0.1)
|
61
|
-
unicode-display_width (1.0
|
62
|
+
unicode-display_width (1.1.0)
|
62
63
|
|
63
64
|
PLATFORMS
|
64
65
|
ruby
|
@@ -70,6 +71,7 @@ DEPENDENCIES
|
|
70
71
|
rake
|
71
72
|
rspec
|
72
73
|
rubocop
|
74
|
+
rubocop-rspec
|
73
75
|
|
74
76
|
BUNDLED WITH
|
75
|
-
1.
|
77
|
+
1.12.5
|
data/README.md
CHANGED
@@ -51,9 +51,7 @@ Changes are maintained under [Releases](https://github.com/lucascaton/enumerate_
|
|
51
51
|
|
52
52
|
## Creating enumerations
|
53
53
|
|
54
|
-
Enumerations are created as
|
55
|
-
application. In Rails applications, you should put them inside
|
56
|
-
`app/enumerations` folder.
|
54
|
+
Enumerations are created as classes and you should put them inside `app/enumerations` folder.
|
57
55
|
|
58
56
|
```ruby
|
59
57
|
class RelationshipStatus < EnumerateIt::Base
|
@@ -187,13 +185,10 @@ end
|
|
187
185
|
|
188
186
|
The `sort_by` methods accept one of the following values:
|
189
187
|
|
190
|
-
*
|
191
|
-
|
192
|
-
*
|
193
|
-
*
|
194
|
-
enumeration option.
|
195
|
-
* `:none`: Will return values in order that was passed to associate_values
|
196
|
-
call.
|
188
|
+
* `:translation`: The default behavior, will sort the returned values based on translations.
|
189
|
+
* `:value`: Will sort the returned values based on values.
|
190
|
+
* `:name`: Will sort the returned values based on the name of each enumeration option.
|
191
|
+
* `:none`: Will return values in order that was passed to associate_values call.
|
197
192
|
|
198
193
|
## Using enumerations
|
199
194
|
|
@@ -494,7 +489,7 @@ their locale files. Take a look at how to use it running:
|
|
494
489
|
rails generate enumerate_it:enum --help
|
495
490
|
```
|
496
491
|
|
497
|
-
## Supported Ruby and Rails
|
492
|
+
## Supported Ruby and Rails versions
|
498
493
|
|
499
494
|
Check [travis config file](https://github.com/lucascaton/enumerate_it/blob/master/.travis.yml).
|
500
495
|
|
@@ -516,7 +511,7 @@ codes. I had both situations in my legacy database.
|
|
516
511
|
* Make your feature addition or bug fix.
|
517
512
|
* Add tests for it. This is important so I don't break it in a future
|
518
513
|
version unintentionally.
|
519
|
-
* Run the tests agaist all supported versions: `$
|
514
|
+
* Run the tests agaist all supported versions: `$ rake`.
|
520
515
|
* Commit, do not mess with Rakefile, version, or history. (if you want to
|
521
516
|
have your own version, that is fine but bump version in a commit by itself
|
522
517
|
I can ignore when I pull)
|
data/Rakefile
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require 'bundler'
|
4
|
-
Bundler::GemHelper.install_tasks
|
3
|
+
require 'bundler/gem_tasks'
|
5
4
|
|
6
|
-
|
7
|
-
require 'rspec/core/rake_task'
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
if ENV['APPRAISAL_INITIALIZED'] || ENV['TRAVIS'] || RUBY_VERSION < '2.0.0'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
Bundler::GemHelper.install_tasks
|
9
|
+
RSpec::Core::RakeTask.new(:spec)
|
11
10
|
|
12
|
-
task default: :spec
|
11
|
+
task default: :spec
|
12
|
+
else
|
13
|
+
require 'appraisal'
|
14
|
+
|
15
|
+
task default: :appraisal
|
16
|
+
end
|
data/enumerate_it.gemspec
CHANGED
@@ -2,8 +2,8 @@ require File.expand_path('../lib/enumerate_it/version', __FILE__)
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
4
|
gem.authors = ['Cássio Marques', 'Lucas Caton']
|
5
|
-
gem.description = 'Enumerations for Ruby with some magic powers!'
|
6
5
|
gem.summary = 'Ruby Enumerations'
|
6
|
+
gem.description = 'Enumerations for Ruby with some magic powers!'
|
7
7
|
gem.homepage = 'https://github.com/lucascaton/enumerate_it'
|
8
8
|
|
9
9
|
gem.files = `git ls-files`.split("\n")
|
@@ -18,5 +18,9 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.add_development_dependency 'pry'
|
19
19
|
gem.add_development_dependency 'rake'
|
20
20
|
gem.add_development_dependency 'rspec'
|
21
|
-
|
21
|
+
|
22
|
+
if RUBY_VERSION >= '2.2.0'
|
23
|
+
gem.add_development_dependency 'rubocop'
|
24
|
+
gem.add_development_dependency 'rubocop-rspec'
|
25
|
+
end
|
22
26
|
end
|
data/lib/enumerate_it/base.rb
CHANGED
@@ -1,122 +1,134 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
module EnumerateIt
|
3
4
|
class Base
|
4
|
-
@@registered_enumerations = {}
|
5
|
-
|
6
5
|
class << self
|
7
6
|
attr_reader :sort_mode
|
8
|
-
end
|
9
7
|
|
10
|
-
|
11
|
-
|
8
|
+
def associate_values(*args)
|
9
|
+
values = values_hash(args)
|
12
10
|
|
13
|
-
|
14
|
-
values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] }
|
15
|
-
end
|
11
|
+
register_enumeration(normalize_enumeration(values))
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
values.each_pair do |value_name, attributes|
|
14
|
+
define_enumeration_constant value_name, attributes[0]
|
15
|
+
end
|
16
|
+
end
|
20
17
|
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
def sort_by(sort_mode)
|
19
|
+
@sort_mode = sort_mode
|
20
|
+
end
|
24
21
|
|
25
|
-
|
26
|
-
|
27
|
-
|
22
|
+
def list
|
23
|
+
sorted_map.map { |_k, v| v.first }
|
24
|
+
end
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
def enumeration
|
27
|
+
@registered_enumerations[self]
|
28
|
+
end
|
32
29
|
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
def to_a
|
31
|
+
sorted_map.map { |_k, v| [translate(v[1]), v[0]] }
|
32
|
+
end
|
36
33
|
|
37
|
-
|
38
|
-
|
39
|
-
|
34
|
+
def length
|
35
|
+
list.length
|
36
|
+
end
|
40
37
|
|
41
|
-
|
42
|
-
|
43
|
-
|
38
|
+
def each_translation
|
39
|
+
each_value { |value| yield t(value) }
|
40
|
+
end
|
44
41
|
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
def translations
|
43
|
+
list.map { |value| t(value) }
|
44
|
+
end
|
48
45
|
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
def each_value
|
47
|
+
list.each { |value| yield value }
|
48
|
+
end
|
52
49
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
50
|
+
def to_json
|
51
|
+
sorted_map.map { |_k, v| { value: v[0], label: translate(v[1]) } }.to_json
|
52
|
+
end
|
57
53
|
|
58
|
-
|
59
|
-
|
60
|
-
|
54
|
+
def t(value)
|
55
|
+
target = to_a.detect { |item| item[1] == value }
|
56
|
+
target ? target[0] : value
|
57
|
+
end
|
61
58
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
nil
|
66
|
-
end
|
59
|
+
def values_for(values)
|
60
|
+
values.map { |v| value_for v.to_sym }
|
61
|
+
end
|
67
62
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
63
|
+
def value_for(value)
|
64
|
+
const_get(value.to_sym)
|
65
|
+
rescue NameError
|
66
|
+
nil
|
67
|
+
end
|
72
68
|
|
73
|
-
|
74
|
-
|
75
|
-
|
69
|
+
def value_from_key(key)
|
70
|
+
return if key.nil?
|
71
|
+
(enumeration[key.to_sym] || []).first
|
72
|
+
end
|
76
73
|
|
77
|
-
|
78
|
-
|
79
|
-
|
74
|
+
def keys
|
75
|
+
enumeration.keys
|
76
|
+
end
|
80
77
|
|
81
|
-
|
82
|
-
|
83
|
-
|
78
|
+
def key_for(value)
|
79
|
+
enumeration.map { |e| e[0] if e[1][0] == value }.compact.first
|
80
|
+
end
|
84
81
|
|
85
|
-
|
82
|
+
def to_range
|
83
|
+
(list.min..list.max)
|
84
|
+
end
|
86
85
|
|
87
|
-
|
88
|
-
|
86
|
+
def translate(value)
|
87
|
+
return value unless value.is_a? Symbol
|
89
88
|
|
90
|
-
|
91
|
-
|
89
|
+
default = value.to_s.tr('_', ' ').split.map(&:capitalize).join(' ')
|
90
|
+
I18n.t("enumerations.#{name.underscore}.#{value.to_s.underscore}", default: default)
|
91
|
+
end
|
92
92
|
|
93
|
-
|
94
|
-
{
|
95
|
-
value: lambda { |_k, v| v[0] },
|
96
|
-
name: lambda { |k, _v| k },
|
97
|
-
translation: lambda { |_k, v| translate(v[1]) }
|
98
|
-
}[sort_mode || :translation]
|
99
|
-
end
|
93
|
+
private
|
100
94
|
|
101
|
-
|
102
|
-
|
95
|
+
def sorted_map
|
96
|
+
return enumeration if sort_mode == :none
|
103
97
|
|
104
|
-
|
105
|
-
|
106
|
-
end
|
98
|
+
enumeration.sort_by { |k, v| sort_lambda.call(k, v) }
|
99
|
+
end
|
107
100
|
|
108
|
-
|
109
|
-
|
110
|
-
|
101
|
+
def sort_lambda
|
102
|
+
{
|
103
|
+
value: ->(_k, v) { v[0] },
|
104
|
+
name: ->(k, _v) { k },
|
105
|
+
translation: ->(_k, v) { translate(v[1]) }
|
106
|
+
}[sort_mode || :translation]
|
111
107
|
end
|
112
|
-
end
|
113
108
|
|
114
|
-
|
115
|
-
|
116
|
-
|
109
|
+
def normalize_enumeration(values_hash)
|
110
|
+
values_hash.each_pair do |key, value|
|
111
|
+
values_hash[key] = [value, key] unless value.is_a? Array
|
112
|
+
end
|
113
|
+
end
|
117
114
|
|
118
|
-
|
119
|
-
|
115
|
+
def register_enumeration(values_hash)
|
116
|
+
@registered_enumerations ||= {}
|
117
|
+
@registered_enumerations[self] = values_hash
|
118
|
+
end
|
119
|
+
|
120
|
+
def define_enumeration_constant(name, value)
|
121
|
+
const_set name.to_s.tr('-', '_').upcase, value
|
122
|
+
end
|
123
|
+
|
124
|
+
def values_hash(args)
|
125
|
+
return args.first if args.first.is_a?(Hash)
|
126
|
+
|
127
|
+
args.each_with_object({}) do |value, hash|
|
128
|
+
hash[value] = value.to_s
|
129
|
+
hash
|
130
|
+
end
|
131
|
+
end
|
120
132
|
end
|
121
133
|
end
|
122
134
|
end
|