sinja 1.2.0.pre2 → 1.2.0.pre3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +5 -3
- data/README.md +3 -4
- data/demo-app/Dockerfile +1 -1
- data/demo-app/Gemfile +4 -8
- data/demo-app/README.md +4 -3
- data/demo-app/app.rb +2 -0
- data/lib/sinja/version.rb +1 -1
- data/sinja.gemspec +2 -2
- metadata +2 -30
- data/extensions/sequel/Gemfile +0 -4
- data/extensions/sequel/LICENSE.txt +0 -21
- data/extensions/sequel/README.md +0 -274
- data/extensions/sequel/Rakefile +0 -10
- data/extensions/sequel/bin/console +0 -14
- data/extensions/sequel/bin/setup +0 -8
- data/extensions/sequel/lib/sinatra/jsonapi/sequel.rb +0 -7
- data/extensions/sequel/lib/sinja-sequel.rb +0 -2
- data/extensions/sequel/lib/sinja/sequel.rb +0 -110
- data/extensions/sequel/lib/sinja/sequel/core.rb +0 -72
- data/extensions/sequel/lib/sinja/sequel/helpers.rb +0 -78
- data/extensions/sequel/lib/sinja/sequel/version.rb +0 -6
- data/extensions/sequel/sinja-sequel.gemspec +0 -29
- data/extensions/sequel/test/test_helper.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82d0d656750a406ce8ac8f8e3fc227dcd9123970
|
4
|
+
data.tar.gz: 87b665b88f0abc9097b935ed59a648d5fdbca4e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14557eda5449b7249797c693497f7bf686c0dd4b040e78fdd77e868df6b3b21c794ca7709f4cd37a0cc40ace6edc2e26f424156a35b4227be9b9190873339b1d
|
7
|
+
data.tar.gz: d9a3e61434e0733425045e110a494bd25f4c57176ffb5ba6e374c8b45a883f1ea42704d77f3426ff73e5ae035ebcb2ed4e6830be6185e24151c47628e52dde83
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gemspec
|
4
|
-
|
5
|
-
gem 'munson',
|
6
|
-
:git=>'https://github.com/mwpastore/munson', :branch=>'develop'
|
4
|
+
|
5
|
+
gem 'munson', :require=>false,
|
6
|
+
:git=>'https://github.com/mwpastore/munson.git', :branch=>'develop'
|
7
|
+
gem 'sinja-sequel', :require=>false,
|
8
|
+
:git=>'https://github.com/mwpastore/sinja-sequel.git', :branch=>'master'
|
data/README.md
CHANGED
@@ -362,8 +362,7 @@ welcome.
|
|
362
362
|
|
363
363
|
Sinja extensions provide additional helpers, DSL, and configuration, packaging
|
364
364
|
ORM-specific boilerplate as separate gems. At the moment, the only available
|
365
|
-
extension is for [Sequel]
|
366
|
-
welcome!
|
365
|
+
extension is for [Sequel][30], but community contributions are welcome!
|
367
366
|
|
368
367
|
### Comparison with JSONAPI::Resources
|
369
368
|
|
@@ -1032,8 +1031,7 @@ Could be used to build the following top-level links in the response document:
|
|
1032
1031
|
You must also set the `page_using` configurable to a hash of symbols
|
1033
1032
|
representing the paging fields used in your application (for example, `:number`
|
1034
1033
|
and `:size` for the above example) along with their default values (or `nil`).
|
1035
|
-
Please see the [Sequel extension]
|
1036
|
-
detailed, working example.
|
1034
|
+
Please see the [Sequel extension][30] for a detailed, working example.
|
1037
1035
|
|
1038
1036
|
The easiest way to page a collection by default is to tweak the post-processed
|
1039
1037
|
query parameter(s) in a `before_<action>` hook:
|
@@ -1574,3 +1572,4 @@ License](http://opensource.org/licenses/MIT).
|
|
1574
1572
|
[27]: https://github.com/coryodaniel/munson
|
1575
1573
|
[28]: https://github.com/chingor13/json_api_client
|
1576
1574
|
[29]: https://github.com/brynary/rack-test
|
1575
|
+
[30]: https://github.com/mwpastore/sinja-sequel
|
data/demo-app/Dockerfile
CHANGED
data/demo-app/Gemfile
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gem 'json', '~> 2.0'
|
4
|
-
gem 'sequel', '~> 4.41'
|
5
4
|
gem 'sinatra', '>= 2.0.0.beta2', '< 3'
|
6
5
|
gem 'sinatra-contrib', '>= 2.0.0.beta2', '< 3'
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
gem 'sinja'
|
12
|
-
gem 'sinja-sequel'
|
13
|
-
end
|
6
|
+
gem 'sinja',
|
7
|
+
git: 'https://github.com/mwpastore/sinja.git', branch: 'master'
|
8
|
+
gem 'sinja-sequel',
|
9
|
+
git: 'https://github.com/mwpastore/sinja-sequel.git', branch: 'master'
|
14
10
|
gem 'sqlite3', '~> 1.3'
|
data/demo-app/README.md
CHANGED
@@ -2,9 +2,10 @@
|
|
2
2
|
|
3
3
|
This is the demo app for Sinja, provided both as an example of and for testing
|
4
4
|
Sinja. It uses [Sequel ORM](http://sequel.jeremyevans.net) with an in-memory
|
5
|
-
SQLite database and demonstrates the [Sequel
|
6
|
-
|
7
|
-
simplistic blog-like
|
5
|
+
SQLite database and demonstrates the [Sequel
|
6
|
+
extension](https://github.com/mwpastore/sinja-sequel) for Sinja. It works under
|
7
|
+
both MRI/YARV 2.3+ and JRuby 9.1+. It is a very simplistic blog-like
|
8
|
+
application with [database
|
8
9
|
tables](http://sequel.jeremyevans.net/rdoc/files/doc/schema_modification_rdoc.html),
|
9
10
|
[models](http://sequel.jeremyevans.net/rdoc/files/README_rdoc.html#label-Sequel+Models),
|
10
11
|
[serializers](https://github.com/fotinakis/jsonapi-serializers), and
|
data/demo-app/app.rb
CHANGED
data/lib/sinja/version.rb
CHANGED
data/sinja.gemspec
CHANGED
@@ -31,9 +31,9 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.add_development_dependency 'jdbc-sqlite3', '~> 3.8' if defined?(JRUBY_VERSION)
|
32
32
|
spec.add_development_dependency 'minitest', '~> 5.9'
|
33
33
|
spec.add_development_dependency 'minitest-hooks', '~> 1.4'
|
34
|
-
#spec.add_development_dependency 'munson', '~> 0.4'
|
34
|
+
#spec.add_development_dependency 'munson', '~> 0.4' # in Gemfile
|
35
35
|
spec.add_development_dependency 'rake', '~> 12.0'
|
36
36
|
spec.add_development_dependency 'sequel', '~> 4.41'
|
37
|
-
spec.add_development_dependency 'sinja-sequel', '~> 0.1'
|
37
|
+
#spec.add_development_dependency 'sinja-sequel', '~> 0.1' # in Gemfile
|
38
38
|
spec.add_development_dependency 'sqlite3', '~> 1.3' if !defined?(JRUBY_VERSION)
|
39
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinja
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.0.
|
4
|
+
version: 1.2.0.pre3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Pastore
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -194,20 +194,6 @@ dependencies:
|
|
194
194
|
- - "~>"
|
195
195
|
- !ruby/object:Gem::Version
|
196
196
|
version: '4.41'
|
197
|
-
- !ruby/object:Gem::Dependency
|
198
|
-
name: sinja-sequel
|
199
|
-
requirement: !ruby/object:Gem::Requirement
|
200
|
-
requirements:
|
201
|
-
- - "~>"
|
202
|
-
- !ruby/object:Gem::Version
|
203
|
-
version: '0.1'
|
204
|
-
type: :development
|
205
|
-
prerelease: false
|
206
|
-
version_requirements: !ruby/object:Gem::Requirement
|
207
|
-
requirements:
|
208
|
-
- - "~>"
|
209
|
-
- !ruby/object:Gem::Version
|
210
|
-
version: '0.1'
|
211
197
|
- !ruby/object:Gem::Dependency
|
212
198
|
name: sqlite3
|
213
199
|
requirement: !ruby/object:Gem::Requirement
|
@@ -250,20 +236,6 @@ files:
|
|
250
236
|
- demo-app/classes/post.rb
|
251
237
|
- demo-app/classes/tag.rb
|
252
238
|
- demo-app/database.rb
|
253
|
-
- extensions/sequel/Gemfile
|
254
|
-
- extensions/sequel/LICENSE.txt
|
255
|
-
- extensions/sequel/README.md
|
256
|
-
- extensions/sequel/Rakefile
|
257
|
-
- extensions/sequel/bin/console
|
258
|
-
- extensions/sequel/bin/setup
|
259
|
-
- extensions/sequel/lib/sinatra/jsonapi/sequel.rb
|
260
|
-
- extensions/sequel/lib/sinja-sequel.rb
|
261
|
-
- extensions/sequel/lib/sinja/sequel.rb
|
262
|
-
- extensions/sequel/lib/sinja/sequel/core.rb
|
263
|
-
- extensions/sequel/lib/sinja/sequel/helpers.rb
|
264
|
-
- extensions/sequel/lib/sinja/sequel/version.rb
|
265
|
-
- extensions/sequel/sinja-sequel.gemspec
|
266
|
-
- extensions/sequel/test/test_helper.rb
|
267
239
|
- lib/sinatra/jsonapi.rb
|
268
240
|
- lib/sinja.rb
|
269
241
|
- lib/sinja/config.rb
|
data/extensions/sequel/Gemfile
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2016 Mike Pastore
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
data/extensions/sequel/README.md
DELETED
@@ -1,274 +0,0 @@
|
|
1
|
-
# Sinja::Sequel
|
2
|
-
|
3
|
-
<!--
|
4
|
-
Title: Sinja::Sequel
|
5
|
-
Description: Sequel-specific Helpers and DSL for Sinja
|
6
|
-
Author: Mike Pastore
|
7
|
-
Keywords: Ruby, Sinatra, Sinatra::JSONAPI, Sinja, Sequel
|
8
|
-
-->
|
9
|
-
|
10
|
-
[![Gem Version](https://badge.fury.io/rb/sinja-sequel.svg)](https://badge.fury.io/rb/sinja-sequel)
|
11
|
-
|
12
|
-
Sinja::Sequel configures your [Sinja][1] application to work with [Sequel][2]
|
13
|
-
out of the box, and provides additional helpers to greatly simplify the process
|
14
|
-
of writing the more complex action helpers (specifically `replace`, `merge`,
|
15
|
-
and `subtract`). An optional extension enhances Sinja's DSL to generate basic
|
16
|
-
action helpers that can be overridden, customized, or removed.
|
17
|
-
|
18
|
-
The core configuration and helpers are in pretty good shape (Sinja uses them in
|
19
|
-
its [demo app][3] and test suite), but the extension could use some fleshing
|
20
|
-
out. Testers and community contributions welcome!
|
21
|
-
|
22
|
-
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
23
|
-
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
24
|
-
|
25
|
-
|
26
|
-
- [Installation](#installation)
|
27
|
-
- [Usage](#usage)
|
28
|
-
- [Core](#core)
|
29
|
-
- [Helpers](#helpers)
|
30
|
-
- [`next_pk`](#next_pk)
|
31
|
-
- [`add_missing`](#add_missing)
|
32
|
-
- [`remove_present`](#remove_present)
|
33
|
-
- [`add_remove`](#add_remove)
|
34
|
-
- [Extension](#extension)
|
35
|
-
- [Development](#development)
|
36
|
-
- [Contributing](#contributing)
|
37
|
-
- [License](#license)
|
38
|
-
|
39
|
-
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
40
|
-
|
41
|
-
## Installation
|
42
|
-
|
43
|
-
Add this line to your application's Gemfile:
|
44
|
-
|
45
|
-
```ruby
|
46
|
-
gem 'sinja-sequel'
|
47
|
-
```
|
48
|
-
|
49
|
-
And then execute:
|
50
|
-
|
51
|
-
```sh
|
52
|
-
$ bundle
|
53
|
-
```
|
54
|
-
|
55
|
-
Or install it yourself as:
|
56
|
-
|
57
|
-
```sh
|
58
|
-
$ gem install sinja-sequel
|
59
|
-
```
|
60
|
-
|
61
|
-
## Usage
|
62
|
-
|
63
|
-
Always return Sequel datasets (instead of arrays of objects) from your `index`
|
64
|
-
(e.g. `Foo.dataset`) and `fetch` (e.g. `resource.bars_dataset`) action
|
65
|
-
helpers. The `finalize` helper, described below, will ensure they are
|
66
|
-
"rasterized" before being passed to [JSONAPI::Serializers][5].
|
67
|
-
|
68
|
-
You'll want to enable Sequel's `:tactical_eager_loading` plugin for the best
|
69
|
-
performance with JSONAPI::Serializers. I've [seen][6] it reduce complex
|
70
|
-
serializations by a factor of 100 (i.e. quite literally one query instead of
|
71
|
-
100).
|
72
|
-
|
73
|
-
If you want to use client-generated IDs, enable the `:update_primary_key`
|
74
|
-
plugin on the model and call `unrestrict_primary_key` in the model definition
|
75
|
-
to allow mass assignment (e.g. with `Sequel::Model#set_fields`).
|
76
|
-
|
77
|
-
If your model has foreign keys and you want to enforce a non-nullable
|
78
|
-
constraint at the application level, consider enabling the
|
79
|
-
`:validation_helpers` plugin on the model and using `validates_not_null` in
|
80
|
-
conjuction with the `validate!` helper described below:
|
81
|
-
|
82
|
-
```ruby
|
83
|
-
class Bar < Sequel::Model
|
84
|
-
plugin :validation_helpers
|
85
|
-
|
86
|
-
def validate
|
87
|
-
super
|
88
|
-
validates_not_null :foo
|
89
|
-
end
|
90
|
-
end
|
91
|
-
```
|
92
|
-
|
93
|
-
See "Avoding Null Foreign Keys" in the [Sinja][1] documentation for more
|
94
|
-
information.
|
95
|
-
|
96
|
-
Finally, enable the `:pagination` extension on your connection (before
|
97
|
-
prepending Core) to enable pagination!
|
98
|
-
|
99
|
-
### Core
|
100
|
-
|
101
|
-
Prepend [Sinja::Sequel::Core](/extensions/sequel/lib/sinja/sequel/core.rb)
|
102
|
-
after registering Sinja:
|
103
|
-
|
104
|
-
```ruby
|
105
|
-
require 'sinja'
|
106
|
-
require 'sinja/sequel/core'
|
107
|
-
|
108
|
-
class MyApp < Sinatra::Base
|
109
|
-
register Sinja
|
110
|
-
|
111
|
-
helpers do
|
112
|
-
prepend Sinja::Sequel::Core
|
113
|
-
end
|
114
|
-
end
|
115
|
-
```
|
116
|
-
|
117
|
-
Note that you must use `prepend` (instead of including Sinja::Sequel::Core like
|
118
|
-
a normal module of Sinatra helpers) in order to ensure that the included
|
119
|
-
methods take precedence over Sinja's method stubs (e.g. `transaction`).
|
120
|
-
[This][4] will hopefully be fixed in a future version of Sinatra.
|
121
|
-
|
122
|
-
Prepending Core does the following to your application:
|
123
|
-
|
124
|
-
* Configures `conflict_`, `not_found_`, and `validation_exceptions`, and
|
125
|
-
`validation_formatter`.
|
126
|
-
* Defines a `database` helper that delegates to `Sequel::Model.db`.
|
127
|
-
* Defines a `transaction` helper that delegates to `database.transaction`.
|
128
|
-
* Defines a `validate!` helper that raises an error if `resource` is invalid
|
129
|
-
after a `create` or `update` action helper invocation.
|
130
|
-
* Defines a simple equality-based `filter` helper that passes the filter params
|
131
|
-
to `Sequel::Dataset#where`.
|
132
|
-
* Defines a `sort` helper that applies `Sequel.asc` and `Sequel.desc` to the
|
133
|
-
sort terms and passes them to `Sequel::Dataset#order`.
|
134
|
-
* Defines a `finalize` helper that simply calls `Sequel::Dataset#all`.
|
135
|
-
|
136
|
-
If the `:pagination` Sequel extension is loaded, it also does the following:
|
137
|
-
|
138
|
-
* Configures `page_using` for page number- and size-based pagination, with an
|
139
|
-
additional record count parameter to avoid repetitive `SELECT COUNT` queries
|
140
|
-
while paging.
|
141
|
-
* Defines a `page` helper that calls `Sequel::Dataset#paginate` and computes a
|
142
|
-
hash of page params that Sinja will use to construct the root pagination
|
143
|
-
links and add to the root metadata of the response.
|
144
|
-
|
145
|
-
You may override any of the installed helpers by defining your own. Please see
|
146
|
-
the [Sinja][1] documentation for more information about Sinja hooks and
|
147
|
-
configurables, and the [Sequel][2] documentation for more information about
|
148
|
-
Sequel plugins and features.
|
149
|
-
|
150
|
-
### Helpers
|
151
|
-
|
152
|
-
Include
|
153
|
-
[Sinja::Sequel::Helpers](/extensions/sequel/lib/sinja/sequel/helpers.rb) after
|
154
|
-
registering Sinja:
|
155
|
-
|
156
|
-
```ruby
|
157
|
-
require 'sinja'
|
158
|
-
require 'sinja/sequel/helpers'
|
159
|
-
|
160
|
-
class MyApp < Sinatra::Base
|
161
|
-
register Sinja
|
162
|
-
|
163
|
-
helpers Sinja::Sequel::Helpers
|
164
|
-
end
|
165
|
-
```
|
166
|
-
|
167
|
-
This is the most common use-case. **Note that including Helpers will
|
168
|
-
automatically prepend Core!**
|
169
|
-
|
170
|
-
#### `next_pk`
|
171
|
-
|
172
|
-
A convenience method to always return the primary key of the resource and the
|
173
|
-
resource from your `create` action helpers. Simply use it instead of `next`!
|
174
|
-
|
175
|
-
```ruby
|
176
|
-
create do |attr|
|
177
|
-
next_pk Foo.create(attr)
|
178
|
-
end
|
179
|
-
```
|
180
|
-
|
181
|
-
#### `add_missing`
|
182
|
-
|
183
|
-
Take the key of a Sequel \*_to_many association and an array of resource
|
184
|
-
identifier objects and add the "missing" records to the collection. Makes
|
185
|
-
writing your `merge` action helpers a breeze!
|
186
|
-
|
187
|
-
```ruby
|
188
|
-
has_many :bars do
|
189
|
-
merge do |rios|
|
190
|
-
add_missing(:bars, rios)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
```
|
194
|
-
|
195
|
-
It will try to cast the ID of each resource identifier object by sending it the
|
196
|
-
`:to_i` method; pass in a third argument to specify a different method (e.g. if
|
197
|
-
the primary key of the `bars` table is a `varchar`, pass in `:to_s` instead).
|
198
|
-
|
199
|
-
This helper also takes an optional block that can be used to filter
|
200
|
-
subresources during processing. Simply return a truthy or falsey value from the
|
201
|
-
block (or raise an error to abort the entire transaction):
|
202
|
-
|
203
|
-
```ruby
|
204
|
-
has_many :bars do
|
205
|
-
merge do |rios|
|
206
|
-
add_missing(:bars, rios) do |bar|
|
207
|
-
role?(:admin) || bar.owner == resource.owner
|
208
|
-
end
|
209
|
-
end
|
210
|
-
end
|
211
|
-
```
|
212
|
-
|
213
|
-
#### `remove_present`
|
214
|
-
|
215
|
-
Like `add_missing`, but removes the "present" records from the collection.
|
216
|
-
Makes writing your `subtract` action helpers a breeze!
|
217
|
-
|
218
|
-
#### `add_remove`
|
219
|
-
|
220
|
-
Like `add_missing` and `remove_present`, but performs an efficient delta
|
221
|
-
operation on the collection. Makes writing your `replace` action helpers a
|
222
|
-
breeze!
|
223
|
-
|
224
|
-
### Extension
|
225
|
-
|
226
|
-
Register [Sinja::Sequel](/extensions/sequel/lib/sinja/sequel.rb) after
|
227
|
-
registering Sinja:
|
228
|
-
|
229
|
-
```ruby
|
230
|
-
require 'sinja'
|
231
|
-
require 'sinja/sequel'
|
232
|
-
|
233
|
-
class MyApp < Sinatra::Base
|
234
|
-
register Sinja
|
235
|
-
register Sinja::Sequel
|
236
|
-
end
|
237
|
-
```
|
238
|
-
|
239
|
-
**Note that registering the extension will automatically include Helpers!**
|
240
|
-
|
241
|
-
After registering the extension, the `resource`, `has_many`, and `has_one` DSL
|
242
|
-
keywords will generate basic action helpers. The default `create` action helper
|
243
|
-
does not support client-generated IDs. These action helpers can be subsequently
|
244
|
-
overridden, customized by setting action helper options (i.e. `:roles`) and/or
|
245
|
-
defining `before_<action>` hooks, or removed entirely with `remove_<action>`.
|
246
|
-
|
247
|
-
## Development
|
248
|
-
|
249
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
250
|
-
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
251
|
-
prompt that will allow you to experiment.
|
252
|
-
|
253
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To
|
254
|
-
release a new version, update the version number in `version.rb`, and then run
|
255
|
-
`bundle exec rake release`, which will create a git tag for the version, push
|
256
|
-
git commits and tags, and push the `.gem` file to
|
257
|
-
[rubygems.org](https://rubygems.org).
|
258
|
-
|
259
|
-
## Contributing
|
260
|
-
|
261
|
-
Bug reports and pull requests are welcome on GitHub at
|
262
|
-
https://github.com/mwpastore/sinja.
|
263
|
-
|
264
|
-
## License
|
265
|
-
|
266
|
-
The gem is available as open source under the terms of the [MIT
|
267
|
-
License](http://opensource.org/licenses/MIT).
|
268
|
-
|
269
|
-
[1]: https://github.com/mwpastore/sinja
|
270
|
-
[2]: http://sequel.jeremyevans.net
|
271
|
-
[3]: https://github.com/mwpastore/sinja/tree/master/demo-app
|
272
|
-
[4]: https://github.com/sinatra/sinatra/issues/1213
|
273
|
-
[5]: https://github.com/fotinakis/jsonapi-serializers
|
274
|
-
[6]: https://github.com/fotinakis/jsonapi-serializers/pull/31#issuecomment-148193366
|
data/extensions/sequel/Rakefile
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'bundler/setup'
|
4
|
-
require 'sinja-sequel'
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require 'pry'
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require 'irb'
|
14
|
-
IRB.start
|
data/extensions/sequel/bin/setup
DELETED
@@ -1,110 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'sinja/sequel/helpers'
|
3
|
-
require 'sinja/sequel/version'
|
4
|
-
|
5
|
-
module Sinja
|
6
|
-
module Sequel
|
7
|
-
def self.registered(app)
|
8
|
-
app.helpers Helpers
|
9
|
-
end
|
10
|
-
|
11
|
-
def resource(res, try_convert=:to_i, &block)
|
12
|
-
klass = res.to_s.classify.constantize
|
13
|
-
|
14
|
-
super(res) do
|
15
|
-
register Resource
|
16
|
-
|
17
|
-
helpers do
|
18
|
-
def find(id)
|
19
|
-
klass[id.send(try_convert)]
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
show
|
24
|
-
|
25
|
-
show_many do |ids|
|
26
|
-
klass.where(klass.primary_key=>ids.map(&try_convert)).all
|
27
|
-
end
|
28
|
-
|
29
|
-
index do
|
30
|
-
klass.dataset
|
31
|
-
end
|
32
|
-
|
33
|
-
create do |attr|
|
34
|
-
tmp = klass.new
|
35
|
-
if respond_to?(:settable_fields)
|
36
|
-
tmp.set_fields(attr, settable_fields)
|
37
|
-
else
|
38
|
-
tmp.set(attr)
|
39
|
-
end
|
40
|
-
tmp.save(:validate=>false)
|
41
|
-
next_pk tmp
|
42
|
-
end
|
43
|
-
|
44
|
-
update do |attr|
|
45
|
-
if respond_to?(:settable_fields)
|
46
|
-
resource.update_fields(attr, settable_fields, :validate=>false, :missing=>:skip)
|
47
|
-
else
|
48
|
-
resource.set(attr)
|
49
|
-
resource.save_changes(:validate=>false)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
destroy do
|
54
|
-
resource.destroy
|
55
|
-
end
|
56
|
-
|
57
|
-
instance_eval(&block) if block
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
module Resource
|
62
|
-
def has_one(rel, &block)
|
63
|
-
super(rel) do
|
64
|
-
pluck do
|
65
|
-
resource.send(rel)
|
66
|
-
end
|
67
|
-
|
68
|
-
prune(:sideload_on=>:update) do
|
69
|
-
resource.send("#{rel}=", nil)
|
70
|
-
resource.save_changes
|
71
|
-
end
|
72
|
-
|
73
|
-
graft(:sideload_on=>%i[create update]) do |rio|
|
74
|
-
klass = resource.class.association_reflection(rel).associated_class
|
75
|
-
resource.send("#{rel}=", klass.with_pk!(rio[:id]))
|
76
|
-
resource.save_changes(:validate=>!sideloaded?)
|
77
|
-
end
|
78
|
-
|
79
|
-
instance_eval(&block) if block
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def has_many(rel, &block)
|
84
|
-
super(rel) do
|
85
|
-
fetch do
|
86
|
-
resource.send("#{rel}_dataset")
|
87
|
-
end
|
88
|
-
|
89
|
-
clear(:sideload_on=>:update) do
|
90
|
-
resource.send("remove_all_#{rel}")
|
91
|
-
end
|
92
|
-
|
93
|
-
replace(:sideload_on=>:update) do |rios|
|
94
|
-
add_remove(rel, rios)
|
95
|
-
end
|
96
|
-
|
97
|
-
merge(:sideload_on=>:create) do |rios|
|
98
|
-
add_missing(rel, rios)
|
99
|
-
end
|
100
|
-
|
101
|
-
subtract do |rios|
|
102
|
-
remove_present(rel, rios)
|
103
|
-
end
|
104
|
-
|
105
|
-
instance_eval(&block) if block
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'forwardable'
|
3
|
-
require 'sequel'
|
4
|
-
|
5
|
-
module Sinja
|
6
|
-
module Sequel
|
7
|
-
module Core
|
8
|
-
extend Forwardable
|
9
|
-
|
10
|
-
def self.prepended(base)
|
11
|
-
base.sinja do |c|
|
12
|
-
c.conflict_exceptions << ::Sequel::ConstraintViolation
|
13
|
-
c.not_found_exceptions << ::Sequel::NoMatchingRow
|
14
|
-
c.validation_exceptions << ::Sequel::ValidationFailed
|
15
|
-
c.validation_formatter = ->(e) { e.errors.keys.zip(e.errors.full_messages) }
|
16
|
-
end
|
17
|
-
|
18
|
-
base.include Pagination if ::Sequel::Database::EXTENSIONS.key?(:pagination)
|
19
|
-
end
|
20
|
-
|
21
|
-
def_delegator ::Sequel::Model, :db, :database
|
22
|
-
|
23
|
-
def_delegator :database, :transaction
|
24
|
-
|
25
|
-
define_method :filter, proc(&:where)
|
26
|
-
|
27
|
-
def sort(collection, fields)
|
28
|
-
collection.order(*fields.map { |k, v| ::Sequel.send(v, k) })
|
29
|
-
end
|
30
|
-
|
31
|
-
define_method :finalize, proc(&:all)
|
32
|
-
|
33
|
-
def validate!
|
34
|
-
raise ::Sequel::ValidationFailed, resource unless resource.valid?
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
module Pagination
|
39
|
-
def self.included(base)
|
40
|
-
base.sinja { |c| c.page_using = {
|
41
|
-
:number=>1,
|
42
|
-
:size=>10,
|
43
|
-
:record_count=>nil
|
44
|
-
}}
|
45
|
-
end
|
46
|
-
|
47
|
-
def page(collection, opts)
|
48
|
-
collection = collection.dataset unless collection.respond_to?(:paginate)
|
49
|
-
|
50
|
-
opts = settings._sinja.page_using.merge(opts)
|
51
|
-
collection = collection.paginate opts[:number].to_i, opts[:size].to_i,
|
52
|
-
(opts[:record_count].to_i if opts[:record_count])
|
53
|
-
|
54
|
-
# Attributes common to all pagination links
|
55
|
-
base = {
|
56
|
-
:size=>collection.page_size,
|
57
|
-
:record_count=>collection.pagination_record_count
|
58
|
-
}
|
59
|
-
|
60
|
-
pagination = {
|
61
|
-
:first=>base.merge(:number=>1),
|
62
|
-
:self=>base.merge(:number=>collection.current_page),
|
63
|
-
:last=>base.merge(:number=>collection.page_count)
|
64
|
-
}
|
65
|
-
pagination[:next] = base.merge(:number=>collection.next_page) if collection.next_page
|
66
|
-
pagination[:prev] = base.merge(:number=>collection.prev_page) if collection.prev_page
|
67
|
-
|
68
|
-
return collection, pagination
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -1,78 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'sinja/sequel/core'
|
3
|
-
|
4
|
-
module Sinja
|
5
|
-
module Sequel
|
6
|
-
module Helpers
|
7
|
-
def self.included(base)
|
8
|
-
base.prepend Core
|
9
|
-
end
|
10
|
-
|
11
|
-
def next_pk(resource, opts={})
|
12
|
-
[resource.pk, resource, opts]
|
13
|
-
end
|
14
|
-
|
15
|
-
def add_remove(association, rios, try_convert=:to_i)
|
16
|
-
meth_suffix = association.to_s.singularize
|
17
|
-
add_meth = "add_#{meth_suffix}".to_sym
|
18
|
-
remove_meth = "remove_#{meth_suffix}".to_sym
|
19
|
-
|
20
|
-
dataset = resource.send("#{association}_dataset")
|
21
|
-
klass = dataset.association_reflection.associated_class
|
22
|
-
|
23
|
-
# does not / will not work with composite primary keys
|
24
|
-
new_ids = rios.map { |rio| rio[:id].send(try_convert) }
|
25
|
-
transaction do
|
26
|
-
resource.lock!
|
27
|
-
old_ids = dataset.select_map(klass.primary_key)
|
28
|
-
in_common = old_ids & new_ids
|
29
|
-
|
30
|
-
(new_ids - in_common).each do |id|
|
31
|
-
subresource = klass.with_pk!(id)
|
32
|
-
resource.send(add_meth, subresource) \
|
33
|
-
unless block_given? && !yield(subresource)
|
34
|
-
end
|
35
|
-
|
36
|
-
(old_ids - in_common).each do |id|
|
37
|
-
subresource = klass.with_pk!(id)
|
38
|
-
resource.send(remove_meth, subresource) \
|
39
|
-
unless block_given? && !yield(subresource)
|
40
|
-
end
|
41
|
-
|
42
|
-
resource.reload
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def add_missing(*args, &block)
|
47
|
-
add_or_remove(:add, :-, *args, &block)
|
48
|
-
end
|
49
|
-
|
50
|
-
def remove_present(*args, &block)
|
51
|
-
add_or_remove(:remove, :&, *args, &block)
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
def add_or_remove(meth_prefix, operator, association, rios, try_convert=:to_i)
|
57
|
-
meth = "#{meth_prefix}_#{association.to_s.singularize}".to_sym
|
58
|
-
transaction do
|
59
|
-
resource.lock!
|
60
|
-
venn(operator, association, rios, try_convert) do |subresource|
|
61
|
-
resource.send(meth, subresource) \
|
62
|
-
unless block_given? && !yield(subresource)
|
63
|
-
end
|
64
|
-
resource.reload
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def venn(operator, association, rios, try_convert)
|
69
|
-
dataset = resource.send("#{association}_dataset")
|
70
|
-
klass = dataset.association_reflection.associated_class
|
71
|
-
# does not / will not work with composite primary keys
|
72
|
-
rios.map { |rio| rio[:id].send(try_convert) }
|
73
|
-
.send(operator, dataset.select_map(klass.primary_key))
|
74
|
-
.each { |id| yield klass.with_pk!(id) }
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'sinja/sequel/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = 'sinja-sequel'
|
8
|
-
spec.version = Sinja::Sequel::VERSION
|
9
|
-
spec.authors = ['Mike Pastore']
|
10
|
-
spec.email = ['mike@oobak.org']
|
11
|
-
|
12
|
-
spec.summary = 'Sequel-specific Helpers and DSL for Sinja'
|
13
|
-
spec.homepage = 'https://github.com/mwpastore/sinja/tree/master/extensions/sequel'
|
14
|
-
spec.license = 'MIT'
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
-
f.match(%r{^(test|spec|features)/})
|
18
|
-
end
|
19
|
-
spec.require_paths = %w[lib]
|
20
|
-
|
21
|
-
spec.required_ruby_version = '>= 2.3.0'
|
22
|
-
|
23
|
-
spec.add_dependency 'sequel', '~> 4.0'
|
24
|
-
spec.add_dependency 'sinja', '>= 1.2.0.pre2', '< 2'
|
25
|
-
|
26
|
-
spec.add_development_dependency 'bundler', '~> 1.11'
|
27
|
-
spec.add_development_dependency 'minitest', '~> 5.9'
|
28
|
-
spec.add_development_dependency 'rake', '~> 12.0'
|
29
|
-
end
|