Permalink
Browse files

fix nested save and load

  • Loading branch information...
1 parent 0c38c7c commit 8d8079ec178c21c5f21ab9af7d2ef96c38c1df22 @funny-falcon committed Sep 7, 2012
Showing with 52 additions and 9 deletions.
  1. +1 −1 VERSION
  2. +19 −5 lib/ar_pg_array/parser.rb
  3. +1 −1 lib/ar_pg_array/schema.rb
  4. +31 −2 spec/pg_array_spec.rb
View
@@ -1 +1 @@
-0.11.1
+0.11.2
View
@@ -69,7 +69,7 @@ def _parse_pgarray(text, &block)
values << ar
if rest =~ /^\}\s*/
return values, $'
- elsif rest =~ /^,\s*{\s*/
+ elsif rest =~ /^,\s*\{\s*/
rest = $'
else
raise "Mailformed postgres array"
@@ -97,15 +97,28 @@ def _parse_pgarray(text, &block)
end
end
+ def _remap_array(array, &block)
+ array.map{|v|
+ case v
+ when Array
+ _remap_array(v, &block)
+ when nil
+ nil
+ else
+ yield v
+ end
+ }
+ end
+
def prepare_pg_integer_array(value)
- val = value.map{|v| v.nil? ? nil : v.to_i}.inspect
+ val = _remap_array(value){|v| v.to_i}.inspect
val.gsub!(NIL, NULL)
val.tr!(SQUARE_BRACKETS, CURLY_BRACKETS)
val
end
def prepare_pg_float_array(value)
- val = value.map{|v| v.nil? ? nil : v.to_f}.inspect
+ val = _remap_array(value){|v| v.to_f}.inspect
val.gsub!(NIL, NULL)
val.tr!(SQUARE_BRACKETS, CURLY_BRACKETS)
val
@@ -139,11 +152,11 @@ def prepare_pg_text_array(value)
"{#{value}}"
end
- def prepare_pg_string_array(value, &block)
+ def _prepare_pg_string_array(value, &block)
value = value.map{|val|
case val
when Array
- prepare_pg_string_array(val, &block)
+ _prepare_pg_string_array(val, &block)
when nil
NULL
else
@@ -157,4 +170,5 @@ def prepare_pg_string_array(value, &block)
}.join(',')
"{#{value}}"
end
+ alias prepare_pg_string_array _prepare_pg_string_array
end
@@ -181,7 +181,7 @@ def prepare_pg_string_array(value, base_type, column=nil)
else
PostgreSQLColumn::BASE_TYPE_COLUMNS[base_type.to_sym]
end
- super(value){|v| quote_without_postgresql_arrays(v, base_column)}
+ _prepare_pg_string_array(value){|v| quote_without_postgresql_arrays(v, base_column)}
end
NATIVE_DATABASE_TYPES.keys.each do |key|
View
@@ -123,11 +123,40 @@ def ab
it "should save right text" do
bulk = Bulk.find(5)
- bulk.texts = ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\\\'\""]
+ bulk.texts = ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""]
bulk.save!
bulk.texts = []
bulk = Bulk.find(:first, :conditions=>'5 = id')
- bulk.texts.should == ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\\\'\""]
+ bulk.texts.should == ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""]
+ end
+
+ it "should save nested arrays" do
+ Bulk.transaction do
+ bulk = Bulk.find(3)
+ bulk.ints = [[1,2],[3,4]]
+ bulk.floats = [[1.0, 2.3e34],[3.43,6.21]]
+ bulk.times = [parse_times(%w{2010-04-01 2011-04-01}), parse_times(%w{2011-05-01 2010-05-01})]
+ bulk.save!
+ bulk = Bulk.find(:first, :conditions=>'3 = id')
+ bulk.ints.should == [[1,2],[3,4]]
+ bulk.floats.should == [[1.0, 2.3e34],[3.43,6.21]]
+ bulk.times.map{|ts| map_times(ts)}.should == [
+ map_times(parse_times(%w{2010-04-01 2011-04-01})),
+ map_times(parse_times(%w{2011-05-01 2010-05-01}))]
+ raise ActiveRecord::Rollback
+ end
+ end
+
+ it "should save right nested text" do
+ Bulk.transaction do
+ bulk = Bulk.find(5)
+ bulk.texts = [['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""], ['asdf', 'fdsa']]
+ bulk.save!
+ bulk.texts = []
+ bulk = Bulk.find(:first, :conditions=>'5 = id')
+ bulk.texts.should == [['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""], ['asdf', 'fdsa']]
+ raise ActiveRecord::Rollback
+ end
end
it "should be safe for eval" do

0 comments on commit 8d8079e

Please sign in to comment.