has-bit-field 0.3.1

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activerecord"
4
+ gem "jeweler"
5
+ gem "sqlite3-ruby"
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Paul Barry
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ has-bit-field
2
+ =============
3
+
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
+
6
+ To use this with Active Record, you would first require this gem in `config/environment.rb`:
7
+
8
+ config.gem "pjb3-has-bit-field", :lib => "has-bit-field", :source => "http://gems.github.com"
9
+
10
+ Now in one of your models, you define a bit field like this:
11
+
12
+ class Person < ActiveRecord::Base
13
+ has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
14
+ end
15
+
16
+ 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:
17
+
18
+ $ script/console
19
+ Loading development environment (Rails 2.3.2)
20
+ p =>> p = Person.new
21
+ => #<Person id: nil, bit_field: nil, created_at: nil, updated_at: nil>
22
+ >> p.likes_ice_cream = "true"
23
+ => "true"
24
+ >> p.plays_golf = 1
25
+ => 1
26
+ >> p.save
27
+ => true
28
+ >> p = Person.find(p.id)
29
+ => #<Person id: 1, bit_field: 3, created_at: "2009-07-18 03:04:06", updated_at: "2009-07-18 03:04:06">
30
+ >> p.likes_ice_cream?
31
+ => true
32
+ >> p.reads_books?
33
+ => false
34
+ >> Person.plays_golf_bit
35
+ => 4
36
+ >> p = Person.likes_ice_cream.first
37
+ => #<Person id: 1, bit_field: 3, created_at: "2009-07-18 03:04:06", updated_at: "2009-07-18 03:04:06">
38
+
39
+ 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
+
41
+ class Person < ActiveRecord::Base
42
+ has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books, :nut_allergy
43
+ end
44
+
45
+ The new flag will be evaluated as false for existing rows on the database table until their values are explicitly set. Be careful with the peanuts!
46
+
47
+ Copyright
48
+ ---------
49
+
50
+ Copyright (c) 2009 Paul Barry. See LICENSE for details.
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
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
17
+
18
+ require 'rake/testtask'
19
+ Rake::TestTask.new(:test) do |test|
20
+ test.libs << 'lib' << 'test'
21
+ test.pattern = 'test/**/*_test.rb'
22
+ test.verbose = true
23
+ end
24
+
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
+ 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/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.1
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{has-bit-field}
8
+ s.version = "0.3.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Barry"]
12
+ s.date = %q{2010-08-03}
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
+ "Gemfile",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "has-bit-field.gemspec",
27
+ "lib/has-bit-field.rb",
28
+ "rails/init.rb",
29
+ "test/has-bit-field_test.rb",
30
+ "test/test_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/pjb3/has-bit-field}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Provides an easy way for dealing with bit fields and active record}
37
+ s.test_files = [
38
+ "test/has-bit-field_test.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
52
+
@@ -0,0 +1,36 @@
1
+ module HasBitField
2
+ # The first arguement +bit_field_attribute+ should be a symbol,
3
+ # the name of attribute that will hold the actual bit field
4
+ # all following arguments should also be symbols,
5
+ # which will be the name of each flag in the bit field
6
+ def has_bit_field(bit_field_attribute, *args)
7
+ args.each_with_index do |field,i|
8
+ (class << self; self end).send(:define_method, "#{field}_bit") do
9
+ (1 << i)
10
+ end
11
+ define_method(field) do
12
+ (send(bit_field_attribute).to_i & self.class.send("#{field}_bit")) != 0
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")))
22
+ end
23
+ end
24
+ define_method("#{field}_was") do
25
+ (send("#{bit_field_attribute}_was") & self.class.send("#{field}_bit")) != 0
26
+ end
27
+ define_method("#{field}_changed?") do
28
+ send(field) != send("#{field}_was")
29
+ end
30
+ if(respond_to?(:named_scope))
31
+ named_scope field, :conditions => ["#{table_name}.#{bit_field_attribute} IS NOT NULL AND (#{table_name}.#{bit_field_attribute} & ?) != 0", send("#{field}_bit")]
32
+ named_scope "not_#{field}", :conditions => ["#{table_name}.#{bit_field_attribute} IS NULL OR (#{table_name}.#{bit_field_attribute} & ?) = 0", send("#{field}_bit")]
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,2 @@
1
+ require 'has-bit-field'
2
+ ActiveRecord::Base.extend HasBitField
@@ -0,0 +1,116 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ :adapter => "sqlite3",
5
+ :database => ":memory:"
6
+ )
7
+
8
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
9
+
10
+ ActiveRecord::Base.connection.create_table(:people) do |t|
11
+ t.integer :bit_field
12
+ end
13
+
14
+ class Person < ActiveRecord::Base
15
+ extend HasBitField
16
+ #attr_accessor :bit_field
17
+ has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
18
+ end
19
+
20
+ class HasBitFieldTest < Test::Unit::TestCase
21
+ def test_bit_field
22
+ p = Person.new
23
+ [:likes_ice_cream, :plays_golf, :watches_tv, :reads_books].each do |f|
24
+ assert p.respond_to?("#{f}?"), "Expected #{p.inspect} to respond to #{f}?"
25
+ assert p.respond_to?("#{f}="), "Expected #{p.inspect} to respond to #{f}="
26
+ end
27
+
28
+ assert_equal Person.likes_ice_cream_bit, (1 << 0)
29
+ assert_equal Person.plays_golf_bit, (1 << 1)
30
+ assert_equal Person.watches_tv_bit, (1 << 2)
31
+ assert_equal Person.reads_books_bit, (1 << 3)
32
+
33
+ p.likes_ice_cream = true
34
+ assert p.likes_ice_cream?
35
+ assert !p.plays_golf?
36
+ assert !p.watches_tv?
37
+ assert !p.reads_books?
38
+
39
+ p.likes_ice_cream = true
40
+ assert p.likes_ice_cream?
41
+ assert !p.plays_golf?
42
+ assert !p.watches_tv?
43
+ assert !p.reads_books?
44
+
45
+ p.watches_tv = true
46
+ assert p.likes_ice_cream
47
+ assert !p.plays_golf
48
+ assert p.watches_tv
49
+ assert !p.reads_books
50
+
51
+ p.watches_tv = false
52
+ p.plays_golf = true
53
+ assert p.likes_ice_cream?
54
+ assert p.plays_golf?
55
+ assert !p.watches_tv?
56
+ assert !p.reads_books?
57
+
58
+ p.watches_tv = "1"
59
+ p.reads_books = "true"
60
+ assert p.likes_ice_cream?
61
+ assert p.plays_golf?
62
+ assert p.watches_tv?
63
+ assert p.reads_books?
64
+
65
+ p.likes_ice_cream = nil
66
+ p.plays_golf = 0
67
+ p.watches_tv = "0"
68
+ p.reads_books = "false"
69
+ assert !p.likes_ice_cream?
70
+ assert !p.plays_golf?
71
+ assert !p.watches_tv?
72
+ assert !p.reads_books?
73
+ end
74
+
75
+ def test_named_scopes
76
+ Person.delete_all
77
+ a = Person.new
78
+ a.plays_golf = true
79
+ a.reads_books = true
80
+ assert a.save
81
+
82
+ b = Person.new
83
+ b.likes_ice_cream = true
84
+ b.watches_tv = true
85
+ assert b.save
86
+
87
+ c = Person.create!
88
+
89
+ assert_equal [b], Person.likes_ice_cream.all(:order => "id")
90
+ assert_equal [a,c], Person.not_likes_ice_cream.all(:order => "id")
91
+
92
+ assert_equal [a], Person.plays_golf.all(:order => "id")
93
+ assert_equal [b,c], Person.not_plays_golf.all(:order => "id")
94
+
95
+ assert_equal [b], Person.watches_tv.all(:order => "id")
96
+ assert_equal [a,c], Person.not_watches_tv.all(:order => "id")
97
+
98
+ assert_equal [a], Person.reads_books.all(:order => "id")
99
+ assert_equal [b,c], Person.not_reads_books.all(:order => "id")
100
+ end
101
+ def test_dirty_attributes
102
+ Person.delete_all
103
+ a = Person.new
104
+ a.plays_golf = true
105
+ a.reads_books = true
106
+ assert a.save
107
+
108
+ a.plays_golf = false
109
+ assert_equal false, a.plays_golf
110
+ assert_equal true, a.plays_golf_was
111
+ assert a.plays_golf_changed?
112
+ assert_equal true, a.reads_books
113
+ assert_equal true, a.reads_books_was
114
+ assert !a.reads_books_changed?
115
+ end
116
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ require 'test/unit'
9
+ require 'active_record'
10
+ require File.join(File.dirname(__FILE__), "../rails/init")
11
+ require 'has-bit-field'
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has-bit-field
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 1
10
+ version: 0.3.1
11
+ platform: ruby
12
+ authors:
13
+ - Paul Barry
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-03 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: mail@paulbarry.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README.md
31
+ files:
32
+ - .document
33
+ - .gitignore
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - VERSION
39
+ - has-bit-field.gemspec
40
+ - lib/has-bit-field.rb
41
+ - rails/init.rb
42
+ - test/has-bit-field_test.rb
43
+ - test/test_helper.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/pjb3/has-bit-field
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Provides an easy way for dealing with bit fields and active record
78
+ test_files:
79
+ - test/has-bit-field_test.rb
80
+ - test/test_helper.rb