DataMapper::Types::FormFriendlyInteger¶ ↑
An adaptation of the DataMapper Integer type designed to work well with web forms. It behaves in the same way as the standard DataMapper Integer type when writing the property to the database but it doesn’t immediately typecast the property unless it is an integer string. The example below illustrates this.
The reason for wanting this behaviour is to ensure that if an invalid entry is fed into a web form then when the form is re-displayed with it’s errors, the value in the form input will be as it was when it was entered.
EXAMPLE:¶ ↑
model MyModel include DataMapper::Resource property :id, Serial property :a_number, Integer property :ff_number, FormFriendlyInteger end
The a_number
property will behave as follows:
my_model = MyModel.new my_model.a_number = 7 my_model.ff_number = 7 my_model.a_number # => 7 my_model.ff_number # => 7 my_model.save Model.get(my_model.id).a_number # => 7 Model.get(my_model.id).ff_number # => 7 my_model = MyModel.new my_model.a_number = -7 my_model.ff_number = -7 my_model.a_number # => -7 my_model.ff_number # => -7 my_model.save Model.get(my_model.id).a_number # => -7 Model.get(my_model.id).ff_number # => -7 my_model = MyModel.new my_model.a_number = "7.45" my_model.ff_number = "7.45" my_model.a_number # => 7 my_model.ff_number # => "7.45" my_model.save Model.get(my_model.id).a_number # => 7 Model.get(my_model.id).ff_number # => 7 my_model = MyModel.new my_model.a_number = "not a number" my_model.ff_number = "not a number" my_model.a_number # => nil my_model.ff_number # => "not a number" my_model.save Model.get(my_model.id).a_number # => 0 Model.get(my_model.id).ff_number # => 0 my_model = MyModel.new my_model.a_number = [] # => nothing raised my_model.ff_number = [] # => raise(ArgumentError, '+value+ must be nil or a String')