pjb3-has-bit-field 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
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.
data/README.md ADDED
@@ -0,0 +1,39 @@
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. 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
+
35
+
36
+ Copyright
37
+ ---------
38
+
39
+ Copyright (c) 2009 Paul Barry. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
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
+ gem.files << "rails/init.rb"
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION.yml')
46
+ config = YAML.load(File.read('VERSION.yml'))
47
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "has-bit-field #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
57
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,21 @@
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
+ flag = (1 << i)
9
+ define_method("#{field}?") do
10
+ (send(bit_field_attribute) & flag) != 0
11
+ end
12
+ define_method("#{field}=") do |v|
13
+ if v.to_s == "true" || v.to_s == "1"
14
+ send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) | flag))
15
+ else
16
+ send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) & ~flag))
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ ActiveRecord::Base.extend(HasBitField)
@@ -0,0 +1,59 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class Person
4
+ extend HasBitField
5
+ attr_accessor :bit_field
6
+ has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
7
+ end
8
+
9
+ class HasBitFieldTest < Test::Unit::TestCase
10
+ def test_bit_field
11
+ p = Person.new
12
+ [:likes_ice_cream, :plays_golf, :watches_tv, :reads_books].each do |f|
13
+ assert p.respond_to?("#{f}?"), "Expected #{p.inspect} to respond to #{f}?"
14
+ assert p.respond_to?("#{f}="), "Expected #{p.inspect} to respond to #{f}="
15
+ end
16
+
17
+ p.likes_ice_cream = true
18
+ assert p.likes_ice_cream?
19
+ assert !p.plays_golf?
20
+ assert !p.watches_tv?
21
+ assert !p.reads_books?
22
+
23
+ p.likes_ice_cream = true
24
+ assert p.likes_ice_cream?
25
+ assert !p.plays_golf?
26
+ assert !p.watches_tv?
27
+ assert !p.reads_books?
28
+
29
+ p.watches_tv = true
30
+ assert p.likes_ice_cream?
31
+ assert !p.plays_golf?
32
+ assert p.watches_tv?
33
+ assert !p.reads_books?
34
+
35
+ p.watches_tv = false
36
+ p.plays_golf = true
37
+ assert p.likes_ice_cream?
38
+ assert p.plays_golf?
39
+ assert !p.watches_tv?
40
+ assert !p.reads_books?
41
+
42
+ p.watches_tv = "1"
43
+ p.reads_books = "true"
44
+ assert p.likes_ice_cream?
45
+ assert p.plays_golf?
46
+ assert p.watches_tv?
47
+ assert p.reads_books?
48
+
49
+ p.likes_ice_cream = nil
50
+ p.plays_golf = 0
51
+ p.watches_tv = "0"
52
+ p.reads_books = "false"
53
+ assert !p.likes_ice_cream?
54
+ assert !p.plays_golf?
55
+ assert !p.watches_tv?
56
+ assert !p.reads_books?
57
+ end
58
+
59
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'test/unit'
5
+ require 'has-bit-field'
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pjb3-has-bit-field
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Barry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-17 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: mail@paulbarry.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.md
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - Rakefile
30
+ - VERSION
31
+ - lib/has-bit-field.rb
32
+ - rails/init.rb
33
+ - test/has-bit-field_test.rb
34
+ - test/test_helper.rb
35
+ - README.md
36
+ has_rdoc: true
37
+ homepage: http://github.com/pjb3/has-bit-field
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Provides an easy way for dealing with bit fields and active record
62
+ test_files:
63
+ - test/has-bit-field_test.rb
64
+ - test/test_helper.rb