simple_set 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_set.rb +1 -1
- data/lib/simple_set/set_hash.rb +7 -10
- data/lib/simple_set/version.rb +1 -1
- data/simple_set.gemspec +5 -7
- data/spec/simple_set_spec.rb +98 -98
- metadata +14 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a27bd4248194e50184ad285ba8262e9a387736b9
|
4
|
+
data.tar.gz: 2ca6d88cf8768a74047770d076ea06b0a5290808
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be3ac07d3ca2b6814adb9a0654ec8320b9be02229beb668885c4185d5e8cdb831f2d3bd7fca53dede9228ca4949b5871487c36935b186e7617a2c3faa0e10ff7
|
7
|
+
data.tar.gz: 7871a706ad62f15b1d05839b76c9274070a60bd1b2e5700edb580b05cc57656ad0415a669b400e2e8e870035d518c11218c9bde59c9ddfdbd6bb6199c943d6dc
|
data/lib/simple_set.rb
CHANGED
@@ -109,7 +109,7 @@ module SimpleSet
|
|
109
109
|
if options[:slim] != true then
|
110
110
|
prefix = options[:prefix] && "#{options[:prefix] == true ? set_cd.to_s.singularize : options[:prefix]}_"
|
111
111
|
values.each do |k,code|
|
112
|
-
sym =
|
112
|
+
sym = k.to_sym
|
113
113
|
|
114
114
|
define_method("#{prefix}#{sym}?") do
|
115
115
|
current = send(options[:column]) || 0
|
data/lib/simple_set/set_hash.rb
CHANGED
@@ -1,26 +1,23 @@
|
|
1
|
-
require 'simple_enum/enum_hash'
|
2
|
-
|
3
1
|
module SimpleSet
|
4
2
|
class SetHash < ::ActiveSupport::OrderedHash
|
5
|
-
def initialize(args = []
|
3
|
+
def initialize(args = [])
|
6
4
|
super()
|
7
5
|
|
8
|
-
if args.is_a?(Hash) then
|
6
|
+
args = if args.is_a?(Hash) then
|
9
7
|
args.each { |k,v| set_value_for_reverse_lookup(k, v) }
|
8
|
+
elsif args.is_a?(Array) && !args.first.is_a?(Array) then
|
9
|
+
args.each_with_index.map { |x,y| [x, 2**y] }
|
10
10
|
else
|
11
|
-
|
12
|
-
ary = args.map { |e| [e, 2**e.id] } if args.first.respond_to?(:map) && !args.first.is_a?(Array)
|
13
|
-
ary ||= args
|
14
|
-
ary.each { |e| set_value_for_reverse_lookup(e[0], strings ? e[0].to_s : e[1]) }
|
11
|
+
raise Exception.new()
|
15
12
|
end
|
13
|
+
args.each { |e| set_value_for_reverse_lookup(e[0], e[1]) }
|
16
14
|
|
17
15
|
freeze
|
18
16
|
end
|
19
17
|
|
20
18
|
private
|
21
19
|
def set_value_for_reverse_lookup(key, value)
|
22
|
-
|
23
|
-
self[key] = value
|
20
|
+
self[key.to_sym] = value
|
24
21
|
end
|
25
22
|
end
|
26
23
|
end
|
data/lib/simple_set/version.rb
CHANGED
data/simple_set.gemspec
CHANGED
@@ -17,11 +17,9 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.
|
21
|
-
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency "activerecord", ">= 3.0.0"
|
26
|
-
spec.add_development_dependency "sqlite3"
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.3'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0.0'
|
23
|
+
spec.add_development_dependency 'activerecord', '>= 3.0.0'
|
24
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
27
25
|
end
|
data/spec/simple_set_spec.rb
CHANGED
@@ -17,12 +17,12 @@ describe SimpleSet do
|
|
17
17
|
as_set :values, [:a, :b, :c, :d, :e, :f]
|
18
18
|
end
|
19
19
|
|
20
|
-
InitWithArrayOfSymbol.a.
|
21
|
-
InitWithArrayOfSymbol.b.
|
22
|
-
InitWithArrayOfSymbol.c.
|
23
|
-
InitWithArrayOfSymbol.d.
|
24
|
-
InitWithArrayOfSymbol.e.
|
25
|
-
InitWithArrayOfSymbol.f.
|
20
|
+
expect(InitWithArrayOfSymbol.a).to eq(1)
|
21
|
+
expect(InitWithArrayOfSymbol.b).to eq(2)
|
22
|
+
expect(InitWithArrayOfSymbol.c).to eq(4)
|
23
|
+
expect(InitWithArrayOfSymbol.d).to eq(8)
|
24
|
+
expect(InitWithArrayOfSymbol.e).to eq(16)
|
25
|
+
expect(InitWithArrayOfSymbol.f).to eq(32)
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'should accept a hash' do
|
@@ -30,23 +30,23 @@ describe SimpleSet do
|
|
30
30
|
as_set :values, { a: 1, b: 2, c: 4, d: 8, all: 15 }
|
31
31
|
end
|
32
32
|
|
33
|
-
InitWithHash.a.
|
34
|
-
InitWithHash.b.
|
35
|
-
InitWithHash.c.
|
36
|
-
InitWithHash.d.
|
37
|
-
InitWithHash.all.
|
33
|
+
expect(InitWithHash.a).to eq(1)
|
34
|
+
expect(InitWithHash.b).to eq(2)
|
35
|
+
expect(InitWithHash.c).to eq(4)
|
36
|
+
expect(InitWithHash.d).to eq(8)
|
37
|
+
expect(InitWithHash.all).to eq(15)
|
38
38
|
|
39
39
|
sample = InitWithHash.new
|
40
40
|
sample.all = true
|
41
|
-
sample.a
|
42
|
-
sample.b
|
43
|
-
sample.c
|
44
|
-
sample.d
|
45
|
-
sample.all
|
41
|
+
expect(sample.a?).to be_truthy
|
42
|
+
expect(sample.b?).to be_truthy
|
43
|
+
expect(sample.c?).to be_truthy
|
44
|
+
expect(sample.d?).to be_truthy
|
45
|
+
expect(sample.all?).to be_truthy
|
46
46
|
sample.b = false
|
47
|
-
sample.all
|
47
|
+
expect(sample.all?).to be_falsey
|
48
48
|
sample.b = true
|
49
|
-
sample.all
|
49
|
+
expect(sample.all?).to be_truthy
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'should distinguish nil from empty set' do
|
@@ -56,25 +56,25 @@ describe SimpleSet do
|
|
56
56
|
|
57
57
|
sample = NilOrEmpty.new
|
58
58
|
|
59
|
-
sample.a
|
60
|
-
sample.b
|
61
|
-
sample.values_cd.
|
62
|
-
sample.values.
|
59
|
+
expect(sample.a?).to be_falsey
|
60
|
+
expect(sample.b?).to be_falsey
|
61
|
+
expect(sample.values_cd).to eq(nil)
|
62
|
+
expect(sample.values).to eq(nil)
|
63
63
|
|
64
64
|
sample.values = [:a]
|
65
|
-
sample.values.
|
65
|
+
expect(sample.values).to eq([:a])
|
66
66
|
sample.values = nil
|
67
|
-
sample.values.
|
67
|
+
expect(sample.values).to eq(nil)
|
68
68
|
|
69
69
|
sample.a = true
|
70
|
-
sample.values.
|
70
|
+
expect(sample.values).to eq([:a])
|
71
71
|
sample.a = false
|
72
|
-
sample.values.
|
72
|
+
expect(sample.values).to eq([])
|
73
73
|
|
74
74
|
sample.values = nil
|
75
|
-
sample.values.
|
75
|
+
expect(sample.values).to eq(nil)
|
76
76
|
sample.a = false
|
77
|
-
sample.values.
|
77
|
+
expect(sample.values).to eq([])
|
78
78
|
end
|
79
79
|
|
80
80
|
it 'should support fields with a default value' do
|
@@ -83,11 +83,11 @@ describe SimpleSet do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
sample = FieldWithDefaultValue.new
|
86
|
-
sample.values_with_default_cd.
|
87
|
-
sample.values_with_default.
|
86
|
+
expect(sample.values_with_default_cd).to eq(2)
|
87
|
+
expect(sample.values_with_default).to eq([:y])
|
88
88
|
|
89
89
|
sample.values_with_default = nil
|
90
|
-
sample.values_with_default.
|
90
|
+
expect(sample.values_with_default).to eq(nil)
|
91
91
|
end
|
92
92
|
|
93
93
|
it 'should work outside activerecord' do
|
@@ -99,7 +99,7 @@ describe SimpleSet do
|
|
99
99
|
|
100
100
|
sample = FruitsEater.new
|
101
101
|
sample.fruits_i_like = [:apples, :pinaple]
|
102
|
-
sample.fruits_i_like_cd.
|
102
|
+
expect(sample.fruits_i_like_cd).to eq(5)
|
103
103
|
end
|
104
104
|
|
105
105
|
it 'should return acceptable values' do
|
@@ -107,7 +107,7 @@ describe SimpleSet do
|
|
107
107
|
as_set :spoken_languages, [:english, :french, :german, :japanese]
|
108
108
|
end
|
109
109
|
|
110
|
-
AcceptableValues.spoken_languages.
|
110
|
+
expect(AcceptableValues.spoken_languages).to eq([:english, :french, :german, :japanese])
|
111
111
|
end
|
112
112
|
|
113
113
|
it 'should support Rails assignment' do
|
@@ -117,7 +117,7 @@ describe SimpleSet do
|
|
117
117
|
|
118
118
|
sample = User.new
|
119
119
|
sample.values = ["foo_manager", "baz_manager", ""]
|
120
|
-
sample.values.
|
120
|
+
expect(sample.values).to eq([:foo_manager, :baz_manager])
|
121
121
|
end
|
122
122
|
|
123
123
|
# ___ _ _
|
@@ -143,11 +143,11 @@ describe SimpleSet do
|
|
143
143
|
|
144
144
|
sample = TestOptionColumn1.new
|
145
145
|
sample.languages = [:french, :german]
|
146
|
-
sample.english
|
147
|
-
sample.french
|
148
|
-
sample.german
|
149
|
-
sample.japanese
|
150
|
-
sample.custom_name.
|
146
|
+
expect(sample.english?).to be_falsey
|
147
|
+
expect(sample.french?).to be_truthy
|
148
|
+
expect(sample.german?).to be_truthy
|
149
|
+
expect(sample.japanese?).to be_falsey
|
150
|
+
expect(sample.custom_name).to eq(6)
|
151
151
|
end
|
152
152
|
end
|
153
153
|
|
@@ -164,22 +164,22 @@ describe SimpleSet do
|
|
164
164
|
end
|
165
165
|
|
166
166
|
sample = TestOptionPrefix1.new
|
167
|
-
sample.
|
168
|
-
sample.
|
169
|
-
sample.
|
170
|
-
sample.
|
171
|
-
|
172
|
-
sample.
|
173
|
-
sample.
|
174
|
-
sample.
|
175
|
-
sample.
|
176
|
-
|
177
|
-
TestOptionPrefix1.
|
178
|
-
TestOptionPrefix1.
|
179
|
-
TestOptionPrefix1.
|
180
|
-
TestOptionPrefix1.
|
181
|
-
|
182
|
-
sample.
|
167
|
+
expect(sample).to respond_to(:spoken_language_english?)
|
168
|
+
expect(sample).to respond_to(:spoken_language_french?)
|
169
|
+
expect(sample).to respond_to(:spoken_language_german?)
|
170
|
+
expect(sample).to respond_to(:spoken_language_japanese?)
|
171
|
+
|
172
|
+
expect(sample).to respond_to(:spoken_language_english=)
|
173
|
+
expect(sample).to respond_to(:spoken_language_french=)
|
174
|
+
expect(sample).to respond_to(:spoken_language_german=)
|
175
|
+
expect(sample).to respond_to(:spoken_language_japanese=)
|
176
|
+
|
177
|
+
expect(TestOptionPrefix1).to respond_to(:spoken_language_english)
|
178
|
+
expect(TestOptionPrefix1).to respond_to(:spoken_language_french)
|
179
|
+
expect(TestOptionPrefix1).to respond_to(:spoken_language_german)
|
180
|
+
expect(TestOptionPrefix1).to respond_to(:spoken_language_japanese)
|
181
|
+
|
182
|
+
expect(sample).to_not respond_to(:japanese?)
|
183
183
|
end
|
184
184
|
|
185
185
|
it "should support custom prefix in getters and setters" do
|
@@ -188,22 +188,22 @@ describe SimpleSet do
|
|
188
188
|
end
|
189
189
|
|
190
190
|
sample = TestOptionPrefix2.new
|
191
|
-
sample.
|
192
|
-
sample.
|
193
|
-
sample.
|
194
|
-
sample.
|
195
|
-
|
196
|
-
sample.
|
197
|
-
sample.
|
198
|
-
sample.
|
199
|
-
sample.
|
200
|
-
|
201
|
-
TestOptionPrefix2.
|
202
|
-
TestOptionPrefix2.
|
203
|
-
TestOptionPrefix2.
|
204
|
-
TestOptionPrefix2.
|
205
|
-
|
206
|
-
sample.
|
191
|
+
expect(sample).to respond_to(:speaks_english?)
|
192
|
+
expect(sample).to respond_to(:speaks_french?)
|
193
|
+
expect(sample).to respond_to(:speaks_german?)
|
194
|
+
expect(sample).to respond_to(:speaks_japanese?)
|
195
|
+
|
196
|
+
expect(sample).to respond_to(:speaks_english=)
|
197
|
+
expect(sample).to respond_to(:speaks_french=)
|
198
|
+
expect(sample).to respond_to(:speaks_german=)
|
199
|
+
expect(sample).to respond_to(:speaks_japanese=)
|
200
|
+
|
201
|
+
expect(TestOptionPrefix2).to respond_to(:speaks_english)
|
202
|
+
expect(TestOptionPrefix2).to respond_to(:speaks_french)
|
203
|
+
expect(TestOptionPrefix2).to respond_to(:speaks_german)
|
204
|
+
expect(TestOptionPrefix2).to respond_to(:speaks_japanese)
|
205
|
+
|
206
|
+
expect(sample).to_not respond_to(:japanese?)
|
207
207
|
end
|
208
208
|
end
|
209
209
|
|
@@ -219,21 +219,21 @@ describe SimpleSet do
|
|
219
219
|
as_set :spoken_languages, [:english, :french, :german, :japanese], slim: true
|
220
220
|
end
|
221
221
|
|
222
|
-
TestOptionSlim1.
|
223
|
-
TestOptionSlim1.
|
224
|
-
TestOptionSlim1.
|
225
|
-
TestOptionSlim1.
|
222
|
+
expect(TestOptionSlim1).to_not respond_to(:english)
|
223
|
+
expect(TestOptionSlim1).to_not respond_to(:french)
|
224
|
+
expect(TestOptionSlim1).to_not respond_to(:german)
|
225
|
+
expect(TestOptionSlim1).to_not respond_to(:japanese)
|
226
226
|
|
227
227
|
sample = TestOptionSlim1.new
|
228
|
-
sample.
|
229
|
-
sample.
|
230
|
-
sample.
|
231
|
-
sample.
|
232
|
-
|
233
|
-
sample.
|
234
|
-
sample.
|
235
|
-
sample.
|
236
|
-
sample.
|
228
|
+
expect(sample).to_not respond_to(:english?)
|
229
|
+
expect(sample).to_not respond_to(:french?)
|
230
|
+
expect(sample).to_not respond_to(:german?)
|
231
|
+
expect(sample).to_not respond_to(:japanese?)
|
232
|
+
|
233
|
+
expect(sample).to_not respond_to(:english=)
|
234
|
+
expect(sample).to_not respond_to(:french=)
|
235
|
+
expect(sample).to_not respond_to(:german=)
|
236
|
+
expect(sample).to_not respond_to(:japanese=)
|
237
237
|
end
|
238
238
|
|
239
239
|
it 'should only generate instance members when :class is passed' do
|
@@ -241,21 +241,21 @@ describe SimpleSet do
|
|
241
241
|
as_set :spoken_languages, [:english, :french, :german, :japanese], slim: :class
|
242
242
|
end
|
243
243
|
|
244
|
-
TestOptionSlim2.
|
245
|
-
TestOptionSlim2.
|
246
|
-
TestOptionSlim2.
|
247
|
-
TestOptionSlim2.
|
244
|
+
expect(TestOptionSlim2).to_not respond_to(:english)
|
245
|
+
expect(TestOptionSlim2).to_not respond_to(:french)
|
246
|
+
expect(TestOptionSlim2).to_not respond_to(:german)
|
247
|
+
expect(TestOptionSlim2).to_not respond_to(:japanese)
|
248
248
|
|
249
249
|
sample = TestOptionSlim2.new
|
250
|
-
sample.
|
251
|
-
sample.
|
252
|
-
sample.
|
253
|
-
sample.
|
254
|
-
|
255
|
-
sample.
|
256
|
-
sample.
|
257
|
-
sample.
|
258
|
-
sample.
|
250
|
+
expect(sample).to respond_to(:english?)
|
251
|
+
expect(sample).to respond_to(:french?)
|
252
|
+
expect(sample).to respond_to(:german?)
|
253
|
+
expect(sample).to respond_to(:japanese?)
|
254
|
+
|
255
|
+
expect(sample).to respond_to(:english=)
|
256
|
+
expect(sample).to respond_to(:french=)
|
257
|
+
expect(sample).to respond_to(:german=)
|
258
|
+
expect(sample).to respond_to(:japanese=)
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
@@ -273,7 +273,7 @@ describe SimpleSet do
|
|
273
273
|
|
274
274
|
sample = TestOptionWhiny1.new
|
275
275
|
expect { sample.spoken_languages = [:french, :italian, :japanese] }.to_not raise_error
|
276
|
-
sample.spoken_languages.
|
276
|
+
expect(sample.spoken_languages).to eq([:french, :japanese])
|
277
277
|
end
|
278
278
|
end
|
279
279
|
end
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Romain Tartière
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: simple_enum
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ~>
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 1.6.8
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ~>
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 1.6.8
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: bundler
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -42,30 +28,30 @@ dependencies:
|
|
42
28
|
name: rake
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
44
30
|
requirements:
|
45
|
-
- -
|
31
|
+
- - ~>
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
33
|
+
version: '10.3'
|
48
34
|
type: :development
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
|
-
- -
|
38
|
+
- - ~>
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
40
|
+
version: '10.3'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: rspec
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
|
-
- -
|
45
|
+
- - ~>
|
60
46
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
47
|
+
version: 3.0.0
|
62
48
|
type: :development
|
63
49
|
prerelease: false
|
64
50
|
version_requirements: !ruby/object:Gem::Requirement
|
65
51
|
requirements:
|
66
|
-
- -
|
52
|
+
- - ~>
|
67
53
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
54
|
+
version: 3.0.0
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: activerecord
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -84,16 +70,16 @@ dependencies:
|
|
84
70
|
name: sqlite3
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
86
72
|
requirements:
|
87
|
-
- -
|
73
|
+
- - ~>
|
88
74
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
75
|
+
version: '1.3'
|
90
76
|
type: :development
|
91
77
|
prerelease: false
|
92
78
|
version_requirements: !ruby/object:Gem::Requirement
|
93
79
|
requirements:
|
94
|
-
- -
|
80
|
+
- - ~>
|
95
81
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
82
|
+
version: '1.3'
|
97
83
|
description:
|
98
84
|
email:
|
99
85
|
- romain@blogreen.org
|