bitswitch 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/bitswitch.rb +60 -70
  2. data/lib/bitswitch/version.rb +1 -1
  3. metadata +2 -2
@@ -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
- KellyLSB::BitSwitch::LocalInstanceMethods.send(:define_method, column.to_sym) do |*args|
140
- val = read_attribute(column)
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
- # Return the value of a specific key
152
- return val[args.first.to_s] unless args[0].nil?
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
- # Return the switch
155
- return val
156
- end
149
+ # Get the BitSwitch
150
+ val = val.to_switch hash
157
151
 
158
- columne = column.to_s + '='
159
- KellyLSB::BitSwitch::LocalInstanceMethods.send(:define_method, columne.to_sym) do |input|
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
- # If nil make 0
163
- val = 0 if val.nil?
155
+ # Return the switch
156
+ return val
157
+ end
164
158
 
165
- # Make sure the value is an integer
166
- raise KellyLSB::BitSwitch::Error, "Column: #{column} is not an integer!" unless val.is_a?(Fixnum)
159
+ send(:define_method, columne.to_sym) do |input|
160
+ val = read_attribute(column)
167
161
 
168
- # Get the BitSwitch
169
- val = val.to_switch(hash).to_hash.merge(input).to_switch(hash)
162
+ # If nil make 0
163
+ val = 0 if val.nil?
170
164
 
171
- # Write the updated value
172
- update_column(column, val.to_i)
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
- # Return the switch
175
- return self.send(column)
176
- end
168
+ # Get the BitSwitch
169
+ val = val.to_switch(hash).to_hash.merge(input).to_switch(hash)
177
170
 
178
- KellyLSB::BitSwitch::SingletonMethods.send(:define_method, column.to_sym) do |*args|
179
- raise KellyLSB::BitSwitch::Error, "Missing arguments!" if args.empty?
180
- bits = hash.invert
171
+ # Write the updated value
172
+ update_column(column, val.to_i)
181
173
 
182
- # Type of condition
183
- if args.first.is_a?(String) && ['AND', 'OR'].include?(args.first.upcase)
184
- delimiter = args.shift
185
- else
186
- delimiter = 'AND'
174
+ # Return the switch
175
+ return self.send(column)
187
176
  end
188
-
189
- # Empty conditions
190
- conditions = Array.new
191
-
192
- # Build conditions
193
- if args.first.is_a?(Hash)
194
- args.first.each do |slug,tf|
195
- bit = bits[slug.to_s]
196
- conditions << "POW(2, #{bit}) & `#{self.table_name}`.`#{column}`" + (tf ? ' > 0' : ' <= 0')
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
- else
199
- args.each do |slug|
200
- bit = bits[slug.to_s]
201
- conditions << "POW(2, #{bit}) & `#{self.table_name}`.`#{column}` > 0"
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
- # Run add query
206
- return self.where(conditions.join(" #{delimiter} ")) unless conditions.empty?
207
+ # Run add query
208
+ return self.where(conditions.join(" #{delimiter} ")) unless conditions.empty?
207
209
 
208
- # Return update query
209
- return query
210
- end
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
 
@@ -1,3 +1,3 @@
1
1
  module Bitswitch
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  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.0.2
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-27 00:00:00.000000000 Z
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