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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77ee8b9297e7e49576b550b2dda9e13f19745a96
4
- data.tar.gz: 991e6aa124ef7d4a6c6fd4863e11ba0f41e82f1c
3
+ metadata.gz: 6e7ea9fc5ff2fd2d870ad64879493e0a0e0f65f1
4
+ data.tar.gz: f6fa93e1759249ea3fb0e1d66f267d18f99e7ba8
5
5
  SHA512:
6
- metadata.gz: 669f023507c1d760065dd41a189550646b67009f56c8ff9bb6852fc6725791e8c7583e5185bb57b98eb59ff8579496b6c43645604fca4a4065db058c7499c1de
7
- data.tar.gz: 57c9d1718a80a4784f87fd136fa981857fcd3c516cd62409013d2a80b11ef01ca75b8cc738e36f82516f219f9126935982bc268033e4be1c8a1d7c5ebd13c2c2
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
- if Array === value
52
- fields[field] = Sequel.pg_array value
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
@@ -1,3 +1,3 @@
1
1
  class PGAdaptor
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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.1
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-21 00:00:00.000000000 Z
11
+ date: 2016-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg