ar_pg_array 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activerecord", ">= 2.3.5", "< 3.0"
4
+ group :development, :test do
5
+ gem "fake_arel"
6
+ gem "cancan"
7
+ gem "pg"
8
+ gem "rspec"
9
+ end
data/Rakefile CHANGED
@@ -1,15 +1,15 @@
1
1
  require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- desc 'Default: run unit tests.'
6
- task :default => :test
2
+ require 'rdoc/task'
7
3
 
8
4
  desc 'Test the postgres_arrays plugin.'
9
- Rake::TestTask.new(:test) do |t|
10
- t.libs << 'lib'
11
- t.pattern = 'test/**/*_test.rb'
12
- t.verbose = true
5
+ task :test do
6
+ Dir.chdir(File.dirname(__FILE__)) do
7
+ Process.wait2 spawn('rspec spec')
8
+ end
9
+ end
10
+
11
+ task :default do
12
+ # nothing
13
13
  end
14
14
 
15
15
  desc 'Generate documentation for the postgres_arrays plugin.'
@@ -20,24 +20,3 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
20
20
  rdoc.rdoc_files.include('README')
21
21
  rdoc.rdoc_files.include('lib/**/*.rb')
22
22
  end
23
-
24
- begin
25
- require 'jeweler'
26
- Jeweler::Tasks.new do |gemspec|
27
- gemspec.name = "ar_pg_array"
28
- gemspec.summary = "Use power of PostgreSQL Arrays in ActiveRecord"
29
- gemspec.description = "ar_pg_array includes support of PostgreSQL's int[], float[], text[], timestamptz[] etc. into ActiveRecord. You could define migrations for array columns, query on array columns."
30
- gemspec.email = "funny.falcon@gmail.com"
31
- gemspec.homepage = "http://github.com/funny-falcon/activerecord-postgresql-arrays"
32
- gemspec.authors = ["Sokolov Yura aka funny_falcon"]
33
- gemspec.add_dependency('activerecord', '>= 2.3.5', '<3.0')
34
- gemspec.rubyforge_project = 'ar-pg-array'
35
- end
36
- Jeweler::GemcutterTasks.new
37
- Jeweler::RubyforgeTasks.new do |rubyforge|
38
-
39
- end
40
- rescue LoadError
41
- puts "Jeweler not available. Install it with: gem install jeweler"
42
- end
43
-
@@ -0,0 +1,34 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "ar_pg_array"
8
+ s.version = "0.10.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sokolov Yura aka funny_falcon"]
12
+ s.date = "2012-06-03"
13
+ s.description = "ar_pg_array includes support of PostgreSQL's int[], float[], text[], timestamptz[] etc. into ActiveRecord. You could define migrations for array columns, query on array columns."
14
+ s.email = "funny.falcon@gmail.com"
15
+ s.extra_rdoc_files = [ "README" ]
16
+ s.files = (Dir['lib/**/*']+Dir['spec/**/*']+%w{Gemfile init.rb MIT_LICENSE README Rakefile ar_pg_array.gemspec}).find_all{|f| File.file?(f)}
17
+ s.homepage = "http://github.com/funny-falcon/activerecord-postgresql-arrays"
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = "ar-pg-array"
20
+ s.summary = "Use power of PostgreSQL Arrays in ActiveRecord"
21
+
22
+ if s.respond_to? :specification_version then
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<activerecord>, ["< 4.0", ">= 3.0.6"])
27
+ else
28
+ s.add_dependency(%q<activerecord>, ["< 4.0", ">= 3.0.6"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<activerecord>, ["< 4.0", ">= 3.0.6"])
32
+ end
33
+ end
34
+
@@ -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|
@@ -1,2 +1,5 @@
1
1
  class Bulk < ActiveRecord::Base
2
2
  end
3
+
4
+ class Bulk1 < Bulk
5
+ end
@@ -28,6 +28,7 @@ fourth:
28
28
  decimals: [ ]
29
29
  fifth:
30
30
  id: 5
31
+ type: "Bulk1"
31
32
  ints: [ 10 ]
32
33
  strings: [ "#{1+1}", "\\#{1+1}\"'\\z\x01", "\t\n" ]
33
34
  times: [ ]
@@ -12,6 +12,7 @@ ActiveRecord::Schema.define do
12
12
  end
13
13
 
14
14
  create_table "bulks", :force => true do |t|
15
+ t.string :type, :default => "Bulk"
15
16
  t.string :value, :default => "'"
16
17
  t.integer_array :ints, :default => [1, 2]
17
18
  t.string_array :strings, :default => %w{as so}
@@ -19,6 +20,7 @@ ActiveRecord::Schema.define do
19
20
  t.float_array :floats, :default => [1.0, 1.2]
20
21
  t.decimal_array :decimals, :default => [1.0, 1.2]
21
22
  t.text_array :texts, :default => [nil, 'Text', 'NULL', 'Text with nil', 'Text with , nil, !"\\', 'nil']
23
+ t.integer_array :empty_def, :default => []
22
24
  end
23
25
 
24
26
  create_table "unrelateds", :force => true do |t|
@@ -72,7 +72,7 @@ describe "PgArray" do
72
72
  bulk.floats.should == [1.0, 2.3]
73
73
  bulk.decimals.should == [1.0, 2.3]
74
74
  end
75
-
75
+
76
76
  it "should be created with defaults" do
77
77
  bulk = Bulk.new
78
78
  bulk.ints.should == [1, 2]
@@ -82,6 +82,22 @@ 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 == []
86
+ end
87
+
88
+ it "should be able to insert" do
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 == []
100
+ bulk.destroy
85
101
  end
86
102
 
87
103
  it "should not alter defaults" do
@@ -107,11 +123,40 @@ describe "PgArray" do
107
123
 
108
124
  it "should save right text" do
109
125
  bulk = Bulk.find(5)
110
- bulk.texts = ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\\\'\""]
126
+ bulk.texts = ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\n\\\'\""]
111
127
  bulk.save!
112
128
  bulk.texts = []
113
129
  bulk = Bulk.find(:first, :conditions=>'5 = id')
114
- 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
115
160
  end
116
161
 
117
162
  it "should be safe for eval" do
@@ -192,6 +237,19 @@ describe "PgArray" do
192
237
  model2.for_custom_serialize.to_yaml.should == obj.to_yaml
193
238
  end
194
239
 
240
+ it 'should be workable with sti' do
241
+ obj = Bulk1.where(:ints => [10].pg).first
242
+ obj.should be_instance_of Bulk1
243
+ obj.floats = [1.1, 2.2]
244
+ obj.save
245
+ obj1 = Bulk.find(obj.id)
246
+ obj1.should be_instance_of Bulk1
247
+ obj1.floats.should == [1.1, 2.2]
248
+ obj2 = Bulk1.new
249
+ obj2.save
250
+ obj2.destroy
251
+ end
252
+
195
253
  def map_times(times)
196
254
  times.map{|t| t.strftime("%F %T")}
197
255
  end
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.10.1
5
4
  prerelease:
5
+ version: 0.10.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sokolov Yura aka funny_falcon
@@ -12,27 +12,27 @@ 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:
19
19
  - - <
20
20
  - !ruby/object:Gem::Version
21
- version: '3.0'
21
+ version: '4.0'
22
22
  - - ! '>='
23
23
  - !ruby/object:Gem::Version
24
- version: 2.3.5
25
- type: :runtime
24
+ version: 3.0.6
26
25
  prerelease: false
27
26
  version_requirements: !ruby/object:Gem::Requirement
28
27
  none: false
29
28
  requirements:
30
29
  - - <
31
30
  - !ruby/object:Gem::Version
32
- version: '3.0'
31
+ version: '4.0'
33
32
  - - ! '>='
34
33
  - !ruby/object:Gem::Version
35
- version: 2.3.5
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.
@@ -42,31 +42,31 @@ extensions: []
42
42
  extra_rdoc_files:
43
43
  - README
44
44
  files:
45
- - MIT-LICENSE
46
- - README
47
- - Rakefile
48
- - VERSION
49
- - init.rb
50
45
  - lib/ar_pg_array.rb
46
+ - lib/ar_pg_array/schema_cacheable.rb
47
+ - lib/ar_pg_array/references_by.rb
48
+ - lib/ar_pg_array/querying.rb
51
49
  - lib/ar_pg_array/allways_save.rb
50
+ - lib/ar_pg_array/schema_fix_will_change.rb
52
51
  - lib/ar_pg_array/parser.rb
53
- - lib/ar_pg_array/querying.rb
54
- - lib/ar_pg_array/references_by.rb
55
52
  - lib/ar_pg_array/schema.rb
56
- - lib/ar_pg_array/schema_cacheable.rb
57
- - lib/ar_pg_array/schema_fix_will_change.rb
58
- - spec/fixtures/bulk.rb
53
+ - spec/spec_helper.rb
54
+ - spec/spec.opts
59
55
  - spec/fixtures/bulks.yml
60
- - spec/fixtures/item.rb
56
+ - spec/fixtures/tags.yml
57
+ - spec/fixtures/unrelateds.yml
58
+ - spec/fixtures/tag.rb
61
59
  - spec/fixtures/items.yml
60
+ - spec/fixtures/item.rb
62
61
  - spec/fixtures/schema.rb
63
- - spec/fixtures/tag.rb
64
- - spec/fixtures/tags.yml
62
+ - spec/fixtures/bulk.rb
65
63
  - spec/fixtures/unrelated.rb
66
- - spec/fixtures/unrelateds.yml
67
64
  - spec/pg_array_spec.rb
68
- - spec/spec.opts
69
- - spec/spec_helper.rb
65
+ - Gemfile
66
+ - init.rb
67
+ - README
68
+ - Rakefile
69
+ - ar_pg_array.gemspec
70
70
  homepage: http://github.com/funny-falcon/activerecord-postgresql-arrays
71
71
  licenses: []
72
72
  post_install_message:
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2010 Sokolov Yura aka funny_falcon
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.10.0