bitswitch 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/bitswitch.rb +60 -70
- data/lib/bitswitch/version.rb +1 -1
- metadata +2 -2
data/lib/bitswitch.rb
CHANGED
@@ -135,94 +135,84 @@ if defined? ActiveRecord::Base
|
|
135
135
|
|
136
136
|
module ClassMethods
|
137
137
|
def bitswitch(column, hash = {})
|
138
|
+
columne = column.to_s + '='
|
139
|
+
send(:include, Module.new {
|
140
|
+
send(:define_method, column.to_sym) do |*args|
|
141
|
+
val = read_attribute(column)
|
138
142
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
# If nil make 0
|
143
|
-
val = 0 if val.nil?
|
144
|
-
|
145
|
-
# Make sure the value is an integer
|
146
|
-
raise KellyLSB::BitSwitch::Error, "Column: #{column} is not an integer!" unless val.is_a?(Fixnum)
|
147
|
-
|
148
|
-
# Get the BitSwitch
|
149
|
-
val = val.to_switch hash
|
143
|
+
# If nil make 0
|
144
|
+
val = 0 if val.nil?
|
150
145
|
|
151
|
-
|
152
|
-
|
146
|
+
# Make sure the value is an integer
|
147
|
+
raise KellyLSB::BitSwitch::Error, "Column: #{column} is not an integer!" unless val.is_a?(Fixnum)
|
153
148
|
|
154
|
-
|
155
|
-
|
156
|
-
end
|
149
|
+
# Get the BitSwitch
|
150
|
+
val = val.to_switch hash
|
157
151
|
|
158
|
-
|
159
|
-
|
160
|
-
val = read_attribute(column)
|
152
|
+
# Return the value of a specific key
|
153
|
+
return val[args.first.to_s] unless args[0].nil?
|
161
154
|
|
162
|
-
|
163
|
-
|
155
|
+
# Return the switch
|
156
|
+
return val
|
157
|
+
end
|
164
158
|
|
165
|
-
|
166
|
-
|
159
|
+
send(:define_method, columne.to_sym) do |input|
|
160
|
+
val = read_attribute(column)
|
167
161
|
|
168
|
-
|
169
|
-
|
162
|
+
# If nil make 0
|
163
|
+
val = 0 if val.nil?
|
170
164
|
|
171
|
-
|
172
|
-
|
165
|
+
# Make sure the value is an integer
|
166
|
+
raise KellyLSB::BitSwitch::Error, "Column: #{column} is not an integer!" unless val.is_a?(Fixnum)
|
173
167
|
|
174
|
-
|
175
|
-
|
176
|
-
end
|
168
|
+
# Get the BitSwitch
|
169
|
+
val = val.to_switch(hash).to_hash.merge(input).to_switch(hash)
|
177
170
|
|
178
|
-
|
179
|
-
|
180
|
-
bits = hash.invert
|
171
|
+
# Write the updated value
|
172
|
+
update_column(column, val.to_i)
|
181
173
|
|
182
|
-
|
183
|
-
|
184
|
-
delimiter = args.shift
|
185
|
-
else
|
186
|
-
delimiter = 'AND'
|
174
|
+
# Return the switch
|
175
|
+
return self.send(column)
|
187
176
|
end
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
177
|
+
})
|
178
|
+
|
179
|
+
send(:extend, Module.new {
|
180
|
+
send(:define_method, column.to_sym) do |*args|
|
181
|
+
raise KellyLSB::BitSwitch::Error, "Missing arguments!" if args.empty?
|
182
|
+
bits = hash.invert
|
183
|
+
|
184
|
+
# Type of condition
|
185
|
+
if args.first.is_a?(String) && ['AND', 'OR'].include?(args.first.upcase)
|
186
|
+
delimiter = args.shift
|
187
|
+
else
|
188
|
+
delimiter = 'AND'
|
197
189
|
end
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
190
|
+
|
191
|
+
# Empty conditions
|
192
|
+
conditions = Array.new
|
193
|
+
|
194
|
+
# Build conditions
|
195
|
+
if args.first.is_a?(Hash)
|
196
|
+
args.first.each do |slug,tf|
|
197
|
+
bit = bits[slug.to_s]
|
198
|
+
conditions << "POW(2, #{bit}) & `#{self.table_name}`.`#{column}`" + (tf ? ' > 0' : ' <= 0')
|
199
|
+
end
|
200
|
+
else
|
201
|
+
args.each do |slug|
|
202
|
+
bit = bits[slug.to_s]
|
203
|
+
conditions << "POW(2, #{bit}) & `#{self.table_name}`.`#{column}` > 0"
|
204
|
+
end
|
202
205
|
end
|
203
|
-
end
|
204
206
|
|
205
|
-
|
206
|
-
|
207
|
+
# Run add query
|
208
|
+
return self.where(conditions.join(" #{delimiter} ")) unless conditions.empty?
|
207
209
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
include KellyLSB::BitSwitch::LocalInstanceMethods
|
213
|
-
extend KellyLSB::BitSwitch::SingletonMethods
|
210
|
+
# Return update query
|
211
|
+
return query
|
212
|
+
end
|
213
|
+
})
|
214
214
|
end
|
215
215
|
end
|
216
|
-
|
217
|
-
# This module contains class methods
|
218
|
-
module SingletonMethods
|
219
|
-
end
|
220
|
-
|
221
|
-
module LocalInstanceMethods
|
222
|
-
end
|
223
|
-
|
224
|
-
class Error < StandardError
|
225
|
-
end
|
226
216
|
end
|
227
217
|
end
|
228
218
|
|
data/lib/bitswitch/version.rb
CHANGED
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.0.
|
4
|
+
version: 1.0.3
|
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: 2012-11-
|
12
|
+
date: 2012-11-28 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
|