Skip to content

Commit

Permalink
Version 0.3.1
Browse files Browse the repository at this point in the history
* Will now work with or without a default value on the bit field
* Added Bundler to ease setup
  • Loading branch information
pjb3 committed Aug 3, 2010
1 parent 8b22fe4 commit d71c9ed
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@
coverage
rdoc
pkg
.bundle
5 changes: 5 additions & 0 deletions Gemfile
@@ -0,0 +1,5 @@
source "http://rubygems.org"

gem "activerecord"
gem "jeweler"
gem "sqlite3-ruby"
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.3.0
0.3.1
6 changes: 3 additions & 3 deletions lib/has-bit-field.rb
Expand Up @@ -9,7 +9,7 @@ def has_bit_field(bit_field_attribute, *args)
(1 << i)
end
define_method(field) do
(send(bit_field_attribute) & self.class.send("#{field}_bit")) != 0
(send(bit_field_attribute).to_i & self.class.send("#{field}_bit")) != 0
end
define_method("#{field}?") do
send(field)
Expand All @@ -28,8 +28,8 @@ def has_bit_field(bit_field_attribute, *args)
send(field) != send("#{field}_was")
end
if(respond_to?(:named_scope))
named_scope field, :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) != 0", send("#{field}_bit")]
named_scope "not_#{field}", :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) = 0", send("#{field}_bit")]
named_scope field, :conditions => ["#{table_name}.#{bit_field_attribute} IS NOT NULL AND (#{table_name}.#{bit_field_attribute} & ?) != 0", send("#{field}_bit")]
named_scope "not_#{field}", :conditions => ["#{table_name}.#{bit_field_attribute} IS NULL OR (#{table_name}.#{bit_field_attribute} & ?) = 0", send("#{field}_bit")]
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions test/has-bit-field_test.rb
@@ -1,9 +1,5 @@
require File.join(File.dirname(__FILE__), 'test_helper')

require 'rubygems'
require 'activerecord'
require File.join(File.dirname(__FILE__), "../rails/init")

ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
Expand All @@ -12,7 +8,7 @@
#ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Base.connection.create_table(:people) do |t|
t.integer :bit_field, :default => 0
t.integer :bit_field
end

class Person < ActiveRecord::Base
Expand Down
6 changes: 6 additions & 0 deletions test/test_helper.rb
@@ -1,5 +1,11 @@
require 'rubygems'
require 'bundler'
Bundler.setup

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'test/unit'
require 'active_record'
require File.join(File.dirname(__FILE__), "../rails/init")
require 'has-bit-field'

0 comments on commit d71c9ed

Please sign in to comment.