Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
Michal Bugno authored
View
9 lib/sequel_model.rb
@@ -50,8 +50,9 @@ def self.Model(source)
# cache_store, cache_ttl, dataset_methods, primary_key, restricted_columns,
# sti_dataset, and sti_key. You should not usually need to
# access these directly.
- # * The following class level attr_accessors are created: raise_on_save_failure,
- # strict_param_setting, typecast_empty_string_to_nil, and typecast_on_assignment:
+ # * The following class level attr_accessors are created: raise_on_typecast_failure,
+ # raise_on_save_failure, strict_param_setting, typecast_empty_string_to_nil,
+ # and typecast_on_assignment:
#
# # Don't raise an error if a validation attempt fails in
# # save/create/save_changes/etc.
@@ -72,6 +73,10 @@ def self.Model(source)
# Model.typecast_empty_string_to_nil = false
# m.number = ''
# m.number # => '' instead of nil
+ # # Don't raise if typecasting to nil but column cannot be null
+ # Model.typecast_empty_string_to_nil = true
+ # Model.raise_on_typecast_failure = false
+ # m.not_null_column = '' # => nil
#
# * The following class level method aliases are defined:
# * Model.dataset= => set_dataset
View
8 lib/sequel_model/base.rb
@@ -16,6 +16,7 @@ class Model
@strict_param_setting = true
@typecast_empty_string_to_nil = true
@typecast_on_assignment = true
+ @raise_on_typecast_failure = true
# Which columns should be the only columns allowed in a call to set
# (default: all columns).
@@ -32,6 +33,10 @@ class Model
# to save/create/save_changes/etc.
metaattr_accessor :raise_on_save_failure
+ # Whether to raise an error if typecasting nil to a column with NULLs not
+ # allowed (default: true)
+ metaattr_accessor :raise_on_typecast_failure
+
# Which columns should not be update in a call to set
# (default: no columns).
metaattr_reader :restricted_columns
@@ -70,7 +75,8 @@ class Model
:@cache_ttl=>nil, :@dataset_methods=>:dup, :@primary_key=>nil,
:@raise_on_save_failure=>nil, :@restricted_columns=>:dup, :@restrict_primary_key=>nil,
:@sti_dataset=>nil, :@sti_key=>nil, :@strict_param_setting=>nil,
- :@typecast_empty_string_to_nil=>nil, :@typecast_on_assignment=>nil}
+ :@typecast_empty_string_to_nil=>nil, :@typecast_on_assignment=>nil,
+ :@raise_on_typecast_failure=>nil}
# Returns the first record from the database matching the conditions.
# If a hash is given, it is used as the conditions. If another
View
7 lib/sequel_model/record.rb
@@ -17,6 +17,10 @@ class Model
# returning nil on a failure to save/save_changes/etc.
attr_writer :raise_on_save_failure
+ # Whether this model instance should raise and error when attempting to
+ # typecast nil to a column which has :null => false set.
+ attr_writer :raise_on_typecast_failure
+
# Whether this model instance should raise an error if attempting
# to call a method through set/update and their variants that either
# doesn't exist or access to it is denied.
@@ -54,6 +58,7 @@ def initialize(values = nil, from_db = false, &block)
@strict_param_setting = model.strict_param_setting
@typecast_on_assignment = model.typecast_on_assignment
@typecast_empty_string_to_nil = model.typecast_empty_string_to_nil
+ @raise_on_typecast_failure = model.raise_on_typecast_failure
if from_db
@new = false
@values = values
@@ -531,7 +536,7 @@ def setter_methods(only, except)
def typecast_value(column, value)
return value unless @typecast_on_assignment && @db_schema && (col_schema = @db_schema[column])
value = nil if value == '' and @typecast_empty_string_to_nil and col_schema[:type] and ![:string, :blob].include?(col_schema[:type])
- raise(Error, "nil/NULL is not allowed for the #{column} column") if value.nil? && (col_schema[:allow_null] == false)
+ raise(Error, "nil/NULL is not allowed for the #{column} column") if @raise_on_typecast_failure && value.nil? && (col_schema[:allow_null] == false)
model.db.typecast_value(col_schema[:type], value)
end
View
11 spec/sequel_model/record_spec.rb
@@ -930,6 +930,17 @@ def ds.insert_select(hash)
m.x.should == nil
end
+ specify "should not raise when typecasting nil to NOT NULL column but raise_on_typecast_failure is off" do
+ @c.raise_on_typecast_failure = false
+ @c.typecast_on_assignment = true
+ @c.instance_variable_set(:@db_schema, {:x=>{:type=>:integer,:allow_null=>false}})
+ m = @c.new
+ m.x = ''
+ m.x.should == nil
+ m.x = nil
+ m.x.should == nil
+ end
+
specify "should raise an error if invalid data is used in an integer field" do
@c.instance_variable_set(:@db_schema, {:x=>{:type=>:integer}})
proc{@c.new.x = 'a'}.should raise_error
Please sign in to comment.
Something went wrong with that request. Please try again.