bitmask_attributes 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm use 1.9.2@bitmask_attributes --create --install
1
+ rvm use 1.9.3@bitmask_attributes --create --install
data/Gemfile CHANGED
@@ -6,6 +6,7 @@ if RUBY_VERSION < '1.9'
6
6
  gem "ruby-debug", ">= 0.10.3"
7
7
  end
8
8
 
9
+ gem 'rake'
9
10
  gem 'shoulda'
10
- gem 'sqlite3'
11
+ gem 'sqlite3', '>=1.3.4'
11
12
  gem 'turn'
@@ -1,37 +1,41 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bitmask_attributes (0.2.1)
4
+ bitmask_attributes (0.2.2)
5
5
  activerecord (~> 3.0)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- activemodel (3.0.9)
11
- activesupport (= 3.0.9)
12
- builder (~> 2.1.2)
13
- i18n (~> 0.5.0)
14
- activerecord (3.0.9)
15
- activemodel (= 3.0.9)
16
- activesupport (= 3.0.9)
17
- arel (~> 2.0.10)
18
- tzinfo (~> 0.3.23)
19
- activesupport (3.0.9)
10
+ activemodel (3.1.1)
11
+ activesupport (= 3.1.1)
12
+ builder (~> 3.0.0)
13
+ i18n (~> 0.6)
14
+ activerecord (3.1.1)
15
+ activemodel (= 3.1.1)
16
+ activesupport (= 3.1.1)
17
+ arel (~> 2.2.1)
18
+ tzinfo (~> 0.3.29)
19
+ activesupport (3.1.1)
20
+ multi_json (~> 1.0)
20
21
  ansi (1.3.0)
21
- arel (2.0.10)
22
- builder (2.1.2)
23
- i18n (0.5.0)
22
+ arel (2.2.1)
23
+ builder (3.0.0)
24
+ i18n (0.6.0)
25
+ multi_json (1.0.3)
26
+ rake (0.9.2.2)
24
27
  shoulda (2.11.3)
25
- sqlite3 (1.3.3)
28
+ sqlite3 (1.3.4)
26
29
  turn (0.8.2)
27
30
  ansi (>= 1.2.2)
28
- tzinfo (0.3.29)
31
+ tzinfo (0.3.30)
29
32
 
30
33
  PLATFORMS
31
34
  ruby
32
35
 
33
36
  DEPENDENCIES
34
37
  bitmask_attributes!
38
+ rake
35
39
  shoulda
36
- sqlite3
40
+ sqlite3 (>= 1.3.4)
37
41
  turn
data/README.md CHANGED
@@ -77,8 +77,15 @@ Find records without any bitmask set:
77
77
 
78
78
  User.without_roles
79
79
  # => (all users without a role)
80
+ User.no_roles
81
+ # => (all users without a role)
82
+
83
+ Find records without a specific attribute.
84
+
85
+ User.without_roles(:editor)
86
+ # => (all users who are not editors)
80
87
 
81
- Later we'll support finding records without a specific bitmask.
88
+ Note that "without_" only supports a single attribute argument, and the "no_" method does not support arguments.
82
89
 
83
90
  Adding Methods
84
91
  --------------
@@ -109,35 +109,44 @@ module BitmaskAttributes
109
109
  scope :with_#{attribute},
110
110
  proc { |*values|
111
111
  if values.blank?
112
- {:conditions => '#{attribute} > 0 OR #{attribute} IS NOT NULL'}
112
+ where('#{attribute} > 0 OR #{attribute} IS NOT NULL')
113
113
  else
114
114
  sets = values.map do |value|
115
115
  mask = #{model}.bitmask_for_#{attribute}(value)
116
116
  "#{attribute} & \#{mask} <> 0"
117
117
  end
118
- {:conditions => sets.join(' AND ')}
118
+ where(sets.join(' AND '))
119
119
  end
120
120
  }
121
- scope :without_#{attribute}, :conditions => "#{attribute} = 0 OR #{attribute} IS NULL"
122
- scope :no_#{attribute}, :conditions => "#{attribute} = 0 OR #{attribute} IS NULL"
121
+ scope :without_#{attribute},
122
+ proc { |value|
123
+ if value
124
+ mask = #{model}.bitmask_for_#{attribute}(value)
125
+ where("#{attribute} IS NULL OR #{attribute} & ? = 0", mask)
126
+ else
127
+ where("#{attribute} IS NULL OR #{attribute} = 0")
128
+ end
129
+ }
130
+
131
+ scope :no_#{attribute}, where("#{attribute} = 0 OR #{attribute} IS NULL")
123
132
 
124
133
  scope :with_any_#{attribute},
125
134
  proc { |*values|
126
135
  if values.blank?
127
- {:conditions => '#{attribute} > 0 OR #{attribute} IS NOT NULL'}
136
+ where('#{attribute} > 0 OR #{attribute} IS NOT NULL')
128
137
  else
129
138
  sets = values.map do |value|
130
139
  mask = #{model}.bitmask_for_#{attribute}(value)
131
140
  "#{attribute} & \#{mask} <> 0"
132
141
  end
133
- {:conditions => sets.join(' OR ')}
142
+ where(sets.join(' OR '))
134
143
  end
135
144
  }
136
145
  )
137
146
  values.each do |value|
138
147
  model.class_eval %(
139
148
  scope :#{attribute}_for_#{value},
140
- :conditions => ['#{attribute} & ? <> 0', #{model}.bitmask_for_#{attribute}(:#{value})]
149
+ where('#{attribute} & ? <> 0', #{model}.bitmask_for_#{attribute}(:#{value}))
141
150
  )
142
151
  end
143
152
  end
@@ -13,6 +13,7 @@ module BitmaskAttributes
13
13
  # = OVERRIDE TO SERIALIZE =
14
14
  # =========================
15
15
 
16
+ alias_method :orig_replace, :replace
16
17
  %w(push << delete replace reject! select!).each do |override|
17
18
  class_eval(<<-EOEVAL)
18
19
  def #{override}(*args)
@@ -39,8 +40,13 @@ module BitmaskAttributes
39
40
  end
40
41
  end
41
42
 
43
+ def symbolize!
44
+ orig_replace(map(&:to_sym))
45
+ end
46
+
42
47
  def updated!
43
48
  validate!
49
+ symbolize!
44
50
  uniq!
45
51
  serialize!
46
52
  end
@@ -1,3 +1,3 @@
1
1
  module BitmaskAttributes
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -34,7 +34,10 @@ class BitmaskAttributesTest < ActiveSupport::TestCase
34
34
  assert_stored campaign, :web, :print, :phone
35
35
  campaign.medium << :phone
36
36
  assert_stored campaign, :web, :print, :phone
37
+ campaign.medium << "phone"
38
+ assert_stored campaign, :web, :print, :phone
37
39
  assert_equal 1, campaign.medium.select { |value| value == :phone }.size
40
+ assert_equal 0, campaign.medium.select { |value| value == "phone" }.size
38
41
  end
39
42
 
40
43
  should "can assign new values at once to bitmask" do
@@ -152,6 +155,11 @@ class BitmaskAttributesTest < ActiveSupport::TestCase
152
155
 
153
156
  should "support retrieval for no values" do
154
157
  assert_equal [@campaign2], @company.campaigns.without_medium
158
+ assert_equal [@campaign2], @company.campaigns.no_medium
159
+ end
160
+
161
+ should "support retrieval without a specific value" do
162
+ assert_equal [@campaign2, @campaign3], @company.campaigns.without_medium(:print)
155
163
  end
156
164
  end
157
165
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitmask_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-04 00:00:00.000000000Z
12
+ date: 2011-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2153477300 !ruby/object:Gem::Requirement
16
+ requirement: &70135687914640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153477300
24
+ version_requirements: *70135687914640
25
25
  description: Simple bitmask attribute support for ActiveRecord
26
26
  email: joel@developwithstyle.com
27
27
  executables: []
@@ -65,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  version: '0'
66
66
  requirements: []
67
67
  rubyforge_project:
68
- rubygems_version: 1.8.6
68
+ rubygems_version: 1.8.10
69
69
  signing_key:
70
70
  specification_version: 3
71
71
  summary: Simple bitmask attribute support for ActiveRecord