pjb3-has-bit-field 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{has-bit-field}
5
- s.version = "0.1.1"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Paul Barry"]
9
- s.date = %q{2009-07-17}
9
+ s.date = %q{2009-07-19}
10
10
  s.email = %q{mail@paulbarry.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
data/lib/has-bit-field.rb CHANGED
@@ -5,20 +5,26 @@ 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
- flag = (1 << i)
8
+ define_method("#{field}_bit") do
9
+ (1 << i)
10
+ end
9
11
  define_method(field) do
10
- (send(bit_field_attribute) & flag) != 0
12
+ (send(bit_field_attribute) & send("#{field}_bit")) != 0
11
13
  end
12
14
  define_method("#{field}?") do
13
15
  send(field)
14
16
  end
15
17
  define_method("#{field}=") do |v|
16
18
  if v.to_s == "true" || v.to_s == "1"
17
- send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) | flag))
19
+ send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) | send("#{field}_bit")))
18
20
  else
19
- send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) & ~flag))
21
+ send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) & ~send("#{field}_bit")))
20
22
  end
21
23
  end
24
+ if(respond_to?(:named_scope))
25
+ named_scope field, :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) != 0", (1 << i)]
26
+ named_scope "not_#{field}", :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) = 0", (1 << i)]
27
+ end
22
28
  end
23
29
  end
24
30
  end
@@ -1,8 +1,23 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
- class Person
3
+ require 'rubygems'
4
+ require 'activerecord'
5
+ require File.join(File.dirname(__FILE__), "../rails/init")
6
+
7
+ ActiveRecord::Base.establish_connection(
8
+ :adapter => "sqlite3",
9
+ :database => ":memory:"
10
+ )
11
+
12
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
13
+
14
+ ActiveRecord::Base.connection.create_table(:people) do |t|
15
+ t.integer :bit_field, :default => 0
16
+ end
17
+
18
+ class Person < ActiveRecord::Base
4
19
  extend HasBitField
5
- attr_accessor :bit_field
20
+ #attr_accessor :bit_field
6
21
  has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
7
22
  end
8
23
 
@@ -14,6 +29,11 @@ class HasBitFieldTest < Test::Unit::TestCase
14
29
  assert p.respond_to?("#{f}="), "Expected #{p.inspect} to respond to #{f}="
15
30
  end
16
31
 
32
+ assert_equal p.likes_ice_cream_bit, (1 << 0)
33
+ assert_equal p.plays_golf_bit, (1 << 1)
34
+ assert_equal p.watches_tv_bit, (1 << 2)
35
+ assert_equal p.reads_books_bit, (1 << 3)
36
+
17
37
  p.likes_ice_cream = true
18
38
  assert p.likes_ice_cream?
19
39
  assert !p.plays_golf?
@@ -56,4 +76,30 @@ class HasBitFieldTest < Test::Unit::TestCase
56
76
  assert !p.reads_books?
57
77
  end
58
78
 
79
+ def test_named_scopes
80
+ a = Person.new
81
+ a.plays_golf = true
82
+ a.reads_books = true
83
+ assert a.save
84
+
85
+ b = Person.new
86
+ b.likes_ice_cream = true
87
+ b.watches_tv = true
88
+ assert b.save
89
+
90
+ c = Person.create!
91
+
92
+ assert_equal [b], Person.likes_ice_cream.all(:order => "id")
93
+ assert_equal [a,c], Person.not_likes_ice_cream.all(:order => "id")
94
+
95
+ assert_equal [a], Person.plays_golf.all(:order => "id")
96
+ assert_equal [b,c], Person.not_plays_golf.all(:order => "id")
97
+
98
+ assert_equal [b], Person.watches_tv.all(:order => "id")
99
+ assert_equal [a,c], Person.not_watches_tv.all(:order => "id")
100
+
101
+ assert_equal [a], Person.reads_books.all(:order => "id")
102
+ assert_equal [b,c], Person.not_reads_books.all(:order => "id")
103
+ end
104
+
59
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pjb3-has-bit-field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Barry
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-17 00:00:00 -07:00
12
+ date: 2009-07-19 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15