bitswitch 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,6 +31,10 @@ https://github.com/KellyLSB/Bitswitch/wiki/How-to-use
31
31
 
32
32
  * Think about new stuff to add in.
33
33
 
34
+ ## Other Languages
35
+
36
+ BitSwitch is also available in Ruby, [PHP](http://github.com/KellyLSB/Bitswitch-PHP)
37
+
34
38
  ## Copyright
35
39
 
36
40
  Copyright (c) 2012 Kelly Becker. See LICENSE.txt for
@@ -8,11 +8,20 @@ class BitSwitch
8
8
  @labels = {}
9
9
  @val = 0
10
10
 
11
- # Loop through the hash and set the switches
12
- i=0; n.each do |label, tf|
13
- self[i] = tf ? 1 : 0
14
- @labels[i] = label
15
- i += 1
11
+ if labels.empty?
12
+ # Loop through the hash and set the switches
13
+ i=0; n.each do |label, tf|
14
+ self[i] = tf ? 1 : 0
15
+ @labels[i] = label.to_s
16
+ i += 1
17
+ end
18
+ else
19
+ # Set the switches
20
+ @labels = labels
21
+
22
+ n.each do |label, tf|
23
+ self[label.to_s] = tf ? 1 : 0
24
+ end
16
25
  end
17
26
 
18
27
  # Return the BitSwitch object
@@ -29,6 +38,7 @@ class BitSwitch
29
38
  val = val > 0
30
39
 
31
40
  # If a string representation of a bit was provided get the numerical
41
+ bit.to_s if bit.is_a?(Symbol)
32
42
  bit = @labels.invert[bit] if bit.is_a?(String)
33
43
 
34
44
  # If nil return false
@@ -46,6 +56,7 @@ class BitSwitch
46
56
  def [](bit)
47
57
 
48
58
  # If a string representation of a bit was provided get the numerical
59
+ bit.to_s if bit.is_a?(Symbol)
49
60
  bit = @labels.invert[bit] if bit.is_a?(String)
50
61
 
51
62
  # If nil return false
@@ -100,8 +111,95 @@ class BitSwitch
100
111
  end
101
112
  end
102
113
 
114
+ # Convert Fixnum to Switch
103
115
  class Fixnum
104
116
  def to_switch(labels = {})
105
117
  BitSwitch.new(self, labels)
106
118
  end
119
+ end
120
+
121
+ # Convert hash of booleans to Switch
122
+ class Hash
123
+ def to_switch(labels = {})
124
+ cleaned = self.delete_if{|k,v| ![true, false].include?(v)}
125
+ return BitSwitch.new(0, labels) if cleaned.empty?
126
+ return BitSwitch.new(cleaned, labels)
127
+ end
128
+ end
129
+
130
+ # Rails 3 Extension
131
+ if defined? ActiveRecord::Base
132
+ module KellyLSB
133
+ module BitSwitch
134
+ extend ActiveSupport::Concern
135
+
136
+ module ClassMethods
137
+ def bitswitch(column, hash = {})
138
+
139
+ KellyLSB::BitSwitch::LocalInstanceMethods.send(:define_method, column.to_sym) do |*args|
140
+ val = read_attribute(column)
141
+
142
+ # Make sure the value is an integer
143
+ raise KellyLSB::BitSwitch::Error, "Column: #{column} is not an integer!" unless val.is_a?(Fixnum)
144
+
145
+ # Get the BitSwitch
146
+ val = val.to_switch hash
147
+
148
+ # Return the value of a specific key
149
+ return val[args.first.to_s] unless args[0].nil?
150
+
151
+ # Return the switch
152
+ return val
153
+ end
154
+
155
+ columne = column.to_s + '='
156
+ KellyLSB::BitSwitch::LocalInstanceMethods.send(:define_method, columne.to_sym) do |input|
157
+ val = read_attribute(column)
158
+
159
+ # Make sure the value is an integer
160
+ raise KellyLSB::BitSwitch::Error, "Column: #{column} is not an integer!" unless val.is_a?(Fixnum)
161
+
162
+ # Get the BitSwitch
163
+ val = val.to_switch(hash).to_hash.merge(input).to_switch(hash)
164
+
165
+ # Write the updated value
166
+ update_column(column, val.to_i)
167
+
168
+ # Return the switch
169
+ return self.send(column)
170
+ end
171
+
172
+ KellyLSB::BitSwitch::SingletonMethods.send(:define_method, column.to_sym) do |*args|
173
+ raise KellyLSB::BitSwitch::Error, "Missing arguments!" if args.empty?
174
+ bits = hash.invert
175
+
176
+ query = self
177
+
178
+ # Perform conditions
179
+ args.each do |slug|
180
+ query = query.where("POW(2, ?) & `#{self.table_name}`.`#{column}` > 0", bits[slug.to_s])
181
+ end
182
+
183
+ # Return results
184
+ return query
185
+ end
186
+
187
+ include KellyLSB::BitSwitch::LocalInstanceMethods
188
+ extend KellyLSB::BitSwitch::SingletonMethods
189
+ end
190
+ end
191
+
192
+ # This module contains class methods
193
+ module SingletonMethods
194
+ end
195
+
196
+ module LocalInstanceMethods
197
+ end
198
+
199
+ class Error < StandardError
200
+ end
201
+ end
202
+ end
203
+
204
+ ActiveRecord::Base.send(:include, KellyLSB::BitSwitch)
107
205
  end
@@ -1,3 +1,3 @@
1
1
  module Bitswitch
2
- VERSION = "0.0.3"
2
+ VERSION = "1.0.0"
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: 0.0.3
4
+ version: 1.0.0
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-09-25 00:00:00.000000000 Z
12
+ date: 2012-11-27 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
@@ -49,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  version: '0'
50
50
  requirements: []
51
51
  rubyforge_project:
52
- rubygems_version: 1.8.24
52
+ rubygems_version: 1.8.23
53
53
  signing_key:
54
54
  specification_version: 3
55
55
  summary: Bitswitch lets you store multiple true/false values in an integer using boolean