simple_enum 2.1.1 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -11
- data/lib/simple_enum/accessors.rb +10 -0
- data/lib/simple_enum/accessors/accessor.rb +7 -1
- data/lib/simple_enum/attribute.rb +21 -1
- data/lib/simple_enum/enum.rb +8 -0
- data/lib/simple_enum/version.rb +1 -1
- data/simple_enum.gemspec +1 -1
- data/spec/simple_enum/accessors_spec.rb +89 -17
- data/spec/simple_enum/attribute_spec.rb +48 -19
- data/spec/simple_enum/enum_spec.rb +29 -5
- data/spec/support/mongoid_support.rb +4 -5
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 788b09549df8143f67025e1c15785ccfcf6b2ff4
|
4
|
+
data.tar.gz: e1a274edb7723d4f9b354cdcae0f40179dba4a8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24a99e865d791190a5468b67b40345438fd32eaf7973a3d08e176ad90ef0cce5dcd9dad9614df22bff5155e74ff1bc179b41aaed007e22a0ab530a584254adab
|
7
|
+
data.tar.gz: 70b69f2c0a4b1516485fba415fd62a492b680a0d4e5d09030ca2a27af790cb2a08d5f872860e399a0dca117f57384e3656eb3d6e6aa60eb61c9031b4aad0e55a
|
data/README.md
CHANGED
@@ -113,7 +113,7 @@ User.females # => #<ActiveRecord::Relation:0x0.....>
|
|
113
113
|
end
|
114
114
|
```
|
115
115
|
|
116
|
-
**Disclaimer**: if you _ever_ decide to reorder this array,
|
116
|
+
**Disclaimer**: if you _ever_ decide to reorder this array, beware that any
|
117
117
|
previous mapping is lost. So it's recommended to create mappings (that might
|
118
118
|
change) using hashes instead of arrays. For stuff like gender it might be
|
119
119
|
probably perfectly fine to use arrays though.
|
@@ -208,7 +208,7 @@ User.females # => #<ActiveRecord::Relation:0x0.....>
|
|
208
208
|
|
209
209
|
```ruby
|
210
210
|
# See lib/simple_enum.rb for other options
|
211
|
-
SimpleEnum.with = [:
|
211
|
+
SimpleEnum.with = [:attribute, :scope]
|
212
212
|
```
|
213
213
|
|
214
214
|
### View Helpers
|
@@ -223,11 +223,23 @@ so expect them to change in future versions of SimpleEnum.
|
|
223
223
|
translate_enum user, :gender # => "Frau" # assuming :de and translations exist
|
224
224
|
te user, :gender # translate_enum is also aliased to te
|
225
225
|
```
|
226
|
+
|
227
|
+
Provide translations in the i18n yaml file like:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
de:
|
231
|
+
enums:
|
232
|
+
gender:
|
233
|
+
female: 'Frau'
|
234
|
+
male: 'Mann'
|
235
|
+
```
|
236
|
+
|
226
237
|
- Build a select tag with a translated dropdown and symbol as value:
|
227
238
|
|
228
239
|
```ruby
|
229
240
|
select :user, :gender, enum_option_pairs(User, :gender)
|
230
241
|
```
|
242
|
+
|
231
243
|
- ...and one with the index as value:
|
232
244
|
|
233
245
|
```ruby
|
@@ -255,16 +267,16 @@ User.females # => returns an ActiveRecord::Relation
|
|
255
267
|
Contributors
|
256
268
|
------------
|
257
269
|
|
258
|
-
- @dmitry - bugfixes and other improvements
|
259
|
-
- @tarsolya - implemented all the ruby 1.9 and rails 3 goodness!
|
260
|
-
- @dbalatero - rails 2.3.5 bugfix & validator fixes
|
261
|
-
- @johnthethird - feature for `_for_select` to return the values
|
270
|
+
- [@dmitry](https://github.com/dmitry) - bugfixes and other improvements
|
271
|
+
- [@tarsolya](https://github.com/tarsolya) - implemented all the ruby 1.9 and rails 3 goodness!
|
272
|
+
- [@dbalatero](https://github.com/dbalatero) - rails 2.3.5 bugfix & validator fixes
|
273
|
+
- [@johnthethird](https://github.com/johnthethird) - feature for `_for_select` to return the values
|
262
274
|
- @sinsiliux - ruby 1.9 fixes and removed AR dependency
|
263
|
-
- @sled - mongoid support
|
264
|
-
- @abrom - `find_by_...` method
|
265
|
-
- @mhuggins - translations fixes
|
266
|
-
- @patbenatar - for helping move towards 2.0 (scopes et all)
|
267
|
-
- @abacha - translation helpers, README fixes
|
275
|
+
- [@sled](https://github.com/sled) - mongoid support
|
276
|
+
- [@abrom](https://github.com/abrom) - `find_by_...` method
|
277
|
+
- [@mhuggins](https://github.com/mhuggins) - translations fixes
|
278
|
+
- [@patbenatar](https://github.com/patbenatar) - for helping move towards 2.0 (scopes et all)
|
279
|
+
- [@abacha](https://github.com/abacha) - translation helpers, README fixes
|
268
280
|
- and all others: https://github.com/lwe/simple_enum/graphs/contributors thanks
|
269
281
|
|
270
282
|
License & Copyright
|
@@ -15,4 +15,14 @@ module SimpleEnum
|
|
15
15
|
klass.new(name, enum, options[:source], options[:prefix])
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
# Public: Extension method to register a custom accessor.
|
20
|
+
#
|
21
|
+
# key - The Symbol of the accessor key, e.g. `:bitwise`
|
22
|
+
# clazz - The Class with the accessor implementation
|
23
|
+
#
|
24
|
+
# Returns nothing
|
25
|
+
def self.register_accessor(key, clazz)
|
26
|
+
Accessors::ACCESSORS[key] = clazz
|
27
|
+
end
|
18
28
|
end
|
@@ -33,7 +33,13 @@ module SimpleEnum
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def was(object)
|
36
|
-
|
36
|
+
changes = object.send(:changed_attributes)
|
37
|
+
key = changes.fetch(source, read_before_type_cast(object))
|
38
|
+
enum.key(key) if key
|
39
|
+
end
|
40
|
+
|
41
|
+
def scope(relation, value)
|
42
|
+
relation.where(source => value)
|
37
43
|
end
|
38
44
|
|
39
45
|
def to_s
|
@@ -13,6 +13,9 @@ module SimpleEnum
|
|
13
13
|
# original method.
|
14
14
|
#
|
15
15
|
module Attribute
|
16
|
+
# Registered registrator methods from extensions
|
17
|
+
EXTENSIONS = []
|
18
|
+
|
16
19
|
def as_enum(name, values, options = {})
|
17
20
|
options.assert_valid_keys(:source, :prefix, :with, :accessor, :map)
|
18
21
|
|
@@ -27,6 +30,10 @@ module SimpleEnum
|
|
27
30
|
send "generate_enum_#{feature}_methods_for", enum, accessor
|
28
31
|
end
|
29
32
|
|
33
|
+
EXTENSIONS.uniq.each do |extension|
|
34
|
+
send "generate_enum_#{extension}_extension_for", enum, accessor
|
35
|
+
end
|
36
|
+
|
30
37
|
enum
|
31
38
|
end
|
32
39
|
|
@@ -70,8 +77,21 @@ module SimpleEnum
|
|
70
77
|
return unless respond_to?(:scope)
|
71
78
|
|
72
79
|
enum.each_pair do |key, value|
|
73
|
-
scope "#{accessor.prefix}#{key.pluralize}", -> {
|
80
|
+
scope "#{accessor.prefix}#{key.pluralize}", -> { accessor.scope(self, value) }
|
74
81
|
end
|
75
82
|
end
|
76
83
|
end
|
84
|
+
|
85
|
+
# Public: Register a generator method and add module as part of
|
86
|
+
# SimpleEnum::Attribute. The generator method is called after all default
|
87
|
+
# generators have been created, this allows to override/change existing methods.
|
88
|
+
#
|
89
|
+
# name - The Symbol with the name of the extension
|
90
|
+
# mod - The Module implementing `generate_enum_{name}_extension_for` method
|
91
|
+
#
|
92
|
+
# Returns nothing
|
93
|
+
def self.register_generator(name, mod)
|
94
|
+
Attribute.send :include, mod
|
95
|
+
Attribute::EXTENSIONS << name.to_s
|
96
|
+
end
|
77
97
|
end
|
data/lib/simple_enum/enum.rb
CHANGED
@@ -25,6 +25,10 @@ module SimpleEnum
|
|
25
25
|
end
|
26
26
|
alias_method :[], :value
|
27
27
|
|
28
|
+
def fetch(key)
|
29
|
+
value(key) || raise("Key \"#{key}\" not found")
|
30
|
+
end
|
31
|
+
|
28
32
|
def each_pair(&block)
|
29
33
|
hash.each_pair(&block)
|
30
34
|
end
|
@@ -38,6 +42,10 @@ module SimpleEnum
|
|
38
42
|
hash.keys
|
39
43
|
end
|
40
44
|
|
45
|
+
def values
|
46
|
+
hash.values
|
47
|
+
end
|
48
|
+
|
41
49
|
def values_at(*keys)
|
42
50
|
keys = keys.map(&:to_s)
|
43
51
|
hash.values_at(*keys)
|
data/lib/simple_enum/version.rb
CHANGED
data/simple_enum.gemspec
CHANGED
@@ -25,6 +25,6 @@ Gem::Specification.new do |s|
|
|
25
25
|
|
26
26
|
s.add_development_dependency 'rake', '>= 10.1.0'
|
27
27
|
s.add_development_dependency 'activerecord', '>= 4.0.0'
|
28
|
-
s.add_development_dependency 'mongoid', '>= 4.0.0
|
28
|
+
s.add_development_dependency 'mongoid', '>= 4.0.0'
|
29
29
|
s.add_development_dependency 'rspec', '~> 2.14'
|
30
30
|
end
|
@@ -19,6 +19,22 @@ describe SimpleEnum::Accessors do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
context '.register_accessor' do
|
23
|
+
let(:accessor) { Class.new { def initialize(*args); end } }
|
24
|
+
subject { described_class.accessor(:gender, "enum", accessor: :would_be) }
|
25
|
+
|
26
|
+
before { SimpleEnum.register_accessor :would_be, accessor }
|
27
|
+
after { SimpleEnum::Accessors::ACCESSORS.delete(:would_be) }
|
28
|
+
|
29
|
+
it 'adds accessor to ACCESSORS' do
|
30
|
+
expect(SimpleEnum::Accessors::ACCESSORS[:would_be]).to eq accessor
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'allows to create an instance of our WouldBeAccessor' do
|
34
|
+
expect(subject).to be_a accessor
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
22
38
|
context 'Accessor' do
|
23
39
|
subject { described_class::Accessor.new(:gender, enum) }
|
24
40
|
|
@@ -125,41 +141,44 @@ describe SimpleEnum::Accessors do
|
|
125
141
|
|
126
142
|
context '#selected?' do
|
127
143
|
it 'returns false when gender_cd is nil' do
|
128
|
-
expect(subject.selected?(object)).to
|
129
|
-
expect(subject.selected?(object, :male)).to
|
144
|
+
expect(subject.selected?(object)).to be_falsey
|
145
|
+
expect(subject.selected?(object, :male)).to be_falsey
|
130
146
|
end
|
131
147
|
|
132
148
|
it 'returns true when gender_cd is != nil' do
|
133
|
-
expect(subject.selected?(klass.new(0))).to
|
134
|
-
expect(subject.selected?(klass.new(1))).to
|
149
|
+
expect(subject.selected?(klass.new(0))).to be_truthy
|
150
|
+
expect(subject.selected?(klass.new(1))).to be_truthy
|
135
151
|
end
|
136
152
|
|
137
153
|
it 'returns true when gender_cd is 0 and :male is passed' do
|
138
|
-
expect(subject.selected?(klass.new(0), :male)).to
|
154
|
+
expect(subject.selected?(klass.new(0), :male)).to be_truthy
|
139
155
|
end
|
140
156
|
|
141
157
|
it 'returns false when gender_cd is 0 and :female is passed' do
|
142
|
-
expect(subject.selected?(klass.new(0), :female)).to
|
158
|
+
expect(subject.selected?(klass.new(0), :female)).to be_falsey
|
143
159
|
end
|
144
160
|
|
145
161
|
it 'returns false when gender_cd is 1 and :other is passed' do
|
146
|
-
expect(subject.selected?(klass.new(0), :other)).to
|
162
|
+
expect(subject.selected?(klass.new(0), :other)).to be_falsey
|
147
163
|
end
|
148
164
|
end
|
149
165
|
|
150
166
|
context '#changed?' do
|
151
167
|
it 'delegates to attribute_changed?' do
|
152
168
|
expect(object).to receive(:attribute_changed?).with('gender_cd') { true }
|
153
|
-
expect(subject.changed?(object)).to
|
169
|
+
expect(subject.changed?(object)).to be_truthy
|
154
170
|
end
|
155
171
|
end
|
156
172
|
|
157
|
-
context 'was' do
|
158
|
-
|
159
|
-
|
160
|
-
expect(subject.was(object)).to eq :female
|
173
|
+
context '#was' do
|
174
|
+
let(:changes) do
|
175
|
+
{ 'gender_cd' => 1 }
|
161
176
|
end
|
162
177
|
|
178
|
+
it 'delegates to changed_attributes and resolves symbol' do
|
179
|
+
expect(object).to receive(:changed_attributes) { changes }
|
180
|
+
expect(subject.was(object)).to eq :female
|
181
|
+
end
|
163
182
|
end
|
164
183
|
end
|
165
184
|
|
@@ -170,33 +189,86 @@ describe SimpleEnum::Accessors do
|
|
170
189
|
it 'does not raise error "private method attribute_was called"' do
|
171
190
|
object.gender = :female
|
172
191
|
expect do
|
173
|
-
expect(object.gender_changed?).to
|
192
|
+
expect(object.gender_changed?).to be_truthy
|
174
193
|
expect(object.gender_was).to eq :male
|
175
194
|
end.to_not raise_error
|
176
195
|
end
|
196
|
+
|
197
|
+
context 'github.com/lwe/simple_enum/issues/109' do
|
198
|
+
fake_active_record(:klass) {
|
199
|
+
as_enum(:gender_cd, %w{female male}, source: :gender_cd)
|
200
|
+
as_enum(:role_cd, %w{completed cap dnf dns}, map: :string, source: :role_cd)
|
201
|
+
}
|
202
|
+
let(:object) { klass.create(role_cd: "cap", gender_cd: :female) }
|
203
|
+
|
204
|
+
context '#gender_cd_was' do
|
205
|
+
it 'returns nil when nil' do
|
206
|
+
expect(klass.create.gender_cd_was).to be_nil
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'returns the current gender' do
|
210
|
+
expect(object.gender_cd_was).to eq :female
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'returns the old gender' do
|
214
|
+
object.gender_cd = :male
|
215
|
+
expect(object.gender_cd_was).to eq :female
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
context '#role_cd_was' do
|
220
|
+
it 'returns nil when nil' do
|
221
|
+
expect(klass.create.role_cd_was).to be_nil
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'returns the current role' do
|
225
|
+
expect(object.role_cd_was).to eq :cap
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'returns completed when changed' do
|
229
|
+
object.role_cd = :completed
|
230
|
+
expect(object.role_cd_was).to eq :cap
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
context '#scope' do
|
237
|
+
fake_active_record(:klass) { as_enum :gender, [:male, :female] }
|
238
|
+
let(:accessor) { described_class::Accessor.new(:gender, enum) }
|
239
|
+
subject { accessor.scope(klass, 1) }
|
240
|
+
|
241
|
+
it 'returns an ActiveRecord::Relation' do
|
242
|
+
expect(subject).to be_a(ActiveRecord::Relation)
|
243
|
+
end
|
244
|
+
|
245
|
+
it "queries for gender_cd = 1" do
|
246
|
+
values_hash = { "gender_cd" => 1 }
|
247
|
+
expect(subject.where_values_hash).to eq values_hash
|
248
|
+
end
|
177
249
|
end
|
178
250
|
|
179
251
|
context 'IgnoreAccessor' do
|
180
252
|
subject { described_class::IgnoreAccessor.new(:gender, enum) }
|
181
253
|
|
182
254
|
it 'sets gender_cd to 0 with symbol' do
|
183
|
-
expect(subject.write(object, :male)).to_not
|
255
|
+
expect(subject.write(object, :male)).to_not be_falsey
|
184
256
|
expect(object.gender_cd).to eq 0
|
185
257
|
end
|
186
258
|
|
187
259
|
it 'sets gender_cd to 1 via value (1)' do
|
188
|
-
expect(subject.write(object, 1)).to_not
|
260
|
+
expect(subject.write(object, 1)).to_not be_falsey
|
189
261
|
expect(object.gender_cd).to eq 1
|
190
262
|
end
|
191
263
|
|
192
264
|
it 'sets gender_cd to nil' do
|
193
|
-
expect(subject.write(object, nil)).to
|
265
|
+
expect(subject.write(object, nil)).to be_falsey
|
194
266
|
expect(object.gender_cd).to be_nil
|
195
267
|
end
|
196
268
|
|
197
269
|
it 'keeps existing value when unknown value is passed' do
|
198
270
|
object.gender_cd = 1
|
199
|
-
expect(subject.write(object, :other)).to
|
271
|
+
expect(subject.write(object, :other)).to be_falsey
|
200
272
|
expect(object.gender_cd).to eq 1
|
201
273
|
end
|
202
274
|
end
|
@@ -10,6 +10,37 @@ describe SimpleEnum::Attribute do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
context '.register_generator' do
|
14
|
+
let(:mod) {
|
15
|
+
Module.new do
|
16
|
+
def generate_enum_spec_extension_for(enum, accessor)
|
17
|
+
module_eval { attr_accessor :some_reader }
|
18
|
+
simple_enum_module.module_eval do
|
19
|
+
define_method("extension_method") { "as_enum(#{enum.name})" }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
}
|
24
|
+
|
25
|
+
before { SimpleEnum.register_generator :spec, mod }
|
26
|
+
after { described_class::EXTENSIONS.clear }
|
27
|
+
|
28
|
+
subject { klass.new }
|
29
|
+
|
30
|
+
it 'adds "spec" to EXTENSIONS' do
|
31
|
+
expect(described_class::EXTENSIONS).to eq %w{spec}
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'calls generate_enum_spec_extension_for during as_enum' do
|
35
|
+
expect(subject.extension_method).to eq "as_enum(gender)"
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'allows to add behavior to class itself (e.g. attr_accessor)' do
|
39
|
+
subject.some_reader = "some value"
|
40
|
+
expect(subject.some_reader).to eq "some value"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
13
44
|
context 'generate_enum_class_accessors_for' do
|
14
45
|
context '.genders' do
|
15
46
|
subject { klass.genders }
|
@@ -48,7 +79,7 @@ describe SimpleEnum::Attribute do
|
|
48
79
|
context '#gender?' do
|
49
80
|
it 'delegates to accessor' do
|
50
81
|
expect(accessor).to receive(:selected?).with(subject, nil) { nil }
|
51
|
-
expect(subject.gender?).to
|
82
|
+
expect(subject.gender?).to be_falsey
|
52
83
|
end
|
53
84
|
end
|
54
85
|
end
|
@@ -70,7 +101,7 @@ describe SimpleEnum::Attribute do
|
|
70
101
|
|
71
102
|
it 'delegates #gender_changed? to accessor' do
|
72
103
|
expect(accessor).to receive(:changed?).with(subject) { true }
|
73
|
-
expect(subject.gender_changed?).to
|
104
|
+
expect(subject.gender_changed?).to be_truthy
|
74
105
|
end
|
75
106
|
|
76
107
|
it 'delegates #gender_was to accesso' do
|
@@ -99,17 +130,17 @@ describe SimpleEnum::Attribute do
|
|
99
130
|
|
100
131
|
it 'delegates #gender? to accessor' do
|
101
132
|
expect(accessor).to receive(:selected?).with(subject, nil) { :female }
|
102
|
-
expect(subject.gender?).to
|
133
|
+
expect(subject.gender?).to be_truthy
|
103
134
|
end
|
104
135
|
|
105
136
|
it 'delegates #male? to accessor' do
|
106
137
|
expect(accessor).to receive(:selected?).with(subject, 'male') { true }
|
107
|
-
expect(subject.male?).to
|
138
|
+
expect(subject.male?).to be_truthy
|
108
139
|
end
|
109
140
|
|
110
141
|
it 'delegates #female? to accessor' do
|
111
142
|
expect(accessor).to receive(:selected?).with(subject, 'female') { false }
|
112
|
-
expect(subject.female?).to
|
143
|
+
expect(subject.female?).to be_falsey
|
113
144
|
end
|
114
145
|
|
115
146
|
it 'delegates #male! to accessor' do
|
@@ -129,17 +160,17 @@ describe SimpleEnum::Attribute do
|
|
129
160
|
|
130
161
|
it 'delegates #gender? to accessor' do
|
131
162
|
expect(accessor).to receive(:selected?).with(subject, nil) { :female }
|
132
|
-
expect(subject.gender?).to
|
163
|
+
expect(subject.gender?).to be_truthy
|
133
164
|
end
|
134
165
|
|
135
166
|
it 'delegates #gender_male? to accessor' do
|
136
167
|
expect(accessor).to receive(:selected?).with(subject, 'male') { true }
|
137
|
-
expect(subject.gender_male?).to
|
168
|
+
expect(subject.gender_male?).to be_truthy
|
138
169
|
end
|
139
170
|
|
140
171
|
it 'delegates #gender_female? to accessor' do
|
141
172
|
expect(accessor).to receive(:selected?).with(subject, 'female') { false }
|
142
|
-
expect(subject.gender_female?).to
|
173
|
+
expect(subject.gender_female?).to be_falsey
|
143
174
|
end
|
144
175
|
|
145
176
|
it 'delegates #gender_male! to accessor' do
|
@@ -159,25 +190,23 @@ describe SimpleEnum::Attribute do
|
|
159
190
|
as_enum :gender, [:male, :female], with: [:scope]
|
160
191
|
}
|
161
192
|
|
162
|
-
|
163
|
-
it 'returns an ActiveRecord::Relation' do
|
164
|
-
expect(subject).to be_a(ActiveRecord::Relation)
|
165
|
-
end
|
193
|
+
let(:accessor) { klass.genders_accessor }
|
166
194
|
|
167
|
-
|
168
|
-
|
169
|
-
expect(
|
195
|
+
shared_examples_for 'delegates to accessor#scope' do |value|
|
196
|
+
it 'delegates to #scope' do
|
197
|
+
expect(accessor).to receive(:scope).with(klass, value)
|
198
|
+
subject
|
170
199
|
end
|
171
200
|
end
|
172
201
|
|
173
202
|
context '.males' do
|
174
203
|
subject { klass.males }
|
175
|
-
it_behaves_like '
|
204
|
+
it_behaves_like 'delegates to accessor#scope', 0
|
176
205
|
end
|
177
206
|
|
178
207
|
context '.females' do
|
179
208
|
subject { klass.females }
|
180
|
-
it_behaves_like '
|
209
|
+
it_behaves_like 'delegates to accessor#scope', 1
|
181
210
|
end
|
182
211
|
|
183
212
|
context 'with prefix' do
|
@@ -187,12 +216,12 @@ describe SimpleEnum::Attribute do
|
|
187
216
|
|
188
217
|
context '.gender_males' do
|
189
218
|
subject { klass.gender_males }
|
190
|
-
it_behaves_like '
|
219
|
+
it_behaves_like 'delegates to accessor#scope', 0
|
191
220
|
end
|
192
221
|
|
193
222
|
context '.gender_females' do
|
194
223
|
subject { klass.gender_females }
|
195
|
-
it_behaves_like '
|
224
|
+
it_behaves_like 'delegates to accessor#scope', 1
|
196
225
|
end
|
197
226
|
end
|
198
227
|
|
@@ -38,6 +38,12 @@ describe SimpleEnum::Enum do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
+
context '#values' do
|
42
|
+
it 'returns the values in the order added' do
|
43
|
+
expect(subject.values).to eq [0, 1]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
41
47
|
context '#each_pair (aliased to #each)' do
|
42
48
|
it 'yields twice with #each_pair' do
|
43
49
|
expect { |b| subject.each_pair(&b) }.to yield_control.exactly(2).times
|
@@ -70,6 +76,24 @@ describe SimpleEnum::Enum do
|
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
79
|
+
context '#fetch' do
|
80
|
+
it 'looks up by string' do
|
81
|
+
expect(subject.fetch('male')).to eq 0
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'looks up by symbol' do
|
85
|
+
expect(subject.fetch(:female)).to eq 1
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'looks up by value' do
|
89
|
+
expect(subject.fetch(0)).to be 0
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'throws exception when key is not found' do
|
93
|
+
expect{subject.fetch(:inexistent)}.to raise_error(/not found/i)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
73
97
|
context '#key' do
|
74
98
|
it 'returns symbolized key for supplied value' do
|
75
99
|
expect(subject.key(0)).to eq :male
|
@@ -83,20 +107,20 @@ describe SimpleEnum::Enum do
|
|
83
107
|
|
84
108
|
context '#include?' do
|
85
109
|
it 'returns true by string' do
|
86
|
-
expect(subject.include?('male')).to
|
110
|
+
expect(subject.include?('male')).to be_truthy
|
87
111
|
end
|
88
112
|
|
89
113
|
it 'returns true by symbol' do
|
90
|
-
expect(subject.include?(:female)).to
|
114
|
+
expect(subject.include?(:female)).to be_truthy
|
91
115
|
end
|
92
116
|
|
93
117
|
it 'returns true by checking actual value' do
|
94
|
-
expect(subject.include?(1)).to
|
118
|
+
expect(subject.include?(1)).to be_truthy
|
95
119
|
end
|
96
120
|
|
97
121
|
it 'returns false when neither in keys nor values' do
|
98
|
-
expect(subject.include?(:other)).to
|
99
|
-
expect(subject.include?(2)).to
|
122
|
+
expect(subject.include?(:other)).to be_falsey
|
123
|
+
expect(subject.include?(2)).to be_falsey
|
100
124
|
end
|
101
125
|
end
|
102
126
|
|
@@ -4,17 +4,16 @@ module MongoidSupport
|
|
4
4
|
def self.connection
|
5
5
|
@connection ||= begin
|
6
6
|
Mongoid.configure.connect_to("simple_enum_mongoid_test")
|
7
|
-
Mongoid.default_session.options[:max_retries] = 0
|
8
7
|
end
|
9
|
-
Mongoid.
|
8
|
+
Mongoid.default_client
|
10
9
|
end
|
11
10
|
|
12
11
|
def self.included(base)
|
13
12
|
base.before {
|
14
13
|
begin
|
15
|
-
MongoidSupport.connection.
|
16
|
-
rescue
|
17
|
-
|
14
|
+
MongoidSupport.connection.list_databases
|
15
|
+
rescue
|
16
|
+
skip "Start MongoDB server to run Mongoid integration tests..."
|
18
17
|
end
|
19
18
|
}
|
20
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lukas Westermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 4.0.0
|
61
|
+
version: 4.0.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 4.0.0
|
68
|
+
version: 4.0.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
138
|
version: 2.0.0
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.2.
|
141
|
+
rubygems_version: 2.2.2
|
142
142
|
signing_key:
|
143
143
|
specification_version: 4
|
144
144
|
summary: Simple enum-like field support for models.
|