has_many_booleans 0.9.1 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -49,6 +49,7 @@ The <tt>:append</tt> option lets you modify the suffix to append to the boolean
49
49
  :lazy => false,
50
50
  :self => 'model_available',
51
51
  :self_value => true,
52
+ :unkown_value => false,
52
53
  end
53
54
 
54
55
  The <tt>:field</tt> option lets you change the database field in which the integer gets stored (default is +booleans+).
@@ -57,6 +58,10 @@ When the <tt>:lazy</tt> option is set to +false+, the bitset integer gets change
57
58
 
58
59
  The <tt>:self</tt> option is just another virtual boolean, which's method name you can freely assign.
59
60
 
61
+ The <tt>:unknown_value</tt> is the new value for booleans, which are assigned a new value, but it is not in the <tt>:false_values</tt> or <tt>:true_values</tt> option arrays. Default is +true+, set it to +false+ to get ActiveRecord behaviour.
62
+
63
+ The default <tt>false_</tt> and <tt>true_values</tt> are the same as in ActiveRecord.
64
+
60
65
  === Scopes
61
66
  The plugin also generates a <tt>.true</tt> and a <tt>.false</tt> scope for the model. You have to pass a boolean name as parameter to filter for this value. If you pass multiple boolean names, they get connected with 'or'. To get an 'and' condition, chain multiple scopes. If you don't pass any boolean names (or +nil+), the special <tt>:self</tt> boolean is meant.
62
67
 
data/Rakefile CHANGED
@@ -27,7 +27,7 @@ end
27
27
  PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*', 'rails/**/*', 'test/**/*' ]
28
28
  spec = Gem::Specification.new do |s|
29
29
  s.name = "has_many_booleans"
30
- s.version = "0.9.1"
30
+ s.version = "0.9.3"
31
31
  s.author = "Jan Lelis"
32
32
  s.email = "mail@janlelis.de"
33
33
  s.homepage = "http://github.com/janlelis/has_many_booleans"
@@ -5,8 +5,8 @@ require 'has_many_booleans/simple_bitset'
5
5
  module HasManyBooleans #:nodoc:
6
6
  RAILS2 = if ENV['RAILS_GEM_VERSION']
7
7
  ENV['RAILS_GEM_VERSION'] < '3'
8
- else
9
- true
8
+ else
9
+ Rails::VERSION::STRING < '3'
10
10
  end
11
11
 
12
12
  module ClassMethods
@@ -35,7 +35,9 @@ else
35
35
  #[<tt>:append</tt>] The name to append to the listed booleans. The underscore is added automatically. +nil+ is also possible. Default is +activated+.
36
36
  #[<tt>:field</tt>] The database field used. Defaults to +booleans+.
37
37
  #[<tt>:suffixes</tt>] Specifies, which "alias" methods are created. Defaults to <tt>["?", "=", "!"]</tt>. You cannot add new ones, you can only forbid some of them.
38
- #[<tt>:false_values</tt>] All the values in the array can be used to set a boolean to +false+ (when used with the <tt>=</tt> method). <b>Example:</b> Set this to <tt>["0"]</tt> and then call <tt>some_boolean_activated = "0"</tt>, it will set the boolean to +false+. Default settings is +false+ (deactivated).
38
+ #[<tt>:false_values</tt>] All the values in the array can be used to set a boolean to +false+ (when used with the <tt>=</tt> method). <b>Example:</b> Set this to <tt>["0"]</tt> and then call <tt>some_boolean_activated = "0"</tt>, it will set the boolean to +false+. By default, this is set to ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.
39
+ #[<tt>:true_values</tt>] Which values should be true (if <tt>:unkown_value</tt> is set to +false+) Default is ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.
40
+ #[<tt>:unkown_value</tt>] What should be the value of a boolean, if it is not found in the <tt>:false_</tt> or <tt>:true_values</tt>? Default is +true+, set this to +false+ to get ActiveRecord behaviour.
39
41
  #[<tt>:lazy</tt>] When the <tt>:lazy</tt> option is set to +false+, the bitset integer gets changed every time you assign a new value for a boolean. The default setting is +true+, which means, the integer gets only updated when the object is saved.
40
42
  #[<tt>:self</tt>] This is just another virtual boolean. You can freely assign the name. It is always stored as first bit in the bitset integer (so if the bitset integer is odd, this special boolean is set). You can also set this to +true+, which means, the <tt>:append</tt> value is used as method name. Default: +false+.
41
43
  #[<tt>:self_value</tt>] The default value for the special <tt>:self</tt> boolean above.
@@ -161,7 +163,9 @@ else
161
163
  :field => 'booleans',
162
164
  :suffixes => %w| ! = ? |,
163
165
  :true => [],
164
- :false_values => [], # e.g. [0, "0", ""],
166
+ :false_values => ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES,
167
+ :true_values => ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES,
168
+ :unknown_value => true,
165
169
  :self => false,
166
170
  :self_value => false,
167
171
  :lazy => true,
@@ -269,8 +273,7 @@ else
269
273
  @booleans_data[name][1] = true
270
274
  save_booleans if !self.class.booleans_options[:lazy]
271
275
  when '='
272
- @booleans_data[name][1] =
273
- !!( new_value[0] && !self.class.booleans_options[:false_values].member?(new_value[0]) )
276
+ @booleans_data[name][1] = set_value new_value[0]
274
277
  save_booleans if !self.class.booleans_options[:lazy]
275
278
  end
276
279
  @booleans_data[name][1]
@@ -278,6 +281,17 @@ else
278
281
  end
279
282
  end
280
283
 
284
+ def set_value(v)
285
+ case
286
+ when !v || self.class.booleans_options[:false_values].member?(v)
287
+ false
288
+ when v == true || self.class.booleans_options[:true_values].member?(v)
289
+ true
290
+ else
291
+ self.class.booleans_options[:unknown_value]
292
+ end
293
+ end
294
+
281
295
  def each_suffix(&block)
282
296
  ( [''] + self.class.booleans_options[:suffixes] ).each{ |suffix|
283
297
  yield suffix
@@ -286,11 +300,36 @@ else
286
300
 
287
301
 
288
302
 
303
+
289
304
  # hook in callbacks part 2 (to not overwrite after_initialize)
290
- #TODO run callback after booleans have been fetched
291
- def initialize(*args, &block) # :nodoc:
292
- super *args, &block
305
+ # TODO someday: run callback after booleans have been fetched
306
+ def initialize(attributes = nil) # :nodoc:
307
+
308
+ if init_callback_defined = respond_to?(:after_initialize)
309
+ instance_eval do
310
+ alias tmp_after_initialize after_initialize
311
+ undef after_initialize
312
+ end
313
+ end
314
+
315
+ super()
293
316
  initialize_booleans
317
+ self.attributes = attributes unless attributes.nil?
318
+ result = yield self if block_given?
319
+
320
+ if init_callback_defined
321
+ instance_eval do
322
+ alias after_initialize tmp_after_initialize
323
+ undef tmp_after_initialize
324
+ if RAILS2
325
+ callback(:after_initialize)
326
+ else
327
+ _run_initialize_callbacks
328
+ end
329
+ end
330
+ end
331
+
332
+ result
294
333
  end
295
334
 
296
335
  if respond_to? :initialize_copy