postgres_ext 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +8 -0
- data/README.md +2 -2
- data/lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb +24 -12
- data/lib/postgres_ext/version.rb +1 -1
- data/spec/columns/array_spec.rb +1 -0
- data/spec/migrations/active_record_migration_spec.rb +36 -0
- data/spec/models/array_spec.rb +78 -0
- metadata +9 -4
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -142,9 +142,9 @@ person.favorite_numbers
|
|
142
142
|
person.save
|
143
143
|
|
144
144
|
person_2 = Person.first
|
145
|
-
person_2.
|
145
|
+
person_2.favorite_numbers
|
146
146
|
# => [1,2,3]
|
147
|
-
person_2.
|
147
|
+
person_2.favorite_numbers.first.class
|
148
148
|
# => Fixnum
|
149
149
|
```
|
150
150
|
|
@@ -30,8 +30,10 @@ module ActiveRecord
|
|
30
30
|
return coder.load(value) if encoded?
|
31
31
|
|
32
32
|
klass = self.class
|
33
|
-
if self.array && value.start_with?('{') && value.end_with?('}')
|
33
|
+
if self.array && String === value && value.start_with?('{') && value.end_with?('}')
|
34
34
|
string_to_array value
|
35
|
+
elsif self.array && Array === value
|
36
|
+
value
|
35
37
|
else
|
36
38
|
case type
|
37
39
|
when :inet, :cidr then klass.string_to_cidr_address(value)
|
@@ -42,11 +44,15 @@ module ActiveRecord
|
|
42
44
|
end
|
43
45
|
alias_method_chain :type_cast, :extended_types
|
44
46
|
def string_to_array(value)
|
45
|
-
|
46
|
-
|
47
|
-
string_array
|
47
|
+
if Array === value
|
48
|
+
value
|
48
49
|
else
|
49
|
-
|
50
|
+
string_array = parse_pg_array value
|
51
|
+
if type == :string
|
52
|
+
string_array
|
53
|
+
else
|
54
|
+
type_cast_array(string_array)
|
55
|
+
end
|
50
56
|
end
|
51
57
|
end
|
52
58
|
|
@@ -65,10 +71,14 @@ module ActiveRecord
|
|
65
71
|
def type_cast_code_with_extended_types(var_name)
|
66
72
|
klass = self.class.name
|
67
73
|
|
68
|
-
|
69
|
-
|
74
|
+
if self.array
|
75
|
+
"#{klass}.new('#{self.name}', #{self.default.nil? ? 'nil' : "'#{self.default}'"}, '#{self.sql_type}').string_to_array(#{var_name})"
|
70
76
|
else
|
71
|
-
|
77
|
+
case type
|
78
|
+
when :inet, :cidr then "#{klass}.string_to_cidr_address(#{var_name})"
|
79
|
+
else
|
80
|
+
type_cast_code_without_extended_types(var_name)
|
81
|
+
end
|
72
82
|
end
|
73
83
|
end
|
74
84
|
alias_method_chain :type_cast_code, :extended_types
|
@@ -165,17 +175,19 @@ module ActiveRecord
|
|
165
175
|
NATIVE_DATABASE_TYPES.merge!(EXTENDED_TYPES)
|
166
176
|
|
167
177
|
def add_column_options!(sql, options)
|
168
|
-
if options[:column].array
|
178
|
+
if options[:array] || options[:column].try(:array)
|
169
179
|
sql << '[]'
|
170
180
|
end
|
171
181
|
super
|
172
182
|
end
|
173
183
|
|
174
|
-
def type_cast_with_extended_types(value, column)
|
184
|
+
def type_cast_with_extended_types(value, column, part_array = false)
|
175
185
|
case value
|
176
186
|
when NilClass
|
177
|
-
if column.array
|
187
|
+
if column.array && part_array
|
178
188
|
'NULL'
|
189
|
+
elsif column.array && !part_array
|
190
|
+
value
|
179
191
|
else
|
180
192
|
type_cast_without_extended_types(value, column)
|
181
193
|
end
|
@@ -200,7 +212,7 @@ module ActiveRecord
|
|
200
212
|
end
|
201
213
|
|
202
214
|
def array_to_string(value, column)
|
203
|
-
"{#{value.map{|val| type_cast(val, column)}.join(',')}}"
|
215
|
+
"{#{value.map{|val| type_cast(val, column, true)}.join(',')}}"
|
204
216
|
end
|
205
217
|
end
|
206
218
|
end
|
data/lib/postgres_ext/version.rb
CHANGED
data/spec/columns/array_spec.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'activerecord migrations' do
|
4
|
+
let!(:connection) { ActiveRecord::Base.connection }
|
5
|
+
it 'creates non-postgres_ext columns' do
|
6
|
+
|
7
|
+
lambda do
|
8
|
+
DoMigration.new.up
|
9
|
+
end.should_not raise_exception
|
10
|
+
|
11
|
+
columns = connection.columns(:generic_data_types)
|
12
|
+
col_1 = columns.detect { |c| c.name == 'col_1'}
|
13
|
+
col_2 = columns.detect { |c| c.name == 'col_2'}
|
14
|
+
col_3 = columns.detect { |c| c.name == 'col_3'}
|
15
|
+
col_4 = columns.detect { |c| c.name == 'col_4'}
|
16
|
+
|
17
|
+
|
18
|
+
col_1.sql_type.should eq 'integer'
|
19
|
+
col_2.sql_type.should eq 'character varying(255)'
|
20
|
+
col_3.sql_type.should eq 'timestamp without time zone'
|
21
|
+
col_4.sql_type.should eq 'text'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
class DoMigration < ActiveRecord::Migration
|
27
|
+
def up
|
28
|
+
create_table :generic_data_types do |t|
|
29
|
+
t.integer :col_1
|
30
|
+
t.string :col_2
|
31
|
+
t.datetime :col_3
|
32
|
+
end
|
33
|
+
add_column :generic_data_types, :col_4, :text
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Models with array columns' do
|
4
|
+
context 'no default value, string array' do
|
5
|
+
let!(:adapter) { ActiveRecord::Base.connection }
|
6
|
+
|
7
|
+
before do
|
8
|
+
adapter.create_table :users, :force => true do |t|
|
9
|
+
t.string :nick_names, :array => true
|
10
|
+
end
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
attr_accessible :nick_names
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
after do
|
17
|
+
adapter.drop_table :users
|
18
|
+
Object.send(:remove_const, :User)
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#create' do
|
22
|
+
it 'creates a user when there is no array assignment' do
|
23
|
+
u = User.create()
|
24
|
+
u.reload
|
25
|
+
u.nick_names.should eq nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'creates a user with an empty array' do
|
29
|
+
u = User.create(:nick_names => [])
|
30
|
+
u.reload
|
31
|
+
u.nick_names.should eq []
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'creates a user with an array of some values' do
|
35
|
+
u = User.create(:nick_names => ['some', 'things'])
|
36
|
+
u.reload
|
37
|
+
u.nick_names.should eq ['some', 'things']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'default value, string array' do
|
43
|
+
let!(:adapter) { ActiveRecord::Base.connection }
|
44
|
+
before do
|
45
|
+
adapter.create_table :defaulted_users, :force => true do |t|
|
46
|
+
t.string :nick_names, :array => true, :default => '{}'
|
47
|
+
end
|
48
|
+
class DefaultedUser < ActiveRecord::Base
|
49
|
+
attr_accessible :nick_names
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
after do
|
54
|
+
adapter.drop_table :defaulted_users
|
55
|
+
Object.send(:remove_const, :DefaultedUser)
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#create' do
|
59
|
+
it 'creates a user when there is no array assignment' do
|
60
|
+
u = DefaultedUser.create()
|
61
|
+
u.reload
|
62
|
+
u.nick_names.should eq []
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'creates a user with an nil' do
|
66
|
+
u = DefaultedUser.create(:nick_names => nil)
|
67
|
+
u.reload
|
68
|
+
u.nick_names.should eq nil
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'creates a user with an array of some values' do
|
72
|
+
u = DefaultedUser.create(:nick_names => ['some', 'things'])
|
73
|
+
u.reload
|
74
|
+
u.nick_names.should eq ['some', 'things']
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postgres_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -165,6 +165,7 @@ files:
|
|
165
165
|
- .gitignore
|
166
166
|
- .rspec
|
167
167
|
- .travis.yml
|
168
|
+
- CHANGELOG.md
|
168
169
|
- Gemfile
|
169
170
|
- LICENSE
|
170
171
|
- README.md
|
@@ -235,11 +236,13 @@ files:
|
|
235
236
|
- spec/dummy/vendor/assets/javascripts/.gitkeep
|
236
237
|
- spec/dummy/vendor/assets/stylesheets/.gitkeep
|
237
238
|
- spec/dummy/vendor/plugins/.gitkeep
|
239
|
+
- spec/migrations/active_record_migration_spec.rb
|
238
240
|
- spec/migrations/array_spec.rb
|
239
241
|
- spec/migrations/cidr_spec.rb
|
240
242
|
- spec/migrations/inet_spec.rb
|
241
243
|
- spec/migrations/macaddr_spec.rb
|
242
244
|
- spec/migrations/uuid_spec.rb
|
245
|
+
- spec/models/array_spec.rb
|
243
246
|
- spec/schema_dumper/array_spec.rb
|
244
247
|
- spec/schema_dumper/cidr_spec.rb
|
245
248
|
- spec/schema_dumper/inet_spec.rb
|
@@ -260,7 +263,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
260
263
|
version: '0'
|
261
264
|
segments:
|
262
265
|
- 0
|
263
|
-
hash: -
|
266
|
+
hash: -1872786162985157385
|
264
267
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
265
268
|
none: false
|
266
269
|
requirements:
|
@@ -269,7 +272,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
269
272
|
version: '0'
|
270
273
|
segments:
|
271
274
|
- 0
|
272
|
-
hash: -
|
275
|
+
hash: -1872786162985157385
|
273
276
|
requirements: []
|
274
277
|
rubyforge_project:
|
275
278
|
rubygems_version: 1.8.23
|
@@ -330,11 +333,13 @@ test_files:
|
|
330
333
|
- spec/dummy/vendor/assets/javascripts/.gitkeep
|
331
334
|
- spec/dummy/vendor/assets/stylesheets/.gitkeep
|
332
335
|
- spec/dummy/vendor/plugins/.gitkeep
|
336
|
+
- spec/migrations/active_record_migration_spec.rb
|
333
337
|
- spec/migrations/array_spec.rb
|
334
338
|
- spec/migrations/cidr_spec.rb
|
335
339
|
- spec/migrations/inet_spec.rb
|
336
340
|
- spec/migrations/macaddr_spec.rb
|
337
341
|
- spec/migrations/uuid_spec.rb
|
342
|
+
- spec/models/array_spec.rb
|
338
343
|
- spec/schema_dumper/array_spec.rb
|
339
344
|
- spec/schema_dumper/cidr_spec.rb
|
340
345
|
- spec/schema_dumper/inet_spec.rb
|