Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added _bit field and named scopes

  • Loading branch information...
commit 9a6af9cf8c4d776d57e80e86f305bc1688c5d93c 1 parent d99339f
@pjb3 authored
View
4 has-bit-field.gemspec
@@ -2,11 +2,11 @@
Gem::Specification.new do |s|
s.name = %q{has-bit-field}
- s.version = "0.1.1"
+ s.version = "0.2.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Paul Barry"]
- s.date = %q{2009-07-17}
+ s.date = %q{2009-07-19}
s.email = %q{mail@paulbarry.com}
s.extra_rdoc_files = [
"LICENSE",
View
14 lib/has-bit-field.rb
@@ -5,20 +5,26 @@ module HasBitField
# which will be the name of each flag in the bit field
def has_bit_field(bit_field_attribute, *args)
args.each_with_index do |field,i|
- flag = (1 << i)
+ define_method("#{field}_bit") do
+ (1 << i)
+ end
define_method(field) do
- (send(bit_field_attribute) & flag) != 0
+ (send(bit_field_attribute) & send("#{field}_bit")) != 0
end
define_method("#{field}?") do
send(field)
end
define_method("#{field}=") do |v|
if v.to_s == "true" || v.to_s == "1"
- send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) | flag))
+ send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) | send("#{field}_bit")))
else
- send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) & ~flag))
+ send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) & ~send("#{field}_bit")))
end
end
+ if(respond_to?(:named_scope))
+ named_scope field, :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) != 0", (1 << i)]
+ named_scope "not_#{field}", :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) = 0", (1 << i)]
+ end
end
end
end
View
50 test/has-bit-field_test.rb
@@ -1,8 +1,23 @@
require File.join(File.dirname(__FILE__), 'test_helper')
-class Person
+require 'rubygems'
+require 'activerecord'
+require File.join(File.dirname(__FILE__), "../rails/init")
+
+ActiveRecord::Base.establish_connection(
+ :adapter => "sqlite3",
+ :database => ":memory:"
+)
+
+#ActiveRecord::Base.logger = Logger.new(STDOUT)
+
+ActiveRecord::Base.connection.create_table(:people) do |t|
+ t.integer :bit_field, :default => 0
+end
+
+class Person < ActiveRecord::Base
extend HasBitField
- attr_accessor :bit_field
+ #attr_accessor :bit_field
has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
end
@@ -14,6 +29,11 @@ def test_bit_field
assert p.respond_to?("#{f}="), "Expected #{p.inspect} to respond to #{f}="
end
+ assert_equal p.likes_ice_cream_bit, (1 << 0)
+ assert_equal p.plays_golf_bit, (1 << 1)
+ assert_equal p.watches_tv_bit, (1 << 2)
+ assert_equal p.reads_books_bit, (1 << 3)
+
p.likes_ice_cream = true
assert p.likes_ice_cream?
assert !p.plays_golf?
@@ -56,4 +76,30 @@ def test_bit_field
assert !p.reads_books?
end
+ def test_named_scopes
+ a = Person.new
+ a.plays_golf = true
+ a.reads_books = true
+ assert a.save
+
+ b = Person.new
+ b.likes_ice_cream = true
+ b.watches_tv = true
+ assert b.save
+
+ c = Person.create!
+
+ assert_equal [b], Person.likes_ice_cream.all(:order => "id")
+ assert_equal [a,c], Person.not_likes_ice_cream.all(:order => "id")
+
+ assert_equal [a], Person.plays_golf.all(:order => "id")
+ assert_equal [b,c], Person.not_plays_golf.all(:order => "id")
+
+ assert_equal [b], Person.watches_tv.all(:order => "id")
+ assert_equal [a,c], Person.not_watches_tv.all(:order => "id")
+
+ assert_equal [a], Person.reads_books.all(:order => "id")
+ assert_equal [b,c], Person.not_reads_books.all(:order => "id")
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.