has-bit-field 0.3.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -3
- data/Gemfile.lock +20 -0
- data/Rakefile +3 -46
- data/has-bit-field.gemspec +17 -47
- data/lib/has-bit-field.rb +39 -23
- data/lib/has-bit-field/version.rb +7 -0
- data/test/has-bit-field_test.rb +38 -12
- data/test/test_helper.rb +2 -0
- metadata +46 -16
- data/VERSION +0 -1
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
has-bit-field (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activerecord (2.3.10)
|
10
|
+
activesupport (= 2.3.10)
|
11
|
+
activesupport (2.3.10)
|
12
|
+
sqlite3-ruby (1.3.2)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
activerecord (~> 2.3.5)
|
19
|
+
has-bit-field!
|
20
|
+
sqlite3-ruby
|
data/Rakefile
CHANGED
@@ -1,20 +1,7 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'jeweler'
|
6
|
-
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name = "has-bit-field"
|
8
|
-
gem.summary = %Q{Provides an easy way for dealing with bit fields and active record}
|
9
|
-
gem.email = "mail@paulbarry.com"
|
10
|
-
gem.homepage = "http://github.com/pjb3/has-bit-field"
|
11
|
-
gem.authors = ["Paul Barry"]
|
12
|
-
end
|
13
|
-
Jeweler::GemcutterTasks.new
|
14
|
-
rescue LoadError
|
15
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
16
|
-
end
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
17
3
|
|
4
|
+
require 'rake'
|
18
5
|
require 'rake/testtask'
|
19
6
|
Rake::TestTask.new(:test) do |test|
|
20
7
|
test.libs << 'lib' << 'test'
|
@@ -22,34 +9,4 @@ Rake::TestTask.new(:test) do |test|
|
|
22
9
|
test.verbose = true
|
23
10
|
end
|
24
11
|
|
25
|
-
begin
|
26
|
-
require 'rcov/rcovtask'
|
27
|
-
Rcov::RcovTask.new do |test|
|
28
|
-
test.libs << 'test'
|
29
|
-
test.pattern = 'test/**/*_test.rb'
|
30
|
-
test.verbose = true
|
31
|
-
end
|
32
|
-
rescue LoadError
|
33
|
-
task :rcov do
|
34
|
-
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
|
39
12
|
task :default => :test
|
40
|
-
|
41
|
-
require 'rake/rdoctask'
|
42
|
-
Rake::RDocTask.new do |rdoc|
|
43
|
-
if File.exist?('VERSION.yml')
|
44
|
-
config = YAML.load(File.read('VERSION.yml'))
|
45
|
-
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
46
|
-
else
|
47
|
-
version = ""
|
48
|
-
end
|
49
|
-
|
50
|
-
rdoc.rdoc_dir = 'rdoc'
|
51
|
-
rdoc.title = "has-bit-field #{version}"
|
52
|
-
rdoc.rdoc_files.include('README*')
|
53
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
-
end
|
55
|
-
|
data/has-bit-field.gemspec
CHANGED
@@ -1,53 +1,23 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "has-bit-field/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |s|
|
7
|
-
s.name
|
8
|
-
s.version
|
6
|
+
s.name = "has-bit-field"
|
7
|
+
s.version = Has::Bit::Field::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Paul Barry"]
|
10
|
+
s.email = ["mail@paulbarry.com"]
|
11
|
+
s.homepage = "http://github.com/pjb3/has-bit-field"
|
12
|
+
s.summary = "Provides an easy way to work with bit fields in active record"
|
13
|
+
s.description = "Provides an easy way to work with bit fields in active record"
|
9
14
|
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
s.email = %q{mail@paulbarry.com}
|
14
|
-
s.extra_rdoc_files = [
|
15
|
-
"LICENSE",
|
16
|
-
"README.md"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".document",
|
20
|
-
".gitignore",
|
21
|
-
".rvmrc",
|
22
|
-
"Gemfile",
|
23
|
-
"LICENSE",
|
24
|
-
"README.md",
|
25
|
-
"Rakefile",
|
26
|
-
"VERSION",
|
27
|
-
"has-bit-field.gemspec",
|
28
|
-
"lib/has-bit-field.rb",
|
29
|
-
"rails/init.rb",
|
30
|
-
"test/has-bit-field_test.rb",
|
31
|
-
"test/test_helper.rb"
|
32
|
-
]
|
33
|
-
s.homepage = %q{http://github.com/pjb3/has-bit-field}
|
34
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
-
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version = %q{1.3.7}
|
37
|
-
s.summary = %q{Provides an easy way for dealing with bit fields and active record}
|
38
|
-
s.test_files = [
|
39
|
-
"test/has-bit-field_test.rb",
|
40
|
-
"test/test_helper.rb"
|
41
|
-
]
|
42
|
-
|
43
|
-
if s.respond_to? :specification_version then
|
44
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
-
s.specification_version = 3
|
15
|
+
s.rubyforge_project = "has-bit-field"
|
16
|
+
s.add_development_dependency "sqlite3-ruby"
|
17
|
+
s.add_development_dependency "activerecord", "~> 2.3.5"
|
46
18
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
52
23
|
end
|
53
|
-
|
data/lib/has-bit-field.rb
CHANGED
@@ -5,31 +5,47 @@ module HasBitField
|
|
5
5
|
# which will be the name of each flag in the bit field
|
6
6
|
def has_bit_field(bit_field_attribute, *args)
|
7
7
|
args.each_with_index do |field,i|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
define_method("#{field}?") do
|
15
|
-
send(field)
|
16
|
-
end
|
17
|
-
define_method("#{field}=") do |v|
|
18
|
-
if v.to_s == "true" || v.to_s == "1"
|
19
|
-
send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) | self.class.send("#{field}_bit")))
|
20
|
-
else
|
21
|
-
send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) & ~self.class.send("#{field}_bit")))
|
8
|
+
class_eval %{
|
9
|
+
class << self
|
10
|
+
def #{field}_bit
|
11
|
+
(1 << #{i})
|
12
|
+
end
|
22
13
|
end
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
14
|
+
|
15
|
+
def #{field}
|
16
|
+
(#{bit_field_attribute} & self.class.#{field}_bit) != 0
|
17
|
+
end
|
18
|
+
|
19
|
+
alias #{field}? #{field}
|
20
|
+
|
21
|
+
def #{field}=(v)
|
22
|
+
if v.to_s == "true" || v.to_s == "1"
|
23
|
+
self.#{bit_field_attribute} = (#{bit_field_attribute} || 0) | self.class.#{field}_bit
|
24
|
+
else
|
25
|
+
self.#{bit_field_attribute} = (#{bit_field_attribute} || 0) & ~self.class.#{field}_bit
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def #{field}_was
|
30
|
+
(#{bit_field_attribute}_was & self.class.#{field}_bit) != 0
|
31
|
+
end
|
32
|
+
|
33
|
+
def #{field}_changed?
|
34
|
+
#{field} != #{field}_was
|
35
|
+
end
|
36
|
+
}
|
30
37
|
if(respond_to?(:named_scope))
|
31
|
-
|
32
|
-
|
38
|
+
if columns_hash[bit_field_attribute.to_s].null
|
39
|
+
class_eval %{
|
40
|
+
named_scope :#{field}, :conditions => ["#{table_name}.#{bit_field_attribute} IS NOT NULL AND (#{table_name}.#{bit_field_attribute} & ?) != 0", #{field}_bit]
|
41
|
+
named_scope :not_#{field}, :conditions => ["#{table_name}.#{bit_field_attribute} IS NULL OR (#{table_name}.#{bit_field_attribute} & ?) = 0", #{field}_bit]
|
42
|
+
}
|
43
|
+
else
|
44
|
+
class_eval %{
|
45
|
+
named_scope :#{field}, :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) != 0", #{field}_bit]
|
46
|
+
named_scope :not_#{field}, :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) = 0", #{field}_bit]
|
47
|
+
}
|
48
|
+
end
|
33
49
|
end
|
34
50
|
end
|
35
51
|
end
|
data/test/has-bit-field_test.rb
CHANGED
@@ -8,7 +8,7 @@ ActiveRecord::Base.establish_connection(
|
|
8
8
|
#ActiveRecord::Base.logger = Logger.new(STDOUT)
|
9
9
|
|
10
10
|
ActiveRecord::Base.connection.create_table(:people) do |t|
|
11
|
-
t.integer :bit_field
|
11
|
+
t.integer :bit_field, :null => true
|
12
12
|
end
|
13
13
|
|
14
14
|
class Person < ActiveRecord::Base
|
@@ -19,8 +19,8 @@ end
|
|
19
19
|
|
20
20
|
|
21
21
|
ActiveRecord::Base.connection.create_table(:skills) do |t|
|
22
|
-
t.integer :outdoor_bit_field, :default => 0
|
23
|
-
t.integer :indoor_bit_field, :default => 0
|
22
|
+
t.integer :outdoor_bit_field, :default => 0, :null => false
|
23
|
+
t.integer :indoor_bit_field, :default => 0, :null => false
|
24
24
|
end
|
25
25
|
|
26
26
|
class Skill < ActiveRecord::Base
|
@@ -89,7 +89,7 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
89
89
|
assert !p.reads_books?
|
90
90
|
end
|
91
91
|
|
92
|
-
def
|
92
|
+
def test_named_scopes_on_nullable_column
|
93
93
|
Person.delete_all
|
94
94
|
a = Person.new
|
95
95
|
a.plays_golf = true
|
@@ -101,9 +101,9 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
101
101
|
b.watches_tv = true
|
102
102
|
assert b.save
|
103
103
|
|
104
|
-
c = Person.create!
|
104
|
+
c = Person.create! :bit_field => 0
|
105
105
|
|
106
|
-
assert_equal [b], Person.likes_ice_cream.all(:order => "id")
|
106
|
+
assert_equal [b], Person.likes_ice_cream.all(:order => "id")
|
107
107
|
assert_equal [a,c], Person.not_likes_ice_cream.all(:order => "id")
|
108
108
|
|
109
109
|
assert_equal [a], Person.plays_golf.all(:order => "id")
|
@@ -116,6 +116,32 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
116
116
|
assert_equal [b,c], Person.not_reads_books.all(:order => "id")
|
117
117
|
end
|
118
118
|
|
119
|
+
def test_named_scopes_on_non_nullable_column
|
120
|
+
Skill.delete_all
|
121
|
+
a = Skill.new
|
122
|
+
a.plays_piano = true
|
123
|
+
a.chops_trees = true
|
124
|
+
a.mops_floors = true
|
125
|
+
assert a.save, a.errors.full_messages.join(", ")
|
126
|
+
|
127
|
+
b = Skill.new
|
128
|
+
b.plays_piano = true
|
129
|
+
b.chops_trees = true
|
130
|
+
b.makes_soup = true
|
131
|
+
assert b.save, b.errors.full_messages.join(", ")
|
132
|
+
|
133
|
+
c = Skill.create! :plays_piano => true, :chops_trees => true
|
134
|
+
|
135
|
+
assert_equal [a,b,c], Skill.plays_piano.all(:order => "id")
|
136
|
+
assert_equal [], Skill.not_plays_piano.all(:order => "id")
|
137
|
+
|
138
|
+
assert_equal [a], Skill.mops_floors.all(:order => "id")
|
139
|
+
assert_equal [b,c], Skill.not_mops_floors.all(:order => "id")
|
140
|
+
|
141
|
+
assert_equal [b], Skill.makes_soup.all(:order => "id")
|
142
|
+
assert_equal [a,c], Skill.not_makes_soup.all(:order => "id")
|
143
|
+
end
|
144
|
+
|
119
145
|
def test_dirty_attributes
|
120
146
|
Person.delete_all
|
121
147
|
a = Person.new
|
@@ -142,17 +168,17 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
142
168
|
assert !s.cuts_hedges
|
143
169
|
|
144
170
|
assert !s.valid?
|
145
|
-
assert s.errors
|
171
|
+
assert s.errors[:chops_trees]
|
146
172
|
|
147
173
|
s.chops_trees = false
|
148
174
|
assert !s.chops_trees?
|
149
175
|
assert !s.valid?
|
150
|
-
assert s.errors
|
176
|
+
assert s.errors[:chops_trees]
|
151
177
|
|
152
178
|
s.chops_trees = true
|
153
179
|
assert s.chops_trees?
|
154
180
|
assert s.valid?
|
155
|
-
assert !s.errors
|
181
|
+
assert !s.errors[:chops_trees]
|
156
182
|
assert s.save
|
157
183
|
end
|
158
184
|
|
@@ -166,17 +192,17 @@ class HasBitFieldTest < Test::Unit::TestCase
|
|
166
192
|
assert !s.makes_soup
|
167
193
|
|
168
194
|
assert !s.valid?
|
169
|
-
assert s.errors
|
195
|
+
assert s.errors[:plays_piano]
|
170
196
|
|
171
197
|
s.plays_piano = false
|
172
198
|
assert !s.plays_piano?
|
173
199
|
assert !s.valid?
|
174
|
-
assert s.errors
|
200
|
+
assert s.errors[:plays_piano]
|
175
201
|
|
176
202
|
s.plays_piano = true
|
177
203
|
assert s.plays_piano?
|
178
204
|
assert s.valid?
|
179
|
-
assert !s.errors
|
205
|
+
assert !s.errors[:plays_piano]
|
180
206
|
assert s.save
|
181
207
|
end
|
182
208
|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -4,10 +4,10 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.3.2
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Paul Barry
|
@@ -15,30 +15,60 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-11-23 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: sqlite3-ruby
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: activerecord
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 9
|
44
|
+
segments:
|
45
|
+
- 2
|
46
|
+
- 3
|
47
|
+
- 5
|
48
|
+
version: 2.3.5
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Provides an easy way to work with bit fields in active record
|
52
|
+
email:
|
53
|
+
- mail@paulbarry.com
|
24
54
|
executables: []
|
25
55
|
|
26
56
|
extensions: []
|
27
57
|
|
28
|
-
extra_rdoc_files:
|
29
|
-
|
30
|
-
- README.md
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
31
60
|
files:
|
32
61
|
- .document
|
33
62
|
- .gitignore
|
34
63
|
- .rvmrc
|
35
64
|
- Gemfile
|
65
|
+
- Gemfile.lock
|
36
66
|
- LICENSE
|
37
67
|
- README.md
|
38
68
|
- Rakefile
|
39
|
-
- VERSION
|
40
69
|
- has-bit-field.gemspec
|
41
70
|
- lib/has-bit-field.rb
|
71
|
+
- lib/has-bit-field/version.rb
|
42
72
|
- rails/init.rb
|
43
73
|
- test/has-bit-field_test.rb
|
44
74
|
- test/test_helper.rb
|
@@ -47,8 +77,8 @@ homepage: http://github.com/pjb3/has-bit-field
|
|
47
77
|
licenses: []
|
48
78
|
|
49
79
|
post_install_message:
|
50
|
-
rdoc_options:
|
51
|
-
|
80
|
+
rdoc_options: []
|
81
|
+
|
52
82
|
require_paths:
|
53
83
|
- lib
|
54
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -71,11 +101,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
101
|
version: "0"
|
72
102
|
requirements: []
|
73
103
|
|
74
|
-
rubyforge_project:
|
104
|
+
rubyforge_project: has-bit-field
|
75
105
|
rubygems_version: 1.3.7
|
76
106
|
signing_key:
|
77
107
|
specification_version: 3
|
78
|
-
summary: Provides an easy way
|
108
|
+
summary: Provides an easy way to work with bit fields in active record
|
79
109
|
test_files:
|
80
110
|
- test/has-bit-field_test.rb
|
81
111
|
- test/test_helper.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.3.2
|