has-bit-field 1.1.2 → 2.0.0
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +26 -18
- data/README.md +6 -15
- data/has-bit-field.gemspec +1 -1
- data/lib/has-bit-field.rb +12 -8
- data/lib/has-bit-field/version.rb +1 -1
- data/test/has-bit-field_test.rb +14 -14
- data/test/test_helper.rb +1 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a96b5f28841290a879e97c9221cf6c082fe9ca1b
|
4
|
+
data.tar.gz: 8d2bbaa533f544bd63ea3a2f8ebc08fe46aae943
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7e93b3644141de4d0321dd026948eda410fe94028e8c4dcec321b0b9bc99685bcf1463419f88fff5acf756d1ca49f0eb7f8fa26757dc51cea81abf5875eb059
|
7
|
+
data.tar.gz: d7e7028d4e4a0602388fb32670ae1ad72a25882512b5587e45751fac3997887e423f7e9e3ca9fcb10de071307ecf91e8965003d100bb5196b6957c929b05dc65
|
data/Gemfile.lock
CHANGED
@@ -1,33 +1,41 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
has-bit-field (
|
4
|
+
has-bit-field (2.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
8
8
|
specs:
|
9
|
-
activemodel (
|
10
|
-
activesupport (=
|
11
|
-
builder (~> 3.
|
12
|
-
activerecord (
|
13
|
-
activemodel (=
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
9
|
+
activemodel (4.0.2)
|
10
|
+
activesupport (= 4.0.2)
|
11
|
+
builder (~> 3.1.0)
|
12
|
+
activerecord (4.0.2)
|
13
|
+
activemodel (= 4.0.2)
|
14
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
15
|
+
activesupport (= 4.0.2)
|
16
|
+
arel (~> 4.0.0)
|
17
|
+
activerecord-deprecated_finders (1.0.3)
|
18
|
+
activesupport (4.0.2)
|
19
|
+
i18n (~> 0.6, >= 0.6.4)
|
20
|
+
minitest (~> 4.2)
|
21
|
+
multi_json (~> 1.3)
|
22
|
+
thread_safe (~> 0.1)
|
23
|
+
tzinfo (~> 0.3.37)
|
24
|
+
arel (4.0.1)
|
25
|
+
atomic (1.1.14)
|
26
|
+
builder (3.1.4)
|
27
|
+
i18n (0.6.9)
|
28
|
+
minitest (4.7.5)
|
29
|
+
multi_json (1.8.4)
|
24
30
|
sqlite3 (1.3.7)
|
25
|
-
|
31
|
+
thread_safe (0.1.3)
|
32
|
+
atomic
|
33
|
+
tzinfo (0.3.38)
|
26
34
|
|
27
35
|
PLATFORMS
|
28
36
|
ruby
|
29
37
|
|
30
38
|
DEPENDENCIES
|
31
|
-
activerecord (~>
|
39
|
+
activerecord (~> 4.0)
|
32
40
|
has-bit-field!
|
33
41
|
sqlite3
|
data/README.md
CHANGED
@@ -1,20 +1,13 @@
|
|
1
|
-
has-bit-field
|
2
|
-
=============
|
1
|
+
# has-bit-field
|
3
2
|
|
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.
|
3
|
+
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. Requires Rails 3.0 or greater.
|
5
4
|
|
6
|
-
|
5
|
+
## Usage
|
7
6
|
|
8
7
|
Add the gem to your Gemfile.
|
9
8
|
|
10
9
|
gem 'has-bit-field'
|
11
10
|
|
12
|
-
**Rails 2.3.x**
|
13
|
-
|
14
|
-
To use this with Active Record, you would first require this gem in `config/environment.rb`:
|
15
|
-
|
16
|
-
config.gem "has-bit-field"
|
17
|
-
|
18
11
|
Now in one of your models, you define a bit field like this:
|
19
12
|
|
20
13
|
class Person < ActiveRecord::Base
|
@@ -24,9 +17,8 @@ Now in one of your models, you define a bit field like this:
|
|
24
17
|
|
25
18
|
This means that your database will have an integer column called `bit_field` which will hold the actual bit field. This will generate getter and setter methods for each of the fields. It will also generate a method that has `_bit` as a suffix which will give you the decimal value of the bit that that field is represented by in the bit field. Also there will be a named scope for that field, as well as a named scope prefixed with `not_`, if class you are adding the bit field to responds to `named_scope`. You can use it like this:
|
26
19
|
|
27
|
-
$
|
28
|
-
|
29
|
-
p =>> p = Person.new
|
20
|
+
$ rails c
|
21
|
+
>> p = Person.new
|
30
22
|
=> #<Person id: nil, bit_field: nil, created_at: nil, updated_at: nil>
|
31
23
|
>> p.likes_ice_cream = "true"
|
32
24
|
=> "true"
|
@@ -62,7 +54,6 @@ Another gotcha to be aware of is when combining a bit field with Active Record's
|
|
62
54
|
has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
|
63
55
|
end
|
64
56
|
|
65
|
-
Copyright
|
66
|
-
---------
|
57
|
+
## Copyright
|
67
58
|
|
68
59
|
Copyright (c) 2009 Paul Barry. See LICENSE for details.
|
data/has-bit-field.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.rubyforge_project = "has-bit-field"
|
16
16
|
s.add_development_dependency "sqlite3"
|
17
|
-
s.add_development_dependency "activerecord", "~>
|
17
|
+
s.add_development_dependency "activerecord", "~> 4.0"
|
18
18
|
|
19
19
|
s.files = `git ls-files`.split("\n")
|
20
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/has-bit-field.rb
CHANGED
@@ -43,17 +43,21 @@ module HasBitField
|
|
43
43
|
end
|
44
44
|
}
|
45
45
|
|
46
|
-
scope_sym = respond_to?(:validates) ? :scope : :named_scope
|
47
|
-
|
48
46
|
if columns_hash[bit_field_attribute.to_s].null
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
scope field, lambda {
|
48
|
+
where(arel_table[bit_field_attribute].not_eq(nil).
|
49
|
+
and(Arel::Nodes::InfixOperation.new(:&, arel_table[bit_field_attribute], 1<<i).not_eq(0)))
|
50
|
+
}
|
51
|
+
scope "not_#{field}", lambda {
|
52
|
+
where(arel_table[bit_field_attribute].eq(nil).
|
53
|
+
or(Arel::Nodes::InfixOperation.new(:&, arel_table[bit_field_attribute], 1<<i).eq(0)))
|
52
54
|
}
|
53
55
|
else
|
54
|
-
|
55
|
-
|
56
|
-
|
56
|
+
scope field, lambda {
|
57
|
+
where(Arel::Nodes::InfixOperation.new(:&, arel_table[bit_field_attribute], 1<<i).not_eq(0))
|
58
|
+
}
|
59
|
+
scope "not_#{field}", lambda {
|
60
|
+
where(Arel::Nodes::InfixOperation.new(:&, arel_table[bit_field_attribute], 1<<i).eq(0))
|
57
61
|
}
|
58
62
|
end
|
59
63
|
|
data/test/has-bit-field_test.rb
CHANGED
@@ -103,17 +103,17 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
103
103
|
|
104
104
|
c = Person.create! :bit_field => 0
|
105
105
|
|
106
|
-
assert_equal [b], Person.likes_ice_cream.
|
107
|
-
assert_equal [a,c], Person.not_likes_ice_cream.
|
106
|
+
assert_equal [b], Person.likes_ice_cream.order("id").to_a
|
107
|
+
assert_equal [a,c], Person.not_likes_ice_cream.order("id").to_a
|
108
108
|
|
109
|
-
assert_equal [a], Person.plays_golf.
|
110
|
-
assert_equal [b,c], Person.not_plays_golf.
|
109
|
+
assert_equal [a], Person.plays_golf.order("id").to_a
|
110
|
+
assert_equal [b,c], Person.not_plays_golf.order("id").to_a
|
111
111
|
|
112
|
-
assert_equal [b], Person.watches_tv.
|
113
|
-
assert_equal [a,c], Person.not_watches_tv.
|
112
|
+
assert_equal [b], Person.watches_tv.order("id").to_a
|
113
|
+
assert_equal [a,c], Person.not_watches_tv.order("id").to_a
|
114
114
|
|
115
|
-
assert_equal [a], Person.reads_books.
|
116
|
-
assert_equal [b,c], Person.not_reads_books.
|
115
|
+
assert_equal [a], Person.reads_books.order("id").to_a
|
116
|
+
assert_equal [b,c], Person.not_reads_books.order("id").to_a
|
117
117
|
end
|
118
118
|
|
119
119
|
def test_named_scopes_on_non_nullable_column
|
@@ -132,14 +132,14 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
132
132
|
|
133
133
|
c = Skill.create! :plays_piano => true, :chops_trees => true
|
134
134
|
|
135
|
-
assert_equal [a,b,c], Skill.plays_piano.
|
136
|
-
assert_equal [], Skill.not_plays_piano.
|
135
|
+
assert_equal [a,b,c], Skill.plays_piano.order("id").to_a
|
136
|
+
assert_equal [], Skill.not_plays_piano.order("id").to_a
|
137
137
|
|
138
|
-
assert_equal [a], Skill.mops_floors.
|
139
|
-
assert_equal [b,c], Skill.not_mops_floors.
|
138
|
+
assert_equal [a], Skill.mops_floors.order("id").to_a
|
139
|
+
assert_equal [b,c], Skill.not_mops_floors.order("id").to_a
|
140
140
|
|
141
|
-
assert_equal [b], Skill.makes_soup.
|
142
|
-
assert_equal [a,c], Skill.not_makes_soup.
|
141
|
+
assert_equal [b], Skill.makes_soup.order("id").to_a
|
142
|
+
assert_equal [a,c], Skill.not_makes_soup.order("id").to_a
|
143
143
|
end
|
144
144
|
|
145
145
|
def test_dirty_attributes
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has-bit-field
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Barry
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sqlite3
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '4.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '4.0'
|
41
41
|
description: Provides an easy way to work with bit fields in active record
|
42
42
|
email:
|
43
43
|
- mail@paulbarry.com
|
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
78
|
version: '0'
|
79
79
|
requirements: []
|
80
80
|
rubyforge_project: has-bit-field
|
81
|
-
rubygems_version: 2.0.
|
81
|
+
rubygems_version: 2.0.14
|
82
82
|
signing_key:
|
83
83
|
specification_version: 4
|
84
84
|
summary: Provides an easy way to work with bit fields in active record
|