pg_adaptor 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pg_adaptor.rb +7 -2
- data/lib/pg_adaptor/version.rb +1 -1
- data/spec/pg_adaptor_spec.rb +8 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e7ea9fc5ff2fd2d870ad64879493e0a0e0f65f1
|
4
|
+
data.tar.gz: f6fa93e1759249ea3fb0e1d66f267d18f99e7ba8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc5537112979468f3fe6ca4735f011bb92702a2187c33bcc5d27850f876268368099c9d9af382ce97aa815f5f76699b2b964b4c194a49bd86919609c73662576
|
7
|
+
data.tar.gz: 610fe12cd6ece4b1c324f4517b461044d75e6d6458d8472789eb50a34f3a179f49bcf9a45e22017e6059db1d261a83736df435d7ba767030638daf3d8a2068f6
|
data/lib/pg_adaptor.rb
CHANGED
@@ -45,11 +45,16 @@ private
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def process(model)
|
48
|
+
schema = {}
|
49
|
+
self.class.db.schema(@table).each do |(key, info)|
|
50
|
+
schema[key] = info
|
51
|
+
end
|
48
52
|
fields = {}
|
49
53
|
model.each_pair do |field,value|
|
50
54
|
next if field == :id && value.nil?
|
51
|
-
|
52
|
-
|
55
|
+
case schema[field][:db_type]
|
56
|
+
when "jsonb" then fields[field] = Sequel.pg_jsonb value
|
57
|
+
when /\[\]$/ then fields[field] = Sequel.pg_array value
|
53
58
|
else
|
54
59
|
fields[field] = value
|
55
60
|
end
|
data/lib/pg_adaptor/version.rb
CHANGED
data/spec/pg_adaptor_spec.rb
CHANGED
@@ -6,11 +6,13 @@ RSpec.describe 'adapting structs into pg' do
|
|
6
6
|
before do
|
7
7
|
PGAdaptor.db = db
|
8
8
|
db.extension :pg_array
|
9
|
+
db.extension :pg_json
|
9
10
|
db.create_table :test_table do
|
10
11
|
primary_key :id
|
11
12
|
String :name
|
12
13
|
String :other
|
13
14
|
column :members, "text[]"
|
15
|
+
column :info, "jsonb"
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
@@ -26,12 +28,12 @@ RSpec.describe 'adapting structs into pg' do
|
|
26
28
|
end
|
27
29
|
|
28
30
|
describe 'using the adaptor' do
|
29
|
-
let(:klass) { Struct.new :name, :other, :members, :id }
|
31
|
+
let(:klass) { Struct.new :name, :other, :members, :info, :id }
|
30
32
|
let(:adaptor) { PGAdaptor.new :test_table, klass }
|
31
33
|
let(:table) { db[:test_table] }
|
32
34
|
|
33
35
|
describe 'with a new model' do
|
34
|
-
let(:model) { klass.new 'Test Model','Some Data',['Some Members'] }
|
36
|
+
let(:model) { klass.new 'Test Model','Some Data',['Some Members'],{ some: :info } }
|
35
37
|
let(:data) { table.order(:id).last }
|
36
38
|
|
37
39
|
shared_examples_for 'creates a record' do
|
@@ -51,6 +53,7 @@ RSpec.describe 'adapting structs into pg' do
|
|
51
53
|
expect(data[:name]).to eq 'Test Model'
|
52
54
|
expect(data[:other]).to eq 'Some Data'
|
53
55
|
expect(data[:members]).to eq ['Some Members']
|
56
|
+
expect(data[:info]).to eq "some" => "info"
|
54
57
|
end
|
55
58
|
end
|
56
59
|
|
@@ -66,9 +69,8 @@ RSpec.describe 'adapting structs into pg' do
|
|
66
69
|
end
|
67
70
|
|
68
71
|
describe 'with an existing model' do
|
69
|
-
let(:model) { klass.new 'Test Model','Some Data',['Some Other Members'] }
|
70
|
-
let(:id) { table.insert(name: 'My Model', other: 'Some Value', members: Sequel.pg_array(['Some Members'])) }
|
71
|
-
|
72
|
+
let(:model) { klass.new 'Test Model','Some Data',['Some Other Members'], { some: :info } }
|
73
|
+
let(:id) { table.insert(name: 'My Model', other: 'Some Value', members: Sequel.pg_array(['Some Members']), info: Sequel.pg_jsonb({other: :info})) }
|
72
74
|
before do
|
73
75
|
model.id = id
|
74
76
|
end
|
@@ -85,6 +87,7 @@ RSpec.describe 'adapting structs into pg' do
|
|
85
87
|
expect(data[:name]).to eq 'Test Model'
|
86
88
|
expect(data[:other]).to eq 'Some Data'
|
87
89
|
expect(data[:members]).to eq ['Some Other Members']
|
90
|
+
expect(data[:info]).to eq "some" => "info"
|
88
91
|
end
|
89
92
|
end
|
90
93
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_adaptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Rowe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|