has-bit-field 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -2
- data/Gemfile.lock +1 -1
- data/README.md +12 -1
- data/lib/has-bit-field.rb +14 -12
- data/lib/has-bit-field/version.rb +1 -1
- data/test/has-bit-field_test.rb +2 -2
- metadata +6 -8
data/.rvmrc
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
|
2
|
-
rvm gemset use has-bit-field
|
1
|
+
rvm ree@has-bit-field --create
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -3,13 +3,22 @@ has-bit-field
|
|
3
3
|
|
4
4
|
has-bit-field allows you to use one attribute of an object to store a bit field which stores the boolean state for multiple flags.
|
5
5
|
|
6
|
+
**Rails 3.0.x**
|
7
|
+
|
8
|
+
Add the gem to your Gemfile.
|
9
|
+
|
10
|
+
gem 'has-bit-field'
|
11
|
+
|
12
|
+
**Rails 2.3.x**
|
13
|
+
|
6
14
|
To use this with Active Record, you would first require this gem in `config/environment.rb`:
|
7
15
|
|
8
|
-
config.gem "
|
16
|
+
config.gem "has-bit-field"
|
9
17
|
|
10
18
|
Now in one of your models, you define a bit field like this:
|
11
19
|
|
12
20
|
class Person < ActiveRecord::Base
|
21
|
+
extend HasBitField
|
13
22
|
has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
|
14
23
|
end
|
15
24
|
|
@@ -39,6 +48,7 @@ This means that your database will have an integer column called `bit_field` whi
|
|
39
48
|
One of the great advantages of this approach is that it is easy to add additional flags as your application evolves without the overhead of adding new table columns since a single integer will be able to store at least 31 boolean flags. A simple amendment to the model will create the new flag on the existing integer column 'on-the-fly'. However, the order of the flags is vitally important and you should only ever add new flags on the end.
|
40
49
|
|
41
50
|
class Person < ActiveRecord::Base
|
51
|
+
extend HasBitField
|
42
52
|
has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books, :nut_allergy
|
43
53
|
end
|
44
54
|
|
@@ -47,6 +57,7 @@ The new flag will be evaluated as false for existing rows on the database table
|
|
47
57
|
Another gotcha to be aware of is when combining a bit field with Active Record's `validates_acceptance_of`. When you call `validates_acceptance_of`, if there is no database column, Active Record will define an `attr_accessor` for that boolean field. If you have already defined the bit field, this will clobber those methods. Also, you need to set the value it's looking for to `true` instead of the default of `"1"`. So here's an example of how to use it:
|
48
58
|
|
49
59
|
class Person < ActiveRecord::Base
|
60
|
+
extend HasBitField
|
50
61
|
validates_acceptance_of :read_books, :message => "You must agree to read", :accept => true
|
51
62
|
has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
|
52
63
|
end
|
data/lib/has-bit-field.rb
CHANGED
@@ -34,19 +34,21 @@ module HasBitField
|
|
34
34
|
#{field} != #{field}_was
|
35
35
|
end
|
36
36
|
}
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
}
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
}
|
48
|
-
|
37
|
+
|
38
|
+
scope_sym = respond_to?(:validates) ? :scope : :named_scope
|
39
|
+
|
40
|
+
if columns_hash[bit_field_attribute.to_s].null
|
41
|
+
class_eval %{
|
42
|
+
send scope_sym, :#{field}, :conditions => ["#{table_name}.#{bit_field_attribute} IS NOT NULL AND (#{table_name}.#{bit_field_attribute} & ?) != 0", #{field}_bit]
|
43
|
+
send scope_sym, :not_#{field}, :conditions => ["#{table_name}.#{bit_field_attribute} IS NULL OR (#{table_name}.#{bit_field_attribute} & ?) = 0", #{field}_bit]
|
44
|
+
}
|
45
|
+
else
|
46
|
+
class_eval %{
|
47
|
+
send scope_sym, :#{field}, :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) != 0", #{field}_bit]
|
48
|
+
send scope_sym, :not_#{field}, :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) = 0", #{field}_bit]
|
49
|
+
}
|
49
50
|
end
|
51
|
+
|
50
52
|
end
|
51
53
|
end
|
52
54
|
end
|
data/test/has-bit-field_test.rb
CHANGED
@@ -178,7 +178,7 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
178
178
|
s.chops_trees = true
|
179
179
|
assert s.chops_trees?
|
180
180
|
assert s.valid?
|
181
|
-
assert
|
181
|
+
assert s.errors[:chops_trees].blank?
|
182
182
|
assert s.save
|
183
183
|
end
|
184
184
|
|
@@ -202,7 +202,7 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
202
202
|
s.plays_piano = true
|
203
203
|
assert s.plays_piano?
|
204
204
|
assert s.valid?
|
205
|
-
assert
|
205
|
+
assert s.errors[:plays_piano].blank?
|
206
206
|
assert s.save
|
207
207
|
end
|
208
208
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has-bit-field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Paul Barry
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
18
|
+
date: 2012-05-17 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: sqlite3-ruby
|
@@ -72,7 +71,6 @@ files:
|
|
72
71
|
- rails/init.rb
|
73
72
|
- test/has-bit-field_test.rb
|
74
73
|
- test/test_helper.rb
|
75
|
-
has_rdoc: true
|
76
74
|
homepage: http://github.com/pjb3/has-bit-field
|
77
75
|
licenses: []
|
78
76
|
|
@@ -102,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
100
|
requirements: []
|
103
101
|
|
104
102
|
rubyforge_project: has-bit-field
|
105
|
-
rubygems_version: 1.
|
103
|
+
rubygems_version: 1.8.17
|
106
104
|
signing_key:
|
107
105
|
specification_version: 3
|
108
106
|
summary: Provides an easy way to work with bit fields in active record
|