Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleanup raise_on_typecase_failure patches

  • Loading branch information...
commit 81d2c10bf74cb34b405c7e1ee8042996b743914d 1 parent 0578060
@jeremyevans authored
View
2  CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Add Model.raise_on_typecast_failure=, which makes it possible to not raise errors on invalid typecasts (michalbugno)
+
* Add schema.rdoc file, which provides an brief description of the various parts of Sequel related to schema modification (jeremyevans)
* Fix constraint generation when not using a proc or interpolated string (jeremyevans)
View
3  lib/sequel_model.rb
@@ -73,10 +73,11 @@ 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
+ # # Don't raise if unable to typecast data for a column
# Model.typecast_empty_string_to_nil = true
# Model.raise_on_typecast_failure = false
# m.not_null_column = '' # => nil
+ # m.number = 'A' # => 'A'
#
# * The following class level method aliases are defined:
# * Model.dataset= => set_dataset
View
6 lib/sequel_model/base.rb
@@ -9,6 +9,7 @@ class Model
@dataset_methods = {}
@primary_key = :id
@raise_on_save_failure = true
+ @raise_on_typecast_failure = true
@restrict_primary_key = true
@restricted_columns = nil
@sti_dataset = nil
@@ -16,7 +17,6 @@ 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).
@@ -33,8 +33,8 @@ 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)
+ # Whether to raise an error when unable to typecast data for a column
+ # (default: true)
metaattr_accessor :raise_on_typecast_failure
# Which columns should not be update in a call to set
View
10 lib/sequel_model/record.rb
@@ -2,7 +2,7 @@ module Sequel
class Model
# The setter methods (methods ending with =) that are never allowed
# to be called automatically via set.
- RESTRICTED_SETTER_METHODS = %w"== === []= taguri= typecast_empty_string_to_nil= typecast_on_assignment= strict_param_setting= raise_on_save_failure="
+ RESTRICTED_SETTER_METHODS = %w"== === []= taguri= typecast_empty_string_to_nil= typecast_on_assignment= strict_param_setting= raise_on_save_failure= raise_on_typecast_failure="
# The current cached associations. A hash with the keys being the
# association name symbols and the values being the associated object
@@ -17,8 +17,8 @@ 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.
+ # Whether this model instance should raise an error when it cannot typecast
+ # data for a column correctly.
attr_writer :raise_on_typecast_failure
# Whether this model instance should raise an error if attempting
@@ -536,10 +536,10 @@ 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 @raise_on_typecast_failure && value.nil? && (col_schema[:allow_null] == false)
+ raise(Error::InvalidValue, "nil/NULL is not allowed for the #{column} column") if @raise_on_typecast_failure && value.nil? && (col_schema[:allow_null] == false)
begin
model.db.typecast_value(col_schema[:type], value)
- rescue Sequel::Error::InvalidValue
+ rescue Error::InvalidValue
if @raise_on_typecast_failure
raise
else
Please sign in to comment.
Something went wrong with that request. Please try again.