Permalink
Browse files

Added tests for boolean conversion, and added handlers for boolean ->…

… boolean and integer -> boolean transforms
  • Loading branch information...
1 parent 1b7ab17 commit 8ffae1836d5310bff0835ae7c0ae7378bde17fa3 @qnm qnm committed with ludicast Nov 12, 2010
Showing with 26 additions and 2 deletions.
  1. +6 −2 lib/serialization_helper.rb
  2. +20 −0 spec/serialization_utils_spec.rb
@@ -115,12 +115,16 @@ def self.convert_booleans(records, columns)
records.each do |record|
columns.each do |column|
next if is_boolean(record[column])
- record[column] = (record[column] == 't' or record[column] == '1')
+ record[column] = convert_boolean(record[column])
end
end
records
end
+ def self.convert_boolean(value)
+ ['t', '1', true, 1].include?(value)
+ end
+
def self.boolean_columns(table)
columns = ActiveRecord::Base.connection.columns(table).reject { |c| silence_warnings { c.type != :boolean } }
columns.map { |c| c.name }
@@ -192,4 +196,4 @@ def self.table_record_count(table)
end
-end
+end
@@ -28,4 +28,24 @@
ActiveRecord::Base.connection.should_receive(:quote_table_name).with('values').and_return('`values`')
SerializationHelper::Utils.quote_table('values').should == '`values`'
end
+
+ it "should convert ruby booleans to true and false" do
+ SerializationHelper::Utils.convert_boolean(true).should == true
+ SerializationHelper::Utils.convert_boolean(false).should == false
+ end
+
+ it "should convert ruby string t and f to true and false" do
+ SerializationHelper::Utils.convert_boolean('t').should == true
+ SerializationHelper::Utils.convert_boolean('f').should == false
+ end
+
+ it "should convert ruby string 1 and 0 to true and false" do
+ SerializationHelper::Utils.convert_boolean('1').should == true
+ SerializationHelper::Utils.convert_boolean('0').should == false
+ end
+
+ it "should convert ruby integer 1 and 0 to true and false" do
+ SerializationHelper::Utils.convert_boolean(1).should == true
+ SerializationHelper::Utils.convert_boolean(0).should == false
+ end
end

0 comments on commit 8ffae18

Please sign in to comment.