Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added raise_on_typecast_failure switch. If set to false, it prevents …

…from

raising an error when trying to typecast nil to column which cannot be NULL.
Defaults to true.
  • Loading branch information...
commit 4479548d2a5b71e2c0460f953f6c742fbb53091b 1 parent e8cd3ee
authored
9  lib/sequel_model.rb
@@ -50,8 +50,9 @@ def self.Model(source)
50 50
   #   cache_store, cache_ttl, dataset_methods, primary_key, restricted_columns,
51 51
   #   sti_dataset, and sti_key.  You should not usually need to
52 52
   #   access these directly.
53  
-  # * The following class level attr_accessors are created: raise_on_save_failure,
54  
-  #   strict_param_setting, typecast_empty_string_to_nil, and typecast_on_assignment:
  53
+  # * The following class level attr_accessors are created: raise_on_typecast_failure,
  54
+  #   raise_on_save_failure, strict_param_setting, typecast_empty_string_to_nil,
  55
+  #   and typecast_on_assignment:
55 56
   #
56 57
   #     # Don't raise an error if a validation attempt fails in
57 58
   #     # save/create/save_changes/etc.
@@ -72,6 +73,10 @@ def self.Model(source)
72 73
   #     Model.typecast_empty_string_to_nil = false
73 74
   #     m.number = ''
74 75
   #     m.number # => '' instead of nil
  76
+  #     # Don't raise if typecasting to nil but column cannot be null
  77
+  #     Model.typecast_empty_string_to_nil = true
  78
+  #     Model.raise_on_typecast_failure = false
  79
+  #     m.not_null_column = '' # => nil
75 80
   #
76 81
   # * The following class level method aliases are defined:
77 82
   #   * Model.dataset= => set_dataset
8  lib/sequel_model/base.rb
@@ -16,6 +16,7 @@ class Model
16 16
     @strict_param_setting = true
17 17
     @typecast_empty_string_to_nil = true
18 18
     @typecast_on_assignment = true
  19
+    @raise_on_typecast_failure = true
19 20
 
20 21
     # Which columns should be the only columns allowed in a call to set
21 22
     # (default: all columns).
@@ -32,6 +33,10 @@ class Model
32 33
     # to save/create/save_changes/etc.
33 34
     metaattr_accessor :raise_on_save_failure
34 35
 
  36
+    # Whether to raise an error if typecasting nil to a column with NULLs not
  37
+    # allowed (default: true)
  38
+    metaattr_accessor :raise_on_typecast_failure
  39
+
35 40
     # Which columns should not be update in a call to set
36 41
     # (default: no columns).
37 42
     metaattr_reader :restricted_columns
@@ -70,7 +75,8 @@ class Model
70 75
       :@cache_ttl=>nil, :@dataset_methods=>:dup, :@primary_key=>nil, 
71 76
       :@raise_on_save_failure=>nil, :@restricted_columns=>:dup, :@restrict_primary_key=>nil,
72 77
       :@sti_dataset=>nil, :@sti_key=>nil, :@strict_param_setting=>nil,
73  
-      :@typecast_empty_string_to_nil=>nil, :@typecast_on_assignment=>nil}
  78
+      :@typecast_empty_string_to_nil=>nil, :@typecast_on_assignment=>nil,
  79
+      :@raise_on_typecast_failure=>nil}
74 80
 
75 81
     # Returns the first record from the database matching the conditions.
76 82
     # If a hash is given, it is used as the conditions.  If another
7  lib/sequel_model/record.rb
@@ -17,6 +17,10 @@ class Model
17 17
     # returning nil on a failure to save/save_changes/etc.
18 18
     attr_writer :raise_on_save_failure
19 19
 
  20
+    # Whether this model instance should raise and error when attempting to
  21
+    # typecast nil to a column which has :null => false set.
  22
+    attr_writer :raise_on_typecast_failure
  23
+
20 24
     # Whether this model instance should raise an error if attempting
21 25
     # to call a method through set/update and their variants that either
22 26
     # doesn't exist or access to it is denied.
@@ -54,6 +58,7 @@ def initialize(values = nil, from_db = false, &block)
54 58
       @strict_param_setting = model.strict_param_setting
55 59
       @typecast_on_assignment = model.typecast_on_assignment
56 60
       @typecast_empty_string_to_nil = model.typecast_empty_string_to_nil
  61
+      @raise_on_typecast_failure = model.raise_on_typecast_failure
57 62
       if from_db
58 63
         @new = false
59 64
         @values = values
@@ -531,7 +536,7 @@ def setter_methods(only, except)
531 536
     def typecast_value(column, value)
532 537
       return value unless @typecast_on_assignment && @db_schema && (col_schema = @db_schema[column])
533 538
       value = nil if value == '' and @typecast_empty_string_to_nil and col_schema[:type] and ![:string, :blob].include?(col_schema[:type])
534  
-      raise(Error, "nil/NULL is not allowed for the #{column} column") if value.nil? && (col_schema[:allow_null] == false)
  539
+      raise(Error, "nil/NULL is not allowed for the #{column} column") if @raise_on_typecast_failure && value.nil? && (col_schema[:allow_null] == false)
535 540
       model.db.typecast_value(col_schema[:type], value)
536 541
     end
537 542
 
11  spec/sequel_model/record_spec.rb
@@ -930,6 +930,17 @@ def ds.insert_select(hash)
930 930
     m.x.should == nil
931 931
   end
932 932
 
  933
+  specify "should not raise when typecasting nil to NOT NULL column but raise_on_typecast_failure is off" do
  934
+    @c.raise_on_typecast_failure = false
  935
+    @c.typecast_on_assignment = true
  936
+    @c.instance_variable_set(:@db_schema, {:x=>{:type=>:integer,:allow_null=>false}})
  937
+    m = @c.new
  938
+    m.x = ''
  939
+    m.x.should == nil
  940
+    m.x = nil
  941
+    m.x.should == nil
  942
+  end
  943
+
933 944
   specify "should raise an error if invalid data is used in an integer field" do
934 945
     @c.instance_variable_set(:@db_schema, {:x=>{:type=>:integer}})
935 946
     proc{@c.new.x = 'a'}.should raise_error

0 notes on commit 4479548

Please sign in to comment.
Something went wrong with that request. Please try again.