pg_adaptor 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +25 -0
- data/Rakefile +10 -0
- data/lib/pg_adaptor/version.rb +3 -0
- data/lib/pg_adaptor.rb +60 -0
- data/pg_adaptor.gemspec +23 -0
- data/spec/pg_adaptor_spec.rb +147 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 77ee8b9297e7e49576b550b2dda9e13f19745a96
|
4
|
+
data.tar.gz: 991e6aa124ef7d4a6c6fd4863e11ba0f41e82f1c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 669f023507c1d760065dd41a189550646b67009f56c8ff9bb6852fc6725791e8c7583e5185bb57b98eb59ff8579496b6c43645604fca4a4065db058c7499c1de
|
7
|
+
data.tar.gz: 57c9d1718a80a4784f87fd136fa981857fcd3c516cd62409013d2a80b11ef01ca75b8cc738e36f82516f219f9126935982bc268033e4be1c8a1d7c5ebd13c2c2
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Jon Rowe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# PGAdaptor
|
2
|
+
|
3
|
+
A simple PG handler. Translates Structs into PG and back.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'pg_adaptor'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install pg_adaptor
|
18
|
+
|
19
|
+
## Contributing
|
20
|
+
|
21
|
+
1. Fork it
|
22
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
23
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
24
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
25
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/pg_adaptor.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require "pg_adaptor/version"
|
3
|
+
|
4
|
+
class PGAdaptor
|
5
|
+
class << self
|
6
|
+
attr_accessor :db
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize name, klass
|
10
|
+
@table = self.class.db[name]
|
11
|
+
@klass = klass
|
12
|
+
end
|
13
|
+
|
14
|
+
def insert model
|
15
|
+
@table.insert process(model)
|
16
|
+
end
|
17
|
+
|
18
|
+
def upsert model, opts = { field: :id }
|
19
|
+
values = process model
|
20
|
+
@table.insert_conflict(target: opts[:field], update: values).insert values
|
21
|
+
end
|
22
|
+
|
23
|
+
def update model, query = { id: model.id }
|
24
|
+
@table.where(query).update process(model)
|
25
|
+
end
|
26
|
+
|
27
|
+
def fetch selector = {}, opts = {}
|
28
|
+
build @table.where(selector).first
|
29
|
+
end
|
30
|
+
|
31
|
+
def remove selector = {}
|
32
|
+
@table.where(selector).delete
|
33
|
+
end
|
34
|
+
|
35
|
+
def find selector = {}
|
36
|
+
@table.where(selector).map { |row| build row }
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def build result
|
42
|
+
@klass.new.tap do |model|
|
43
|
+
result.each { |field,value| model[field] = value }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def process(model)
|
48
|
+
fields = {}
|
49
|
+
model.each_pair do |field,value|
|
50
|
+
next if field == :id && value.nil?
|
51
|
+
if Array === value
|
52
|
+
fields[field] = Sequel.pg_array value
|
53
|
+
else
|
54
|
+
fields[field] = value
|
55
|
+
end
|
56
|
+
end
|
57
|
+
fields
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
data/pg_adaptor.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/pg_adaptor/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jon Rowe"]
|
6
|
+
gem.email = ["hello@jonrowe.co.uk"]
|
7
|
+
gem.description = %q{A simple pg handler. Translates Structs into PG and back.}
|
8
|
+
gem.summary = %q{A simple pg handler. Translates Structs into PG and back.}
|
9
|
+
gem.homepage = "https://github.com/JonRowe/PGAdaptor"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "pg_adaptor"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = PGAdaptor::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'pg'
|
19
|
+
gem.add_dependency 'sequel'
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'pg_adaptor'
|
2
|
+
|
3
|
+
RSpec.describe 'adapting structs into pg' do
|
4
|
+
let(:db) { Sequel.postgres 'pg_adaptor_test' }
|
5
|
+
|
6
|
+
before do
|
7
|
+
PGAdaptor.db = db
|
8
|
+
db.extension :pg_array
|
9
|
+
db.create_table :test_table do
|
10
|
+
primary_key :id
|
11
|
+
String :name
|
12
|
+
String :other
|
13
|
+
column :members, "text[]"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
db.drop_table :test_table
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'db setup' do
|
22
|
+
it 'can be configured' do
|
23
|
+
PGAdaptor.db = fake = double
|
24
|
+
expect(PGAdaptor.db).to eq fake
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'using the adaptor' do
|
29
|
+
let(:klass) { Struct.new :name, :other, :members, :id }
|
30
|
+
let(:adaptor) { PGAdaptor.new :test_table, klass }
|
31
|
+
let(:table) { db[:test_table] }
|
32
|
+
|
33
|
+
describe 'with a new model' do
|
34
|
+
let(:model) { klass.new 'Test Model','Some Data',['Some Members'] }
|
35
|
+
let(:data) { table.order(:id).last }
|
36
|
+
|
37
|
+
shared_examples_for 'creates a record' do
|
38
|
+
it 'changes the number of items in the table' do
|
39
|
+
expect { perform }.to change { table.count }.by(1)
|
40
|
+
end
|
41
|
+
it 'generates an id, ignoring any set key' do
|
42
|
+
perform
|
43
|
+
expect(data[:id]).to be_a Integer
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
shared_examples_for 'new model' do
|
48
|
+
it_should_behave_like 'creates a record'
|
49
|
+
it 'sets my fields and values' do
|
50
|
+
perform
|
51
|
+
expect(data[:name]).to eq 'Test Model'
|
52
|
+
expect(data[:other]).to eq 'Some Data'
|
53
|
+
expect(data[:members]).to eq ['Some Members']
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'inserting' do
|
58
|
+
let(:perform) { adaptor.insert model }
|
59
|
+
it_should_behave_like 'new model'
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'upserting' do
|
63
|
+
let(:perform) { adaptor.upsert model }
|
64
|
+
it_should_behave_like 'new model'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
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
|
+
before do
|
73
|
+
model.id = id
|
74
|
+
end
|
75
|
+
|
76
|
+
shared_examples_for 'modifying an existing model' do
|
77
|
+
let(:data) { table.order(:id).last }
|
78
|
+
|
79
|
+
it 'doesnt change the number of items in the table' do
|
80
|
+
expect { perform }.to change { table.count }.by(0)
|
81
|
+
end
|
82
|
+
it 'sets my fields and values' do
|
83
|
+
perform
|
84
|
+
expect(data[:id]).to eq model.id
|
85
|
+
expect(data[:name]).to eq 'Test Model'
|
86
|
+
expect(data[:other]).to eq 'Some Data'
|
87
|
+
expect(data[:members]).to eq ['Some Other Members']
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'to update it' do
|
92
|
+
let(:perform) { adaptor.update model }
|
93
|
+
it_should_behave_like 'modifying an existing model'
|
94
|
+
end
|
95
|
+
|
96
|
+
describe 'to upsert it' do
|
97
|
+
let(:perform) { adaptor.upsert model }
|
98
|
+
it_should_behave_like 'modifying an existing model'
|
99
|
+
end
|
100
|
+
|
101
|
+
describe 'to fetch it' do
|
102
|
+
let(:result) { adaptor.fetch(id: id) }
|
103
|
+
|
104
|
+
it "returns a class" do
|
105
|
+
expect(result).to be_a klass
|
106
|
+
end
|
107
|
+
specify "the classes fields are set correctly" do
|
108
|
+
expect(result.id).to eq id
|
109
|
+
expect(result.name).to eq 'My Model'
|
110
|
+
expect(result.other).to eq 'Some Value'
|
111
|
+
expect(result.members).to eq ['Some Members']
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe 'to remove it' do
|
116
|
+
it 'removes the record matching the selector' do
|
117
|
+
expect {
|
118
|
+
adaptor.remove(id: id)
|
119
|
+
}.to change { table.count }.to 0
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe 'finding multiples' do
|
125
|
+
before do
|
126
|
+
3.times do |i|
|
127
|
+
table.insert(name: 'My Model', other: i)
|
128
|
+
end
|
129
|
+
3.times do |i|
|
130
|
+
table.insert(name: 'Other Model', other: i)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
let(:result) { adaptor.find(name: 'My Model') }
|
135
|
+
|
136
|
+
it 'returns 3 models' do
|
137
|
+
expect(result.count).to eq 3
|
138
|
+
end
|
139
|
+
it 'translates all to klass' do
|
140
|
+
expect(result.all? { |k| k.is_a? klass }).to be true
|
141
|
+
end
|
142
|
+
it 'gets them all' do
|
143
|
+
expect(result.map(&:other)).to eq ['0', '1', '2']
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_adaptor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jon Rowe
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sequel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A simple pg handler. Translates Structs into PG and back.
|
70
|
+
email:
|
71
|
+
- hello@jonrowe.co.uk
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/pg_adaptor.rb
|
83
|
+
- lib/pg_adaptor/version.rb
|
84
|
+
- pg_adaptor.gemspec
|
85
|
+
- spec/pg_adaptor_spec.rb
|
86
|
+
homepage: https://github.com/JonRowe/PGAdaptor
|
87
|
+
licenses: []
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.5.1
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: A simple pg handler. Translates Structs into PG and back.
|
109
|
+
test_files:
|
110
|
+
- spec/pg_adaptor_spec.rb
|