bitswitch 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bitswitch.rb +357 -325
- data/lib/bitswitch/version.rb +7 -7
- metadata +3 -2
data/lib/bitswitch.rb
CHANGED
@@ -2,344 +2,376 @@ require 'bitswitch/version'
|
|
2
2
|
|
3
3
|
class BitSwitch
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
5
|
+
def initialize(input = 0, labels = {})
|
6
|
+
|
7
|
+
# Placeholder
|
8
|
+
@labels = {}
|
9
|
+
@val = 0
|
10
|
+
|
11
|
+
# Validate the value input
|
12
|
+
unless input.is_a?(Fixnum) || input.is_a?(Hash)
|
13
|
+
raise KellyLSB::BitSwitch::Error,
|
14
|
+
"BitSwitch: BitSwitch can only accept an instance of `Fixnum` or `Hash` as the first argument"
|
15
|
+
end
|
16
|
+
|
17
|
+
# Validate the labels input
|
18
|
+
unless labels.is_a?(Hash)
|
19
|
+
raise KellyLSB::BitSwitch::Error,
|
20
|
+
"BitSwitch: BitSwitch expected the second argument to be a `Hash`"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Validate hash value input
|
24
|
+
if input.is_a?(Hash)
|
25
|
+
input.each do |label, value|
|
26
|
+
|
27
|
+
# Require a String, Symbol or Fixnum value for input hash keys
|
28
|
+
unless label.is_a?(String) || label.is_a?(Symbol) || label.is_a?(Fixnum)
|
29
|
+
raise KellyLSB::BitSwitch::Error,
|
30
|
+
"BitSwitch: Input Hash keys must be a String, Symbol or Fixnum representation of the bit."
|
31
|
+
end
|
32
|
+
|
33
|
+
# Require input hash values to be true or false
|
34
|
+
unless value === true || value === false
|
35
|
+
raise KellyLSB::BitSwitch::Error,
|
36
|
+
"BitSwitch: Input Hash values must be either true or false."
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Validate label hash format
|
42
|
+
labels.each do |bit, label|
|
43
|
+
|
44
|
+
# Require label bits to be Fixnum
|
45
|
+
unless bit.is_a?(Fixnum)
|
46
|
+
raise KellyLSB::BitSwitch::Error,
|
47
|
+
"BitSwitch: Label Hash keys must be instances of Fixnum"
|
48
|
+
end
|
49
|
+
|
50
|
+
# Require labels to be Strings or Symbols
|
51
|
+
unless label.is_a?(String) || label.is_a?(Symbol)
|
52
|
+
raise KellyLSB::BitSwitch::Error,
|
53
|
+
"BitSwitch: Label Hash values must be either Symbols or Strings"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Apply label hash into the instance variable and assume 0
|
58
|
+
@labels = labels.inject({}){|h, (k, v)|h.merge(k => v.to_sym)}
|
59
|
+
@val = input.is_a?(Hash) ? 0 : input
|
60
|
+
|
61
|
+
# Handle hash input
|
62
|
+
if input.is_a?(Hash)
|
63
|
+
|
64
|
+
# If no labels are set
|
65
|
+
# Loop through the input and set the values
|
66
|
+
input.each_with_index { |(label, value), index|
|
67
|
+
@labels[index] = label.to_sym
|
68
|
+
self[index] = value
|
69
|
+
} if @labels.empty?
|
70
|
+
|
71
|
+
# Otherwise just set
|
72
|
+
input.each { |label, value|
|
73
|
+
self[label] = value
|
74
|
+
} unless @labels.empty?
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Set a bit (or label)
|
79
|
+
def []=(bit, val)
|
80
|
+
|
81
|
+
# Validate input label / bit
|
82
|
+
unless bit.is_a?(Symbol) || bit.is_a?(String) || bit.is_a?(Fixnum)
|
83
|
+
raise KellyLSB::BitSwitch::Error,
|
84
|
+
"BitSwitch (#{__method__}): Expected the key to be a Symbol, String or Fixnum"
|
85
|
+
end
|
86
|
+
|
87
|
+
# Validate input value
|
88
|
+
unless val === true || val === false || val.is_a?(Fixnum)
|
89
|
+
raise KellyLSB::BitSwitch::Error,
|
90
|
+
"BitSwitch (#{__method__}): Expected the value to be true, false or Fixnum"
|
91
|
+
end
|
92
|
+
|
93
|
+
# Convert numerical to boolean
|
94
|
+
val = val > 0 if val.is_a?(Fixnum)
|
95
|
+
|
96
|
+
# Get the numerical representation of the label
|
97
|
+
bit = @labels.invert[bit.to_sym] unless bit.is_a?(Fixnum)
|
98
|
+
|
99
|
+
# If nil return false
|
100
|
+
if bit.nil?
|
101
|
+
raise KellyLSB::BitSwitch::Error,
|
102
|
+
"BitSwitch (#{__method__}): There was no bit to match the requested label."
|
103
|
+
end
|
104
|
+
|
105
|
+
# Set/Unset the bits
|
106
|
+
@val |= 2 ** bit if val
|
107
|
+
@val &= ~(2 ** bit) if !val && self[bit]
|
108
|
+
|
109
|
+
# Return self
|
110
|
+
self
|
111
|
+
end
|
112
|
+
|
113
|
+
# Check a bit status
|
114
|
+
def [](bit)
|
115
|
+
|
116
|
+
# Validate input label / bit
|
117
|
+
unless bit.is_a?(Symbol) || bit.is_a?(String) || bit.is_a?(Fixnum)
|
118
|
+
raise KellyLSB::BitSwitch::Error,
|
119
|
+
"BitSwitch (#{__method__}): Expected the key to be a Symbol, String or Fixnum"
|
120
|
+
end
|
121
|
+
|
122
|
+
# Get the numerical representation of the label
|
123
|
+
bit = @labels.invert[bit.to_sym] unless bit.is_a?(Fixnum)
|
124
|
+
|
125
|
+
# If nil return false
|
126
|
+
if bit.nil?
|
127
|
+
raise KellyLSB::BitSwitch::Error,
|
128
|
+
"BitSwitch (#{__method__}): There was no bit to match the requested label."
|
129
|
+
end
|
130
|
+
|
131
|
+
# Check if the bit was set
|
132
|
+
(2 ** bit) & @val > 0
|
133
|
+
end
|
134
|
+
|
135
|
+
# Set an integer
|
136
|
+
def set=(input)
|
137
|
+
|
138
|
+
# Validate input
|
139
|
+
unless input.is_a?(Fixnum)
|
140
|
+
raise KellyLSB::BitSwitch::Error,
|
141
|
+
"BitSwitch (#{__method__}): Expected value to be a Fixnum"
|
142
|
+
end
|
143
|
+
|
144
|
+
# Set the value
|
145
|
+
@val = input
|
146
|
+
|
147
|
+
# Return self
|
148
|
+
self
|
149
|
+
end
|
150
|
+
|
151
|
+
def labels(hash = {}, reset = false)
|
152
|
+
|
153
|
+
# Either merge or overwrite labels
|
154
|
+
@labels.merge!(hash) unless reset
|
155
|
+
@labels = hash if reset
|
156
|
+
|
157
|
+
# Return self
|
158
|
+
self
|
159
|
+
end
|
160
|
+
|
161
|
+
# Get value
|
162
|
+
def to_i
|
163
|
+
@val
|
164
|
+
end
|
165
|
+
|
166
|
+
# Get hash
|
167
|
+
def to_hash
|
168
|
+
|
169
|
+
# Make sure labels are set
|
170
|
+
if @labels.empty?
|
171
|
+
raise KellyLSB::BitSwitch::Error,
|
172
|
+
"BitSwitch (#{__method__}): No labels were set!"
|
173
|
+
end
|
174
|
+
|
175
|
+
# Prepare new hash
|
176
|
+
serialized = Hash.new
|
177
|
+
|
178
|
+
# Loop through the labels
|
179
|
+
@labels.each do |bit, label|
|
180
|
+
serialized[label] = self[bit]
|
181
|
+
end
|
182
|
+
|
183
|
+
# Return serialization
|
184
|
+
serialized
|
185
|
+
end
|
186
|
+
|
187
|
+
# Method access
|
188
|
+
def method_missing(method, *args)
|
189
|
+
|
190
|
+
# Handle setting values
|
191
|
+
if method[-1] == '='
|
192
|
+
method = method[0..-2]
|
193
|
+
return self[method] = args.first
|
194
|
+
end
|
195
|
+
|
196
|
+
# Return a value
|
197
|
+
self[method]
|
198
|
+
end
|
199
199
|
end
|
200
200
|
|
201
201
|
# Convert Fixnum to Switch
|
202
202
|
class Fixnum
|
203
|
-
|
204
|
-
|
205
|
-
|
203
|
+
def to_switch(labels = {})
|
204
|
+
BitSwitch.new(self, labels)
|
205
|
+
end
|
206
206
|
end
|
207
207
|
|
208
208
|
# Convert hash of booleans to Switch
|
209
209
|
class Hash
|
210
|
-
|
210
|
+
def to_switch(labels = {})
|
211
211
|
|
212
|
-
|
213
|
-
|
212
|
+
# Remove any non boolean values
|
213
|
+
cleaned = self.delete_if{|k,v| ![true, false, 1, 0, '1', '0'].include?(v)}
|
214
214
|
|
215
|
-
|
216
|
-
|
217
|
-
|
215
|
+
# Convert Numerical Booleans
|
216
|
+
cleaned.collect!{|k,v|(v.is_a?(String) ? v.to_i : v)}
|
217
|
+
cleaned.collect!{|k,v|(v.is_a?(Fixnum) ? !v.zero? : v)}
|
218
218
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
219
|
+
# Return new BitSwitch
|
220
|
+
return BitSwitch.new(0, labels) if cleaned.empty?
|
221
|
+
return BitSwitch.new(cleaned, labels)
|
222
|
+
end
|
223
223
|
end
|
224
224
|
|
225
225
|
# Rails 3 Extension
|
226
226
|
if defined? ActiveRecord::Base
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
227
|
+
module KellyLSB
|
228
|
+
module BitSwitch
|
229
|
+
extend ActiveSupport::Concern
|
230
|
+
|
231
|
+
module ClassMethods
|
232
|
+
|
233
|
+
# Generate switch methods
|
234
|
+
def bitswitch(column, hash = {})
|
235
|
+
|
236
|
+
# Set column method name
|
237
|
+
columne = column.to_s + '='
|
238
|
+
|
239
|
+
# Instance methods
|
240
|
+
send(:include, Module.new {
|
241
|
+
|
242
|
+
# BitSwitch access method
|
243
|
+
send(:define_method, column) do |*args|
|
244
|
+
val = read_attribute(column)
|
245
|
+
|
246
|
+
# If nil make 0
|
247
|
+
val = 0 if val.nil?
|
248
|
+
|
249
|
+
# Make sure the column value is a Fixnum
|
250
|
+
unless val.is_a?(Fixnum)
|
251
|
+
raise KellyLSB::BitSwitch::Error,
|
252
|
+
"Column: #{column} is not an integer!"
|
253
|
+
end
|
254
|
+
|
255
|
+
# Convert the Fixnum to a BitSwitch
|
256
|
+
val = val.to_switch hash
|
257
|
+
|
258
|
+
# Return the value of a specific key if requested
|
259
|
+
return val[args.first] unless args[0].nil?
|
260
|
+
|
261
|
+
# Return the switch
|
262
|
+
val
|
263
|
+
end
|
264
|
+
|
265
|
+
# BitSwitch set method
|
266
|
+
send(:define_method, columne) do |args|
|
267
|
+
val = read_attribute(column)
|
268
|
+
|
269
|
+
# If nil make 0
|
270
|
+
val = 0 if val.nil?
|
271
|
+
|
272
|
+
# Get the input data
|
273
|
+
if args.is_a?(Array)
|
274
|
+
input = args[0]
|
275
|
+
truncate = args[1]
|
276
|
+
else
|
277
|
+
input = args
|
278
|
+
truncate = false
|
279
|
+
end
|
280
|
+
|
281
|
+
# Make sure the value is an integer
|
282
|
+
unless val.is_a?(Fixnum)
|
283
|
+
raise KellyLSB::BitSwitch::Error,
|
284
|
+
"Column: #{column} is not an integer!"
|
285
|
+
end
|
286
|
+
|
287
|
+
# Make sure the first input is a hash
|
288
|
+
unless input.is_a?(Hash)
|
289
|
+
raise KellyLSB::BitSwitch::Error,
|
290
|
+
"Input: We are expecting at least one argument that is a Hash"
|
291
|
+
end
|
292
|
+
|
293
|
+
# Convert Fixnum -> BitSwitch -> Hash
|
294
|
+
val = val.to_switch(hash).to_hash
|
295
|
+
|
296
|
+
# Convert all keys to symbols
|
297
|
+
input.delete_if do |key, val|
|
298
|
+
if key.is_a?(String)
|
299
|
+
input[key.to_sym] = val
|
300
|
+
true
|
301
|
+
else
|
302
|
+
false
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
# If we are requested to truncate other keys
|
307
|
+
if truncate == true
|
308
|
+
|
309
|
+
# Get list of unset keys and set them to false
|
310
|
+
remove = val.keys.collect(&:to_sym) - input.keys.collect(&:to_sym)
|
311
|
+
remove.each { |key| input[key] = false }
|
312
|
+
end
|
313
|
+
|
314
|
+
# Merge in the changes then convert to BitSwitch
|
315
|
+
val = val.merge(input).to_switch(hash)
|
316
|
+
|
317
|
+
# Dont save if this is a new model
|
318
|
+
return false if new_record?
|
319
|
+
|
320
|
+
# Write the updated value
|
321
|
+
update_column(column, val.to_i)
|
322
|
+
|
323
|
+
# Return the switch
|
324
|
+
return self.send(column)
|
325
|
+
end
|
326
|
+
})
|
327
|
+
|
328
|
+
# Scoping methods
|
329
|
+
send(:extend, Module.new {
|
330
|
+
send(:define_method, column) do |*args|
|
331
|
+
|
332
|
+
# Require at least one argument
|
333
|
+
if args.empty?
|
334
|
+
raise KellyLSB::BitSwitch::Error,
|
335
|
+
"Missing arguments! We were expecing at least one label or bit to query by."
|
336
|
+
end
|
337
|
+
|
338
|
+
# Invert the label hash
|
339
|
+
bits = hash.invert
|
340
|
+
|
341
|
+
# Type of condition
|
342
|
+
if args.first.is_a?(String) && ['AND', 'OR'].include?(args.first.to_s.upcase)
|
343
|
+
delimiter = args.shift
|
344
|
+
else
|
345
|
+
delimiter = 'AND'
|
346
|
+
end
|
347
|
+
|
348
|
+
# Empty conditions
|
349
|
+
conditions = Array.new
|
350
|
+
|
351
|
+
# Build conditions
|
352
|
+
if args.first.is_a?(Hash)
|
353
|
+
args.first.each do |slug, tf|
|
354
|
+
bit = bits[slug.to_s]
|
355
|
+
conditions << "POW(2, #{bit}) & #{self.table_name}.#{column}" + (tf ? ' > 0' : ' <= 0')
|
356
|
+
end
|
357
|
+
else
|
358
|
+
args.each do |slug|
|
359
|
+
bit = bits[slug.to_s]
|
360
|
+
conditions << "POW(2, #{bit}) & #{self.table_name}.#{column} > 0"
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
# If we have query conditions go ahead and return the updated scope
|
365
|
+
return self.where(conditions.join(" #{delimiter} ")) unless conditions.empty?
|
366
|
+
|
367
|
+
# Return self
|
368
|
+
self
|
369
|
+
end
|
370
|
+
})
|
371
|
+
end
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
ActiveRecord::Base.send(:include, KellyLSB::BitSwitch)
|
345
377
|
end
|
data/lib/bitswitch/version.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module KellyLSB
|
2
|
-
|
2
|
+
module BitSwitch
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# Error class
|
5
|
+
class Error < StandardError
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
# Version of the gem
|
9
|
+
VERSION = "1.1.1"
|
10
|
+
end
|
11
11
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitswitch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Have you ever wanted to store multiple true/false values in your database,
|
15
15
|
but annoyed with how many fields your tables have, then BitSwitcher is good for
|
@@ -55,3 +55,4 @@ specification_version: 3
|
|
55
55
|
summary: Bitswitch lets you store multiple true/false values in an integer using boolean
|
56
56
|
math.
|
57
57
|
test_files: []
|
58
|
+
has_rdoc:
|