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
data/spec/enumerate_it_spec.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
4
4
|
|
5
5
|
describe EnumerateIt do
|
6
|
-
|
6
|
+
let :target do
|
7
7
|
class TestClass
|
8
8
|
extend EnumerateIt
|
9
9
|
attr_accessor :foobar
|
@@ -16,57 +16,62 @@ describe EnumerateIt do
|
|
16
16
|
I18n.locale = :en
|
17
17
|
end
|
18
18
|
|
19
|
-
|
19
|
+
TestClass.new(TestEnumeration::VALUE_2)
|
20
20
|
end
|
21
21
|
|
22
|
-
context
|
22
|
+
context 'associating an enumeration with a class attribute' do
|
23
23
|
it "creates an humanized description for the attribute's value" do
|
24
|
-
expect(
|
24
|
+
expect(target.foobar_humanize).to eq('Hey, I am 2!')
|
25
25
|
end
|
26
26
|
|
27
|
-
it
|
28
|
-
|
29
|
-
expect(
|
27
|
+
it 'if the attribute is blank, the humanize description is nil' do
|
28
|
+
target.foobar = nil
|
29
|
+
expect(target.foobar_humanize).to be_nil
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
33
|
-
expect(
|
32
|
+
it 'defaults to not creating helper methods' do
|
33
|
+
expect(target).not_to respond_to(:value_1?)
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
36
|
+
it 'stores the enumeration class in a class-level hash' do
|
37
37
|
expect(TestClass.enumerations[:foobar]).to eq(TestEnumeration)
|
38
38
|
end
|
39
39
|
|
40
40
|
context 'use the same enumeration from an inherited class' do
|
41
|
-
|
41
|
+
let :target do
|
42
42
|
class SomeClassWithoutEnum < BaseClass
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
|
+
SomeClassWithoutEnum.new
|
45
46
|
end
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
expect(
|
48
|
+
let(:base) { BaseClass.new }
|
49
|
+
|
50
|
+
it 'has use the correct class' do
|
51
|
+
expect(base.class.enumerations[:foobar]).to eq(TestEnumeration)
|
52
|
+
expect(target.class.enumerations[:foobar]).to eq(TestEnumeration)
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
54
56
|
context 'declaring a simple enum on an inherited class' do
|
55
|
-
|
57
|
+
let :target do
|
56
58
|
class SomeClass < BaseClass
|
57
59
|
has_enumeration_for :foobar
|
58
60
|
end
|
59
|
-
|
61
|
+
|
62
|
+
SomeClass.new
|
60
63
|
end
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
64
|
+
|
65
|
+
let(:base) { BaseClass.new }
|
66
|
+
|
67
|
+
it 'has use the corret class' do
|
68
|
+
expect(base.class.enumerations[:foobar]).to eq(TestEnumeration)
|
69
|
+
expect(target.class.enumerations[:foobar]).to eq(Foobar)
|
65
70
|
end
|
66
71
|
end
|
67
72
|
|
68
|
-
context
|
69
|
-
|
73
|
+
context 'passing options values without the human string (just the value, without an array)' do
|
74
|
+
let :target do
|
70
75
|
class TestClassForEnumerationWithoutArray
|
71
76
|
extend EnumerateIt
|
72
77
|
attr_accessor :foobar
|
@@ -77,56 +82,54 @@ describe EnumerateIt do
|
|
77
82
|
end
|
78
83
|
end
|
79
84
|
|
80
|
-
|
85
|
+
TestClassForEnumerationWithoutArray.new(TestEnumerationWithoutArray::VALUE_TWO)
|
81
86
|
end
|
82
87
|
|
83
|
-
it
|
84
|
-
expect(
|
88
|
+
it 'humanizes the respective hash key' do
|
89
|
+
expect(target.foobar_humanize).to eq('Value Two')
|
85
90
|
end
|
86
91
|
|
87
|
-
it
|
88
|
-
|
89
|
-
expect(
|
92
|
+
it 'translates the respective hash key when a translation is found' do
|
93
|
+
target.foobar = TestEnumerationWithoutArray::VALUE_ONE
|
94
|
+
expect(target.foobar_humanize).to eq('First Value')
|
90
95
|
I18n.locale = :pt
|
91
96
|
|
92
|
-
expect(
|
97
|
+
expect(target.foobar_humanize).to eq('Primeiro Valor')
|
93
98
|
end
|
94
99
|
end
|
95
100
|
|
96
|
-
context
|
97
|
-
|
101
|
+
context 'without passing the enumeration class' do
|
102
|
+
let :target do
|
98
103
|
class FooBar
|
99
104
|
extend EnumerateIt
|
100
105
|
attr_accessor :test_enumeration
|
101
|
-
|
102
106
|
has_enumeration_for :test_enumeration
|
103
107
|
|
104
108
|
def initialize(test_enumeration_value)
|
105
109
|
@test_enumeration = test_enumeration_value
|
106
110
|
end
|
107
111
|
end
|
112
|
+
|
113
|
+
FooBar.new(TestEnumeration::VALUE_1)
|
108
114
|
end
|
109
115
|
|
110
|
-
it
|
111
|
-
target = FooBar.new(TestEnumeration::VALUE_1)
|
116
|
+
it 'finds out which enumeration class to use' do
|
112
117
|
expect(target.test_enumeration_humanize).to eq('Hey, I am 1!')
|
113
118
|
end
|
114
119
|
|
115
|
-
context
|
120
|
+
context 'when using a nested class as the enumeration' do
|
116
121
|
before do
|
117
122
|
class NestedEnum < EnumerateIt::Base
|
118
|
-
associate_values foo:
|
123
|
+
associate_values foo: %w(1 Fooo), bar: %w(2 Barrrr)
|
119
124
|
end
|
120
125
|
|
121
126
|
class ClassWithNestedEnum
|
122
127
|
class NestedEnum < EnumerateIt::Base
|
123
|
-
associate_values foo:
|
128
|
+
associate_values foo: %w(1 Blerrgh), bar: ['2' => 'Blarghhh']
|
124
129
|
end
|
125
130
|
|
126
131
|
extend EnumerateIt
|
127
|
-
|
128
132
|
attr_accessor :nested_enum
|
129
|
-
|
130
133
|
has_enumeration_for :nested_enum
|
131
134
|
|
132
135
|
def initialize(nested_enum_value)
|
@@ -135,14 +138,14 @@ describe EnumerateIt do
|
|
135
138
|
end
|
136
139
|
end
|
137
140
|
|
138
|
-
it
|
141
|
+
it 'uses the inner class as the enumeration class' do
|
139
142
|
expect(ClassWithNestedEnum.new('1').nested_enum_humanize).to eq('Blerrgh')
|
140
143
|
end
|
141
144
|
end
|
142
145
|
end
|
143
146
|
end
|
144
147
|
|
145
|
-
context
|
148
|
+
context 'using the :create_helpers option' do
|
146
149
|
before do
|
147
150
|
class TestClassWithHelper
|
148
151
|
extend EnumerateIt
|
@@ -155,13 +158,13 @@ describe EnumerateIt do
|
|
155
158
|
end
|
156
159
|
end
|
157
160
|
|
158
|
-
it
|
161
|
+
it 'creates helpers methods with question marks for each enumeration option' do
|
159
162
|
target = TestClassWithHelper.new(TestEnumeration::VALUE_2)
|
160
163
|
expect(target).to be_value_2
|
161
164
|
expect(target).not_to be_value_1
|
162
165
|
end
|
163
166
|
|
164
|
-
it
|
167
|
+
it 'creates a mutator method for each enumeration value' do
|
165
168
|
[:value_1, :value_2, :value_3].each do |value|
|
166
169
|
expect(TestClassWithHelper.new(TestEnumeration::VALUE_1)).to respond_to(:"#{value}!")
|
167
170
|
end
|
@@ -173,21 +176,22 @@ describe EnumerateIt do
|
|
173
176
|
expect(target.foobar).to eq(TestEnumeration::VALUE_3)
|
174
177
|
end
|
175
178
|
|
176
|
-
context
|
179
|
+
context 'with :prefix option' do
|
177
180
|
before do
|
178
181
|
class TestClassWithHelper
|
179
182
|
has_enumeration_for :foobar, with: TestEnumeration, create_helpers: { prefix: true }
|
180
183
|
end
|
181
184
|
end
|
182
185
|
|
183
|
-
it
|
186
|
+
it 'creates helpers methods with question marks and prefixes for each enumeration option' do
|
184
187
|
target = TestClassWithHelper.new(TestEnumeration::VALUE_2)
|
185
188
|
expect(target).to be_foobar_value_2
|
186
189
|
end
|
187
190
|
|
188
|
-
it
|
191
|
+
it 'creates a mutator method for each enumeration value' do
|
189
192
|
[:value_1, :value_2, :value_3].each do |value|
|
190
|
-
expect(TestClassWithHelper.new(TestEnumeration::VALUE_1))
|
193
|
+
expect(TestClassWithHelper.new(TestEnumeration::VALUE_1))
|
194
|
+
.to respond_to(:"foobar_#{value}!")
|
191
195
|
end
|
192
196
|
end
|
193
197
|
|
@@ -198,7 +202,7 @@ describe EnumerateIt do
|
|
198
202
|
end
|
199
203
|
end
|
200
204
|
|
201
|
-
context
|
205
|
+
context 'with :polymorphic option' do
|
202
206
|
before do
|
203
207
|
class Polymorphic
|
204
208
|
extend EnumerateIt
|
@@ -211,23 +215,24 @@ describe EnumerateIt do
|
|
211
215
|
target = Polymorphic.new
|
212
216
|
target.foo = PolymorphicEnum::NORMAL
|
213
217
|
|
214
|
-
expect(target.foo_object.print(
|
218
|
+
expect(target.foo_object.print('Gol')).to eq("I'm Normal: Gol")
|
215
219
|
|
216
220
|
target.foo = PolymorphicEnum::CRAZY
|
217
221
|
|
218
|
-
expect(target.foo_object.print(
|
222
|
+
expect(target.foo_object.print('Gol')).to eq('Whoa!: Gol')
|
219
223
|
end
|
220
224
|
|
221
|
-
it
|
225
|
+
it 'returns nil if foo is not set' do
|
222
226
|
target = Polymorphic.new
|
223
227
|
|
224
228
|
expect(target.foo_object).to be_nil
|
225
229
|
end
|
226
230
|
|
227
|
-
context
|
231
|
+
context 'and :suffix' do
|
228
232
|
before do
|
229
233
|
class Polymorphic
|
230
|
-
has_enumeration_for :foo, with: PolymorphicEnum,
|
234
|
+
has_enumeration_for :foo, with: PolymorphicEnum,
|
235
|
+
create_helpers: { polymorphic: { suffix: '_strategy' } }
|
231
236
|
end
|
232
237
|
end
|
233
238
|
|
@@ -235,22 +240,20 @@ describe EnumerateIt do
|
|
235
240
|
target = Polymorphic.new
|
236
241
|
target.foo = PolymorphicEnum::NORMAL
|
237
242
|
|
238
|
-
expect(target.foo_strategy.print(
|
243
|
+
expect(target.foo_strategy.print('Gol')).to eq("I'm Normal: Gol")
|
239
244
|
|
240
245
|
target.foo = PolymorphicEnum::CRAZY
|
241
246
|
|
242
|
-
expect(target.foo_strategy.print(
|
247
|
+
expect(target.foo_strategy.print('Gol')).to eq('Whoa!: Gol')
|
243
248
|
end
|
244
249
|
end
|
245
250
|
end
|
246
251
|
end
|
247
252
|
|
248
|
-
describe
|
249
|
-
context
|
253
|
+
describe 'using the :create_scopes option' do
|
254
|
+
context 'if the hosting class responds to :scope' do
|
250
255
|
before do
|
251
|
-
|
252
|
-
Object.send :remove_const, 'TestClassWithScope' if defined?(TestClassWithScope)
|
253
|
-
end
|
256
|
+
Object.send :remove_const, 'TestClassWithScope' if defined?(TestClassWithScope)
|
254
257
|
|
255
258
|
class TestClassWithScope < ActiveRecord::Base
|
256
259
|
extend EnumerateIt
|
@@ -258,43 +261,44 @@ describe EnumerateIt do
|
|
258
261
|
end
|
259
262
|
end
|
260
263
|
|
261
|
-
it
|
264
|
+
it 'creates a scope for each enumeration value' do
|
262
265
|
TestEnumeration.enumeration.keys.each do |symbol|
|
263
266
|
expect(TestClassWithScope).to respond_to(symbol)
|
264
267
|
end
|
265
268
|
end
|
266
269
|
|
267
|
-
it
|
270
|
+
it 'when called, the scopes create the correct query' do
|
268
271
|
TestEnumeration.enumeration.each do |symbol, pair|
|
269
272
|
expect(TestClassWithScope).to receive(:where).with(foobar: pair.first)
|
270
|
-
TestClassWithScope.send
|
273
|
+
TestClassWithScope.send(symbol)
|
271
274
|
end
|
272
275
|
end
|
273
276
|
end
|
274
277
|
|
275
|
-
context
|
278
|
+
context 'when the hosting class do not respond to :scope' do
|
276
279
|
before do
|
277
280
|
class GenericClass
|
278
281
|
extend EnumerateIt
|
279
282
|
end
|
280
283
|
end
|
281
284
|
|
282
|
-
it
|
283
|
-
expect
|
284
|
-
GenericClass.
|
285
|
-
|
285
|
+
it 'raises no errors' do
|
286
|
+
expect do
|
287
|
+
GenericClass.has_enumeration_for(:foobar, with: TestEnumeration, create_scopes: true)
|
288
|
+
end.not_to raise_error
|
286
289
|
end
|
287
290
|
end
|
288
291
|
|
289
|
-
context
|
292
|
+
context 'with :prefix option' do
|
290
293
|
before do
|
291
294
|
class OtherTestClass < ActiveRecord::Base
|
292
295
|
extend EnumerateIt
|
293
|
-
has_enumeration_for :foobar, with: TestEnumerationWithReservedWords,
|
296
|
+
has_enumeration_for :foobar, with: TestEnumerationWithReservedWords,
|
297
|
+
create_scopes: { prefix: true }
|
294
298
|
end
|
295
299
|
end
|
296
300
|
|
297
|
-
it
|
301
|
+
it 'creates a scope with prefix for each enumeration value' do
|
298
302
|
TestEnumerationWithReservedWords.enumeration.keys.each do |symbol|
|
299
303
|
expect(OtherTestClass).to respond_to(:"foobar_#{symbol}")
|
300
304
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -75,10 +75,10 @@ def create_enumeration_class_with_sort_mode(sort_mode)
|
|
75
75
|
sort_by sort_mode
|
76
76
|
|
77
77
|
associate_values(
|
78
|
-
foo:
|
79
|
-
bar:
|
80
|
-
omg:
|
81
|
-
zomg:
|
78
|
+
foo: %w(1 xyz),
|
79
|
+
bar: %w(2 fgh),
|
80
|
+
omg: %w(3 abc),
|
81
|
+
zomg: %w(0 jkl)
|
82
82
|
)
|
83
83
|
end
|
84
84
|
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: 1.3.
|
4
|
+
version: 1.3.1.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: 2016-
|
12
|
+
date: 2016-08-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -95,15 +95,29 @@ dependencies:
|
|
95
95
|
- - ">="
|
96
96
|
- !ruby/object:Gem::Version
|
97
97
|
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rubocop-rspec
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
98
112
|
description: Enumerations for Ruby with some magic powers!
|
99
113
|
email:
|
100
114
|
executables: []
|
101
115
|
extensions: []
|
102
116
|
extra_rdoc_files: []
|
103
117
|
files:
|
104
|
-
- ".document"
|
105
118
|
- ".gitignore"
|
106
119
|
- ".rspec"
|
120
|
+
- ".rubocop.yml"
|
107
121
|
- ".ruby-version"
|
108
122
|
- ".travis.yml"
|
109
123
|
- Appraisals
|
@@ -114,12 +128,13 @@ files:
|
|
114
128
|
- README.md
|
115
129
|
- Rakefile
|
116
130
|
- enumerate_it.gemspec
|
117
|
-
- gemfiles/
|
118
|
-
- gemfiles/
|
119
|
-
- gemfiles/
|
120
|
-
- gemfiles/
|
121
|
-
- gemfiles/
|
122
|
-
- gemfiles/
|
131
|
+
- gemfiles/rails_3.0.gemfile
|
132
|
+
- gemfiles/rails_3.1.gemfile
|
133
|
+
- gemfiles/rails_3.2.gemfile
|
134
|
+
- gemfiles/rails_4.0.gemfile
|
135
|
+
- gemfiles/rails_4.1.gemfile
|
136
|
+
- gemfiles/rails_4.2.gemfile
|
137
|
+
- gemfiles/rails_5.0.gemfile
|
123
138
|
- lib/enumerate_it.rb
|
124
139
|
- lib/enumerate_it/base.rb
|
125
140
|
- lib/enumerate_it/class_methods.rb
|
@@ -151,12 +166,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
151
166
|
version: '0'
|
152
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
168
|
requirements:
|
154
|
-
- - "
|
169
|
+
- - ">"
|
155
170
|
- !ruby/object:Gem::Version
|
156
|
-
version:
|
171
|
+
version: 1.3.1
|
157
172
|
requirements: []
|
158
173
|
rubyforge_project:
|
159
|
-
rubygems_version: 2.
|
174
|
+
rubygems_version: 2.6.6
|
160
175
|
signing_key:
|
161
176
|
specification_version: 4
|
162
177
|
summary: Ruby Enumerations
|