pjb3-has-bit-field 0.2.0 → 0.2.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.
data/README.md CHANGED
@@ -13,7 +13,7 @@ Now in one of your models, you define a bit field like this:
13
13
  has_bit_field :bit_field, :likes_ice_cream, :plays_golf, :watches_tv, :reads_books
14
14
  end
15
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:
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
17
 
18
18
  $ script/console
19
19
  Loading development environment (Rails 2.3.2)
@@ -31,7 +31,11 @@ This means that your database will have an integer column called `bit_field` whi
31
31
  => true
32
32
  >> p.reads_books?
33
33
  => false
34
-
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
+
35
39
 
36
40
  Copyright
37
41
  ---------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{has-bit-field}
5
- s.version = "0.2.0"
5
+ s.version = "0.2.1"
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"]
data/lib/has-bit-field.rb CHANGED
@@ -5,25 +5,25 @@ 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
- define_method("#{field}_bit") do
8
+ (class << self; self end).send(:define_method, "#{field}_bit") do
9
9
  (1 << i)
10
10
  end
11
11
  define_method(field) do
12
- (send(bit_field_attribute) & send("#{field}_bit")) != 0
12
+ (send(bit_field_attribute) & self.class.send("#{field}_bit")) != 0
13
13
  end
14
14
  define_method("#{field}?") do
15
15
  send(field)
16
16
  end
17
17
  define_method("#{field}=") do |v|
18
18
  if v.to_s == "true" || v.to_s == "1"
19
- send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) | send("#{field}_bit")))
19
+ send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) | self.class.send("#{field}_bit")))
20
20
  else
21
- send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) & ~send("#{field}_bit")))
21
+ send("#{bit_field_attribute}=", ((send(bit_field_attribute) || 0) & ~self.class.send("#{field}_bit")))
22
22
  end
23
23
  end
24
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)]
25
+ named_scope field, :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) != 0", send("#{field}_bit")]
26
+ named_scope "not_#{field}", :conditions => ["(#{table_name}.#{bit_field_attribute} & ?) = 0", send("#{field}_bit")]
27
27
  end
28
28
  end
29
29
  end
@@ -29,10 +29,10 @@ class HasBitFieldTest < Test::Unit::TestCase
29
29
  assert p.respond_to?("#{f}="), "Expected #{p.inspect} to respond to #{f}="
30
30
  end
31
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)
32
+ assert_equal Person.likes_ice_cream_bit, (1 << 0)
33
+ assert_equal Person.plays_golf_bit, (1 << 1)
34
+ assert_equal Person.watches_tv_bit, (1 << 2)
35
+ assert_equal Person.reads_books_bit, (1 << 3)
36
36
 
37
37
  p.likes_ice_cream = true
38
38
  assert p.likes_ice_cream?
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Barry