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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eeb5e0de6df61f17293c7d0c7b5d833c5d3738b3
4
- data.tar.gz: 31978fd7c1d78bbcf499ecf4c6d66f3101add5a1
3
+ metadata.gz: fa84a89fcca8d6194d41aaf25a1b08ae33f0f61d
4
+ data.tar.gz: 3fa95bf16fe87f5fb99b21667747a21d8d48c9fc
5
5
  SHA512:
6
- metadata.gz: 910befeeabdbd3647ca2b1c7c9115cee8e8bd927fd938dac4704a03a6b0cf077524e17a604edf2fb31d5493b6d4aec6819cfbec0e46b51d16a633b0974903af2
7
- data.tar.gz: cf9b34ec103040023ac449a9fecffad45a3bca529f3ae041683838fbd25f23f48cce1dda40c5aeae9ccddc7388aa3a9058353b1e985d533a339b09a12105854e
6
+ metadata.gz: 78fdd11e5fbaf213ae694925d9014127c24037b77771a66e997a61907a7d207ae9ac0b72170fe904087747f10c8498aaf013d86c34fff16fc5884e676c1cb3e0
7
+ data.tar.gz: 2ad8706ed2aee354ee17e3cadb24b5c74e6ac5bf429c858da80e075c7b06f01672ea3cf706683e6efdb093eb86900e217a748886d204b9ac38bebc2cda945e53
@@ -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'
@@ -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.5
11
- - 2.2.1
14
+ - 2.1.10
15
+ - 2.2.5
12
16
  - 2.3.1
13
17
 
14
18
  gemfile:
15
- - gemfiles/activesupport_3_0.gemfile
16
- - gemfiles/activesupport_3_1.gemfile
17
- - gemfiles/activesupport_3_2.gemfile
18
- - gemfiles/activesupport_4_0.gemfile
19
- - gemfiles/activesupport_4_1.gemfile
20
- - gemfiles/activesupport_4_2.gemfile
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
- %w(3_0 3_1 3_2 4_0 4_1 4_2).each do |version|
2
- appraise "activesupport_#{version}" do
3
- gem 'activesupport', "~> #{version.gsub(/_/, '.')}.0"
4
- gem 'activerecord', "~> #{version.gsub(/_/, '.')}.0"
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
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'http://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in enumerate_it.gemspec
4
4
  gemspec
@@ -1,64 +1,65 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumerate_it (1.3.0)
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 (4.2.6)
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.2.0)
21
- coderay (1.0.9)
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
- json (1.8.3)
25
- method_source (0.8.1)
26
- minitest (5.8.4)
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.9.12)
31
- coderay (~> 1.0.5)
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.1.1)
36
- rspec (3.4.0)
37
- rspec-core (~> 3.4.0)
38
- rspec-expectations (~> 3.4.0)
39
- rspec-mocks (~> 3.4.0)
40
- rspec-core (3.4.1)
41
- rspec-support (~> 3.4.0)
42
- rspec-expectations (3.4.0)
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.4.0)
45
- rspec-mocks (3.4.0)
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.4.0)
48
- rspec-support (3.4.1)
49
- rubocop (0.38.0)
50
- parser (>= 2.3.0.6, < 3.0)
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
- ruby-progressbar (1.7.5)
56
- slop (3.4.4)
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.2)
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.11.2
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 models, but you can put them anywhere in your
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
- * `:translation`: The default behavior, will sort the returned values based
191
- on translations.
192
- * `:value`: Will sort the returned values based on values.
193
- * `:name`: Will sort the returned values based on the name of each
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 (ActiveSupport) versions
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: `$ appraisal rake`.
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
- require 'rspec/core'
7
- require 'rspec/core/rake_task'
8
- RSpec::Core::RakeTask.new(:spec) do |spec|
9
- spec.pattern = FileList['spec/**/*_spec.rb']
10
- end
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
@@ -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
- gem.add_development_dependency 'rubocop'
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "activesupport", "~> 4.0.0"
6
- gem "activerecord", "~> 4.0.0"
5
+ gem "activesupport", "~> 3.0.9"
6
+ gem "activerecord", "~> 3.0.9"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "activesupport", "~> 3.0.0"
6
- gem "activerecord", "~> 3.0.0"
5
+ gem "activesupport", "~> 3.1.9"
6
+ gem "activerecord", "~> 3.1.9"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "activesupport", "~> 3.1.0"
6
- gem "activerecord", "~> 3.1.0"
5
+ gem "activesupport", "~> 3.2.9"
6
+ gem "activerecord", "~> 3.2.9"
7
7
 
8
8
  gemspec :path => "../"
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "activesupport", "~> 3.2.0"
6
- gem "activerecord", "~> 3.2.0"
5
+ gem "activesupport", "~> 4.0.9"
6
+ gem "activerecord", "~> 4.0.9"
7
7
 
8
8
  gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activesupport", "~> 4.1.9"
6
+ gem "activerecord", "~> 4.1.9"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activesupport", "~> 4.2.7.1"
6
+ gem "activerecord", "~> 4.2.7.1"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "activesupport", "~> 5.0.0.1"
6
+ gem "activerecord", "~> 5.0.0.1"
7
+
8
+ gemspec :path => "../"
@@ -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
- def self.associate_values(*args)
11
- values_hash = args.first.is_a?(Hash) ? args.first : args.inject({}) { |h, v| h[v] = v.to_s; h }
8
+ def associate_values(*args)
9
+ values = values_hash(args)
12
10
 
13
- register_enumeration normalize_enumeration(values_hash)
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
- def self.sort_by(sort_mode)
18
- @sort_mode = sort_mode
19
- end
13
+ values.each_pair do |value_name, attributes|
14
+ define_enumeration_constant value_name, attributes[0]
15
+ end
16
+ end
20
17
 
21
- def self.list
22
- sorted_map.map { |_k, v| v.first }
23
- end
18
+ def sort_by(sort_mode)
19
+ @sort_mode = sort_mode
20
+ end
24
21
 
25
- def self.enumeration
26
- @@registered_enumerations[self]
27
- end
22
+ def list
23
+ sorted_map.map { |_k, v| v.first }
24
+ end
28
25
 
29
- def self.to_a
30
- sorted_map.map { |_k, v| [translate(v[1]), v[0]] }
31
- end
26
+ def enumeration
27
+ @registered_enumerations[self]
28
+ end
32
29
 
33
- def self.length
34
- list.length
35
- end
30
+ def to_a
31
+ sorted_map.map { |_k, v| [translate(v[1]), v[0]] }
32
+ end
36
33
 
37
- def self.each_translation
38
- each_value { |value| yield t(value) }
39
- end
34
+ def length
35
+ list.length
36
+ end
40
37
 
41
- def self.translations
42
- list.map { |value| t(value) }
43
- end
38
+ def each_translation
39
+ each_value { |value| yield t(value) }
40
+ end
44
41
 
45
- def self.each_value
46
- list.each { |value| yield value }
47
- end
42
+ def translations
43
+ list.map { |value| t(value) }
44
+ end
48
45
 
49
- def self.to_json
50
- sorted_map.map { |_k, v| { value: v[0], label: translate(v[1]) } }.to_json
51
- end
46
+ def each_value
47
+ list.each { |value| yield value }
48
+ end
52
49
 
53
- def self.t(value)
54
- target = to_a.detect { |item| item[1] == value }
55
- target ? target[0] : value
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
- def self.values_for(values)
59
- values.map { |v| self.value_for v.to_sym }
60
- end
54
+ def t(value)
55
+ target = to_a.detect { |item| item[1] == value }
56
+ target ? target[0] : value
57
+ end
61
58
 
62
- def self.value_for(value)
63
- self.const_get(value.to_sym)
64
- rescue NameError
65
- nil
66
- end
59
+ def values_for(values)
60
+ values.map { |v| value_for v.to_sym }
61
+ end
67
62
 
68
- def self.value_from_key(key)
69
- return if key.nil?
70
- (enumeration[key.to_sym] || []).first
71
- end
63
+ def value_for(value)
64
+ const_get(value.to_sym)
65
+ rescue NameError
66
+ nil
67
+ end
72
68
 
73
- def self.keys
74
- enumeration.keys
75
- end
69
+ def value_from_key(key)
70
+ return if key.nil?
71
+ (enumeration[key.to_sym] || []).first
72
+ end
76
73
 
77
- def self.key_for(value)
78
- enumeration.map { |e| e[0] if e[1][0] == value }.compact.first
79
- end
74
+ def keys
75
+ enumeration.keys
76
+ end
80
77
 
81
- def self.to_range
82
- (list.min..list.max)
83
- end
78
+ def key_for(value)
79
+ enumeration.map { |e| e[0] if e[1][0] == value }.compact.first
80
+ end
84
81
 
85
- private
82
+ def to_range
83
+ (list.min..list.max)
84
+ end
86
85
 
87
- def self.sorted_map
88
- return enumeration if sort_mode == :none
86
+ def translate(value)
87
+ return value unless value.is_a? Symbol
89
88
 
90
- enumeration.sort_by { |k, v| sort_lambda.call(k, v) }
91
- end
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
- def self.sort_lambda
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
- def self.translate(value)
102
- return value unless value.is_a? Symbol
95
+ def sorted_map
96
+ return enumeration if sort_mode == :none
103
97
 
104
- default = value.to_s.gsub(/_/, ' ').split.map(&:capitalize).join(' ')
105
- I18n.t("enumerations.#{self.name.underscore}.#{value.to_s.underscore}", default: default)
106
- end
98
+ enumeration.sort_by { |k, v| sort_lambda.call(k, v) }
99
+ end
107
100
 
108
- def self.normalize_enumeration(values_hash)
109
- values_hash.each_pair do |key, value|
110
- values_hash[key] = [value, key] unless value.is_a? Array
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
- def self.register_enumeration(values_hash)
115
- @@registered_enumerations[self] = values_hash
116
- end
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
- def self.define_enumeration_constant(name, value)
119
- const_set name.to_s.gsub(/-/, '_').upcase, value
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