attr_bitwise 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +8 -1
- data/lib/attr_bitwise.rb +18 -2
- data/lib/attr_bitwise/version.rb +1 -1
- data/spec/attr_bitwise_user_defined_spec.rb +217 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57bf7aecf5f3ba5b00efa5ea754231c9b9ed5cef
|
4
|
+
data.tar.gz: e7db4dc252699db2f4ac1ffbf17e450952c1ae2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69872ff915d08721f8691bebdbfa512427d841cecc6d01e6cd7a4b3eeab54ba8fc63a9c3a5a23cb3aeff1fbf9359749897874820c5a0c1c83ca3f8fb081e4329
|
7
|
+
data.tar.gz: 70f80d780266f62e51324652a8b85662f1e069bd296b10fd79ed5b64db40f50d57a5eb9876269bf9501efcf4fb96153fa01f15223751261e24537394836ace6a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -27,9 +27,16 @@ Bitwise attribute for ruby class and Rails model
|
|
27
27
|
attr_bitwise :<name>, mapping: <values_sym> [, column_name: <column_name>]
|
28
28
|
```
|
29
29
|
|
30
|
+
Alternatively, you can explicitly specify your states by supplying a hash with the values.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
attr_bitwise :<name>, mapping: {<sym1: 1, sym2: 2, sym3: 4>} [, column_name: <column_name>]
|
34
|
+
```
|
35
|
+
|
36
|
+
|
30
37
|
## Example
|
31
38
|
|
32
|
-
You have a website with many locales (English, French, German...) with specific content in each locale. You want your users to be able to chose which content they want to see and you want to be able to query the users by the locales they have
|
39
|
+
You have a website with many locales (English, French, German...) with specific content in each locale. You want your users to be able to chose which content they want to see and you want to be able to query the users by the locales they have chosen.
|
33
40
|
|
34
41
|
Start with migration
|
35
42
|
```ruby
|
data/lib/attr_bitwise.rb
CHANGED
@@ -171,6 +171,7 @@ module AttrBitwise
|
|
171
171
|
|
172
172
|
private
|
173
173
|
|
174
|
+
|
174
175
|
# return mapping given a bitwise name
|
175
176
|
def mapping_from_name(name)
|
176
177
|
const_get("#{name}_mapping".upcase)
|
@@ -182,8 +183,13 @@ module AttrBitwise
|
|
182
183
|
# each sym get a power of 2 value
|
183
184
|
def build_mapping(symbols, name)
|
184
185
|
mapping = {}.tap do |hash|
|
185
|
-
symbols.
|
186
|
-
|
186
|
+
if symbols.is_a?(Hash)
|
187
|
+
validate_user_defined_values!(symbols, name)
|
188
|
+
hash.merge!(symbols.sort_by{|k,v| v}.to_h)
|
189
|
+
else
|
190
|
+
symbols.each_with_index do |key, i|
|
191
|
+
hash[key] = 2**i
|
192
|
+
end
|
187
193
|
end
|
188
194
|
hash[:empty] = 0
|
189
195
|
end
|
@@ -191,14 +197,24 @@ module AttrBitwise
|
|
191
197
|
const_mapping_name = "#{name}_mapping".upcase
|
192
198
|
const_set(const_mapping_name, mapping)
|
193
199
|
end
|
200
|
+
|
201
|
+
def validate_user_defined_values!(hash, name)
|
202
|
+
hash.select{|key,value| (Math.log2(value) % 1.0)!=0}.tap do |invalid_options|
|
203
|
+
if invalid_options.any?
|
204
|
+
raise(ArgumentError, "#{name} value should be a power of two number (#{invalid_options.to_s})")
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
194
208
|
end
|
195
209
|
|
210
|
+
|
196
211
|
##########################
|
197
212
|
# Private instance methods
|
198
213
|
##########################
|
199
214
|
|
200
215
|
private
|
201
216
|
|
217
|
+
|
202
218
|
def force_to_bitwise_value(value_or_symbol, mapping)
|
203
219
|
self.class.force_to_bitwise_value(value_or_symbol, mapping)
|
204
220
|
end
|
data/lib/attr_bitwise/version.rb
CHANGED
@@ -0,0 +1,217 @@
|
|
1
|
+
require_relative '../lib/attr_bitwise.rb'
|
2
|
+
|
3
|
+
describe AttrBitwise, 'with user defined mapping values' do
|
4
|
+
|
5
|
+
describe 'invalid options' do
|
6
|
+
|
7
|
+
|
8
|
+
#wrap to ensure class is not evaluated until we are ready
|
9
|
+
let(:test_class) do
|
10
|
+
|
11
|
+
class InvalidUserDefinedTestClass
|
12
|
+
|
13
|
+
include AttrBitwise
|
14
|
+
|
15
|
+
attr_accessor :fruits_value
|
16
|
+
|
17
|
+
attr_bitwise :fruits, mapping: {banana: 2, kiwi: 4, apple: 1, dog: 3, fish: 99}
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@fruits_value = 0
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'raise an exception with invalid options' do
|
28
|
+
it do
|
29
|
+
expect{test_class}.to raise_error(ArgumentError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'valid options' do
|
35
|
+
|
36
|
+
# test class in order to test concern
|
37
|
+
class UserDefinedTestClass
|
38
|
+
|
39
|
+
include AttrBitwise
|
40
|
+
|
41
|
+
attr_accessor :fruits_value
|
42
|
+
|
43
|
+
attr_bitwise :fruits, mapping: {banana: 2, kiwi: 4, apple: 1}
|
44
|
+
|
45
|
+
def initialize
|
46
|
+
@fruits_value = 0
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
subject { UserDefinedTestClass.new }
|
53
|
+
|
54
|
+
context '.to_bitwise_values' do
|
55
|
+
|
56
|
+
context 'with Hash argument' do
|
57
|
+
it do
|
58
|
+
expect(
|
59
|
+
UserDefinedTestClass.to_bitwise_values(
|
60
|
+
{
|
61
|
+
a: :apple,
|
62
|
+
b: :banana
|
63
|
+
},
|
64
|
+
'fruits'
|
65
|
+
)
|
66
|
+
).to eq [1, 2]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'with Array argument' do
|
71
|
+
it do
|
72
|
+
expect(UserDefinedTestClass.to_bitwise_values(
|
73
|
+
[:apple, :banana],
|
74
|
+
'fruits'
|
75
|
+
)).to eq [1, 2]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'with Fixnum argument' do
|
80
|
+
it do
|
81
|
+
expect(UserDefinedTestClass.to_bitwise_values(1, 'fruits')).to eq 1
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'with `fruits` attribute_name' do
|
88
|
+
|
89
|
+
context '#fruits=' do
|
90
|
+
|
91
|
+
before { subject.fruits = [:banana, :apple] }
|
92
|
+
|
93
|
+
it 'should set proper value' do
|
94
|
+
expect(subject.fruits_value).to eq 3
|
95
|
+
expect(subject.fruits).to eq [:apple, :banana]
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
context '#fruits==' do
|
101
|
+
|
102
|
+
context 'when value is incorrect' do
|
103
|
+
before { subject.fruits = [:banana, :apple] }
|
104
|
+
|
105
|
+
it do
|
106
|
+
expect(subject.fruits == :banana).to eq false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'when value is correct' do
|
111
|
+
before { subject.fruits = [:banana] }
|
112
|
+
|
113
|
+
it do
|
114
|
+
expect(subject.fruits == :banana).to eq true
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'with `fruits_value` = 0' do
|
121
|
+
context '#fruits' do
|
122
|
+
|
123
|
+
it do
|
124
|
+
expect(subject.fruits).to eq []
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
context '#add_fruit' do
|
130
|
+
|
131
|
+
it do
|
132
|
+
subject.add_fruit(:banana)
|
133
|
+
expect(subject.fruits).to eq [:banana]
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'when called twice, each type remains unique' do
|
137
|
+
it do
|
138
|
+
subject.add_fruit(:banana)
|
139
|
+
subject.add_fruit(:banana)
|
140
|
+
expect(subject.fruits).to eq [:banana]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
context '#fruit?(:banana)' do
|
147
|
+
|
148
|
+
it do
|
149
|
+
expect(subject.fruit?(:banana)).to eq false
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'with `fruits_value` = 3' do
|
156
|
+
|
157
|
+
before { subject.fruits_value = 3 }
|
158
|
+
|
159
|
+
context '#fruits' do
|
160
|
+
|
161
|
+
it do
|
162
|
+
expect(subject.fruits).to eq [:apple, :banana]
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
context '#fruit?(:banana)' do
|
168
|
+
|
169
|
+
it do
|
170
|
+
expect(subject.fruit?(:banana)).to eq true
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
context '#remove_fruit' do
|
176
|
+
|
177
|
+
it do
|
178
|
+
subject.remove_fruit(:banana)
|
179
|
+
expect(subject.fruits).to eq [:apple]
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
context '#fruits_union' do
|
185
|
+
|
186
|
+
it do
|
187
|
+
expect(subject.fruits_union(1, 2, 4)).to eq [1, 3, 5, 2, 6, 4]
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
|
192
|
+
context '.bitwise_union' do
|
193
|
+
|
194
|
+
it do
|
195
|
+
expect(
|
196
|
+
TestClass.bitwise_union(1, 2, 'fruits')
|
197
|
+
).to eq [1, 3, 2]
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
context '#bitwise_intersection' do
|
203
|
+
|
204
|
+
it do
|
205
|
+
expect(
|
206
|
+
TestClass.bitwise_intersection(1, 2, 'fruits')
|
207
|
+
).to eq [3]
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_bitwise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charly POLY
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- lib/attr_bitwise.rb
|
85
85
|
- lib/attr_bitwise/version.rb
|
86
86
|
- spec/attr_bitwise_spec.rb
|
87
|
+
- spec/attr_bitwise_user_defined_spec.rb
|
87
88
|
homepage: ''
|
88
89
|
licenses:
|
89
90
|
- MIT
|
@@ -110,3 +111,4 @@ specification_version: 4
|
|
110
111
|
summary: Bitwise attribute for ruby class and Rails model
|
111
112
|
test_files:
|
112
113
|
- spec/attr_bitwise_spec.rb
|
114
|
+
- spec/attr_bitwise_user_defined_spec.rb
|