ar_pg_array 0.11.1 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ar_pg_array"
8
- s.version = "0.11.1"
8
+ s.version = "0.11.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sokolov Yura aka funny_falcon"]
@@ -69,7 +69,7 @@ module PgArrayParser
69
69
  values << ar
70
70
  if rest =~ /^\}\s*/
71
71
  return values, $'
72
- elsif rest =~ /^,\s*{\s*/
72
+ elsif rest =~ /^,\s*\{\s*/
73
73
  rest = $'
74
74
  else
75
75
  raise "Mailformed postgres array"
@@ -97,15 +97,28 @@ module PgArrayParser
97
97
  end
98
98
  end
99
99
 
100
+ def _remap_array(array, &block)
101
+ array.map{|v|
102
+ case v
103
+ when Array
104
+ _remap_array(v, &block)
105
+ when nil
106
+ nil
107
+ else
108
+ yield v
109
+ end
110
+ }
111
+ end
112
+
100
113
  def prepare_pg_integer_array(value)
101
- val = value.map{|v| v.nil? ? nil : v.to_i}.inspect
114
+ val = _remap_array(value){|v| v.to_i}.inspect
102
115
  val.gsub!(NIL, NULL)
103
116
  val.tr!(SQUARE_BRACKETS, CURLY_BRACKETS)
104
117
  val
105
118
  end
106
119
 
107
120
  def prepare_pg_float_array(value)
108
- val = value.map{|v| v.nil? ? nil : v.to_f}.inspect
121
+ val = _remap_array(value){|v| v.to_f}.inspect
109
122
  val.gsub!(NIL, NULL)
110
123
  val.tr!(SQUARE_BRACKETS, CURLY_BRACKETS)
111
124
  val
@@ -139,11 +152,11 @@ module PgArrayParser
139
152
  "{#{value}}"
140
153
  end
141
154
 
142
- def prepare_pg_string_array(value, &block)
155
+ def _prepare_pg_string_array(value, &block)
143
156
  value = value.map{|val|
144
157
  case val
145
158
  when Array
146
- prepare_pg_string_array(val, &block)
159
+ _prepare_pg_string_array(val, &block)
147
160
  when nil
148
161
  NULL
149
162
  else
@@ -157,4 +170,5 @@ module PgArrayParser
157
170
  }.join(',')
158
171
  "{#{value}}"
159
172
  end
173
+ alias prepare_pg_string_array _prepare_pg_string_array
160
174
  end
@@ -181,7 +181,7 @@ module ActiveRecord
181
181
  else
182
182
  PostgreSQLColumn::BASE_TYPE_COLUMNS[base_type.to_sym]
183
183
  end
184
- super(value){|v| quote_without_postgresql_arrays(v, base_column)}
184
+ _prepare_pg_string_array(value){|v| quote_without_postgresql_arrays(v, base_column)}
185
185
  end
186
186
 
187
187
  NATIVE_DATABASE_TYPES.keys.each do |key|
@@ -20,6 +20,7 @@ ActiveRecord::Schema.define do
20
20
  t.float_array :floats, :default => [1.0, 1.2]
21
21
  t.decimal_array :decimals, :default => [1.0, 1.2]
22
22
  t.text_array :texts, :default => [nil, 'Text', 'NULL', 'Text with nil', 'Text with , nil, !"\\', 'nil']
23
+ t.integer_array :empty_def, :default => []
23
24
  end
24
25
 
25
26
  create_table "unrelateds", :force => true do |t|
@@ -82,11 +82,21 @@ describe "PgArray" do
82
82
  bulk.texts.should == [nil, 'Text', 'NULL', 'Text with nil', 'Text with , nil, !"\\', 'nil']
83
83
  map_times(bulk.times).should ==
84
84
  map_times(parse_times(%w{2010-01-01 2010-02-01}))
85
+ bulk.empty_def.should == []
85
86
  end
86
87
 
87
88
  it "should be able to insert" do
88
- bulk = Bulk.new
89
- bulk.save
89
+ bulki = Bulk.new
90
+ bulki.save
91
+ bulk = Bulk.find(bulki.id)
92
+ bulk.ints.should == [1, 2]
93
+ bulk.strings.should == %w{as so}
94
+ bulk.floats.should == [1.0, 1.2]
95
+ bulk.decimals.should == [1.0, 1.2]
96
+ bulk.texts.should == [nil, 'Text', 'NULL', 'Text with nil', 'Text with , nil, !"\\', 'nil']
97
+ map_times(bulk.times).should ==
98
+ map_times(parse_times(%w{2010-01-01 2010-02-01}))
99
+ bulk.empty_def.should == []
90
100
  bulk.destroy
91
101
  end
92
102
 
@@ -113,11 +123,40 @@ describe "PgArray" do
113
123
 
114
124
  it "should save right text" do
115
125
  bulk = Bulk.find(5)
116
- bulk.texts = ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\\\'\""]
126
+ bulk.texts = ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""]
117
127
  bulk.save!
118
128
  bulk.texts = []
119
129
  bulk = Bulk.find(:first, :conditions=>'5 = id')
120
- bulk.texts.should == ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\\\'\""]
130
+ bulk.texts.should == ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""]
131
+ end
132
+
133
+ it "should save nested arrays" do
134
+ Bulk.transaction do
135
+ bulk = Bulk.find(3)
136
+ bulk.ints = [[1,2],[3,4]]
137
+ bulk.floats = [[1.0, 2.3e34],[3.43,6.21]]
138
+ bulk.times = [parse_times(%w{2010-04-01 2011-04-01}), parse_times(%w{2011-05-01 2010-05-01})]
139
+ bulk.save!
140
+ bulk = Bulk.find(:first, :conditions=>'3 = id')
141
+ bulk.ints.should == [[1,2],[3,4]]
142
+ bulk.floats.should == [[1.0, 2.3e34],[3.43,6.21]]
143
+ bulk.times.map{|ts| map_times(ts)}.should == [
144
+ map_times(parse_times(%w{2010-04-01 2011-04-01})),
145
+ map_times(parse_times(%w{2011-05-01 2010-05-01}))]
146
+ raise ActiveRecord::Rollback
147
+ end
148
+ end
149
+
150
+ it "should save right nested text" do
151
+ Bulk.transaction do
152
+ bulk = Bulk.find(5)
153
+ bulk.texts = [['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""], ['asdf', 'fdsa']]
154
+ bulk.save!
155
+ bulk.texts = []
156
+ bulk = Bulk.find(:first, :conditions=>'5 = id')
157
+ bulk.texts.should == [['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""], ['asdf', 'fdsa']]
158
+ raise ActiveRecord::Rollback
159
+ end
121
160
  end
122
161
 
123
162
  it "should be safe for eval" do
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_pg_array
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
5
4
  prerelease:
5
+ version: 0.11.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sokolov Yura aka funny_falcon
@@ -12,7 +12,7 @@ cert_chain: []
12
12
  date: 2012-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: activerecord
15
+ type: :runtime
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
@@ -22,7 +22,6 @@ dependencies:
22
22
  - - ! '>='
23
23
  - !ruby/object:Gem::Version
24
24
  version: 3.0.6
25
- type: :runtime
26
25
  prerelease: false
27
26
  version_requirements: !ruby/object:Gem::Requirement
28
27
  none: false
@@ -33,6 +32,7 @@ dependencies:
33
32
  - - ! '>='
34
33
  - !ruby/object:Gem::Version
35
34
  version: 3.0.6
35
+ name: activerecord
36
36
  description: ar_pg_array includes support of PostgreSQL's int[], float[], text[],
37
37
  timestamptz[] etc. into ActiveRecord. You could define migrations for array columns,
38
38
  query on array columns.