hanami-model 0.6.0 → 0.6.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/EXAMPLE.md +3 -2
- data/README.md +16 -2
- data/lib/hanami/model.rb +0 -37
- data/lib/hanami/model/adapters/sql/command.rb +25 -10
- data/lib/hanami/model/error.rb +73 -1
- data/lib/hanami/model/version.rb +1 -1
- 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: 29439a9ce3f9aa3cdd0600a6a04a0316209eb91d
|
|
4
|
+
data.tar.gz: fa6bb573a2e58f14cc3b09126168fdc4ed88f72a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 10a26d11cb59a13d93664a4e9fd29d35f8fa6ce788d95281b7b805ed04cf9a4ca390ace931c3a943ee65fc8a55b138f9931d5c351a50d09b19d8acf8b1845cae
|
|
7
|
+
data.tar.gz: 5d5ef5cf0bdee581b7d4d9272fd064c3c8973ea4a6da813fe318f49c24b00a285c6a4b2a410166b19560ddbc6a4a9a69f4a3221d79efc952f3d40e8234599a63
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# Hanami::Model
|
|
2
2
|
A persistence layer for Hanami
|
|
3
3
|
|
|
4
|
+
## v0.6.1 - 2016-02-05
|
|
5
|
+
### Changed
|
|
6
|
+
- [Hélio Costa e Silva & Pascal Betz] Mapping SQL Adapter's errors as `Hanami::Model` errors
|
|
7
|
+
|
|
4
8
|
## v0.6.0 - 2016-01-22
|
|
5
9
|
### Changed
|
|
6
10
|
- [Luca Guidi] Renamed the project
|
data/EXAMPLE.md
CHANGED
|
@@ -58,7 +58,8 @@ We have two entities in our application: `Author` and `Article`.
|
|
|
58
58
|
```ruby
|
|
59
59
|
Author = Struct.new(:id, :name) do
|
|
60
60
|
def initialize(attributes = {})
|
|
61
|
-
|
|
61
|
+
self.id = attributes[:id]
|
|
62
|
+
self.name = attributes[:name]
|
|
62
63
|
end
|
|
63
64
|
end
|
|
64
65
|
|
|
@@ -159,7 +160,7 @@ We instantiate and persist an `Author` and a few `Articles` for our example:
|
|
|
159
160
|
|
|
160
161
|
```ruby
|
|
161
162
|
author = Author.new(name: 'Luca')
|
|
162
|
-
AuthorRepository.create(author)
|
|
163
|
+
author = AuthorRepository.create(author)
|
|
163
164
|
|
|
164
165
|
articles = [
|
|
165
166
|
Article.new(title: 'Announcing Hanami', author_id: author.id, comments_count: 123, published: true),
|
data/README.md
CHANGED
|
@@ -19,8 +19,8 @@ Like all the other Hanami components, it can be used as a standalone framework o
|
|
|
19
19
|
|
|
20
20
|
[](http://badge.fury.io/rb/hanami-model)
|
|
21
21
|
[](http://travis-ci.org/hanami/model?branch=master)
|
|
22
|
-
[](https://coveralls.io/github/hanami/model?branch=master)
|
|
23
|
+
[](https://codeclimate.com/github/hanami/model)
|
|
24
24
|
[](https://gemnasium.com/hanami/model)
|
|
25
25
|
[](http://inch-ci.org/github/hanami/model)
|
|
26
26
|
|
|
@@ -470,6 +470,20 @@ If you need more information regarding those methods, you can use comments from
|
|
|
470
470
|
|
|
471
471
|
Think of an adapter for Redis, it will probably employ different strategies to filter records than an SQL query object.
|
|
472
472
|
|
|
473
|
+
### Model Error Coercions
|
|
474
|
+
|
|
475
|
+
All adapters' errors are encapsulated into Hanami error classes.
|
|
476
|
+
|
|
477
|
+
Hanami Model may raise the following exceptions:
|
|
478
|
+
|
|
479
|
+
* `Hanami::Model::UniqueConstraintViolationError`
|
|
480
|
+
* `Hanami::Model::ForeignKeyConstraintViolationError`
|
|
481
|
+
* `Hanami::Model::NotNullConstraintViolationError`
|
|
482
|
+
* `Hanami::Model::CheckConstraintViolationError`
|
|
483
|
+
|
|
484
|
+
For any other adapter's errors, Hanami will raise the `Hanami::Model::InvalidCommandError` object.
|
|
485
|
+
All errors contains the root cause and the full error message thrown by sql adapter.
|
|
486
|
+
|
|
473
487
|
### Conventions
|
|
474
488
|
|
|
475
489
|
* A repository must be named after an entity, by appending `"Repository"` to the entity class name (eg. `Article` => `ArticleRepository`).
|
data/lib/hanami/model.rb
CHANGED
|
@@ -11,42 +11,6 @@ module Hanami
|
|
|
11
11
|
#
|
|
12
12
|
# @since 0.1.0
|
|
13
13
|
module Model
|
|
14
|
-
# Error for non persisted entity
|
|
15
|
-
# It's raised when we try to update or delete a non persisted entity.
|
|
16
|
-
#
|
|
17
|
-
# @since 0.1.0
|
|
18
|
-
#
|
|
19
|
-
# @see Hanami::Repository.update
|
|
20
|
-
class NonPersistedEntityError < Hanami::Model::Error
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# Error for invalid mapper configuration
|
|
24
|
-
# It's raised when mapping is not configured correctly
|
|
25
|
-
#
|
|
26
|
-
# @since 0.2.0
|
|
27
|
-
#
|
|
28
|
-
# @see Hanami::Configuration#mapping
|
|
29
|
-
class InvalidMappingError < Hanami::Model::Error
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# Error for invalid raw command syntax
|
|
33
|
-
#
|
|
34
|
-
# @since 0.5.0
|
|
35
|
-
class InvalidCommandError < Hanami::Model::Error
|
|
36
|
-
def initialize(message = "Invalid command")
|
|
37
|
-
super
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# Error for invalid raw query syntax
|
|
42
|
-
#
|
|
43
|
-
# @since 0.3.1
|
|
44
|
-
class InvalidQueryError < Hanami::Model::Error
|
|
45
|
-
def initialize(message = "Invalid query")
|
|
46
|
-
super
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
14
|
include Utils::ClassAttribute
|
|
51
15
|
|
|
52
16
|
# Framework configuration
|
|
@@ -204,6 +168,5 @@ module Hanami
|
|
|
204
168
|
duplicated.configure(&blk) if block_given?
|
|
205
169
|
end
|
|
206
170
|
end
|
|
207
|
-
|
|
208
171
|
end
|
|
209
172
|
end
|
|
@@ -9,6 +9,15 @@ module Hanami
|
|
|
9
9
|
# @api private
|
|
10
10
|
# @since 0.1.0
|
|
11
11
|
class Command
|
|
12
|
+
# @api private
|
|
13
|
+
# @since 0.6.1
|
|
14
|
+
SEQUEL_TO_HANAMI_ERROR_MAPPING = {
|
|
15
|
+
'Sequel::UniqueConstraintViolation' => Hanami::Model::UniqueConstraintViolationError,
|
|
16
|
+
'Sequel::ForeignKeyConstraintViolation' => Hanami::Model::ForeignKeyConstraintViolationError,
|
|
17
|
+
'Sequel::NotNullConstraintViolation' => Hanami::Model::NotNullConstraintViolationError,
|
|
18
|
+
'Sequel::CheckConstraintViolation' => Hanami::Model::CheckConstraintViolationError
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
12
21
|
# Initialize a command
|
|
13
22
|
#
|
|
14
23
|
# @param query [Hanami::Model::Adapters::Sql::Query]
|
|
@@ -30,9 +39,7 @@ module Hanami
|
|
|
30
39
|
# @api private
|
|
31
40
|
# @since 0.1.0
|
|
32
41
|
def create(entity)
|
|
33
|
-
@collection.insert(entity)
|
|
34
|
-
rescue Sequel::DatabaseError => e
|
|
35
|
-
raise Hanami::Model::Error.new(e.message)
|
|
42
|
+
_handle_database_error { @collection.insert(entity) }
|
|
36
43
|
end
|
|
37
44
|
|
|
38
45
|
# Updates the corresponding record for the given entity.
|
|
@@ -44,9 +51,7 @@ module Hanami
|
|
|
44
51
|
# @api private
|
|
45
52
|
# @since 0.1.0
|
|
46
53
|
def update(entity)
|
|
47
|
-
@collection.update(entity)
|
|
48
|
-
rescue Sequel::DatabaseError => e
|
|
49
|
-
raise Hanami::Model::Error.new(e.message)
|
|
54
|
+
_handle_database_error { @collection.update(entity) }
|
|
50
55
|
end
|
|
51
56
|
|
|
52
57
|
# Deletes all the records for the current query.
|
|
@@ -59,15 +64,25 @@ module Hanami
|
|
|
59
64
|
# @api private
|
|
60
65
|
# @since 0.1.0
|
|
61
66
|
def delete
|
|
62
|
-
@collection.delete
|
|
63
|
-
rescue Sequel::DatabaseError => e
|
|
64
|
-
raise Hanami::Model::Error.new(e.message)
|
|
67
|
+
_handle_database_error { @collection.delete }
|
|
65
68
|
end
|
|
66
69
|
|
|
67
70
|
alias_method :clear, :delete
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
# Handles any possible Adapter's Database Error
|
|
75
|
+
#
|
|
76
|
+
# @api private
|
|
77
|
+
# @since 0.6.1
|
|
78
|
+
def _handle_database_error
|
|
79
|
+
yield
|
|
80
|
+
rescue Sequel::DatabaseError => e
|
|
81
|
+
error_class = SEQUEL_TO_HANAMI_ERROR_MAPPING.fetch(e.class.name, Hanami::Model::InvalidCommandError)
|
|
82
|
+
raise error_class, e.message
|
|
83
|
+
end
|
|
68
84
|
end
|
|
69
85
|
end
|
|
70
86
|
end
|
|
71
87
|
end
|
|
72
88
|
end
|
|
73
|
-
|
data/lib/hanami/model/error.rb
CHANGED
|
@@ -1,7 +1,79 @@
|
|
|
1
1
|
module Hanami
|
|
2
2
|
module Model
|
|
3
|
+
|
|
4
|
+
# Default Error class
|
|
5
|
+
#
|
|
3
6
|
# @since 0.5.1
|
|
4
|
-
|
|
7
|
+
Error = Class.new(::StandardError)
|
|
8
|
+
|
|
9
|
+
# Error for non persisted entity
|
|
10
|
+
# It's raised when we try to update or delete a non persisted entity.
|
|
11
|
+
#
|
|
12
|
+
# @since 0.1.0
|
|
13
|
+
#
|
|
14
|
+
# @see Hanami::Repository.update
|
|
15
|
+
NonPersistedEntityError = Class.new(Error)
|
|
16
|
+
|
|
17
|
+
# Error for invalid mapper configuration
|
|
18
|
+
# It's raised when mapping is not configured correctly
|
|
19
|
+
#
|
|
20
|
+
# @since 0.2.0
|
|
21
|
+
#
|
|
22
|
+
# @see Hanami::Configuration#mapping
|
|
23
|
+
InvalidMappingError = Class.new(Error)
|
|
24
|
+
|
|
25
|
+
# Error for invalid raw command syntax
|
|
26
|
+
#
|
|
27
|
+
# @since 0.5.0
|
|
28
|
+
class InvalidCommandError < Error
|
|
29
|
+
def initialize(message = "Invalid command")
|
|
30
|
+
super
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Error for invalid raw query syntax
|
|
35
|
+
#
|
|
36
|
+
# @since 0.3.1
|
|
37
|
+
class InvalidQueryError < Error
|
|
38
|
+
def initialize(message = "Invalid query")
|
|
39
|
+
super
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Error for Unique Constraint Violation
|
|
44
|
+
#
|
|
45
|
+
# @since 0.6.1
|
|
46
|
+
class UniqueConstraintViolationError < Error
|
|
47
|
+
def initialize(message = "Unique constraint has been violated")
|
|
48
|
+
super
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Error for Foreign Key Constraint Violation
|
|
53
|
+
#
|
|
54
|
+
# @since 0.6.1
|
|
55
|
+
class ForeignKeyConstraintViolationError < Error
|
|
56
|
+
def initialize(message = "Foreign key constraint has been violated")
|
|
57
|
+
super
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Error for Not Null Constraint Violation
|
|
62
|
+
#
|
|
63
|
+
# @since 0.6.1
|
|
64
|
+
class NotNullConstraintViolationError < Error
|
|
65
|
+
def initialize(message = "NOT NULL constraint has been violated")
|
|
66
|
+
super
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Error for Check Constraint Violation raised by Sequel
|
|
71
|
+
#
|
|
72
|
+
# @since 0.6.1
|
|
73
|
+
class CheckConstraintViolationError < Error
|
|
74
|
+
def initialize(message = "Check constraint has been violated")
|
|
75
|
+
super
|
|
76
|
+
end
|
|
5
77
|
end
|
|
6
78
|
end
|
|
7
79
|
end
|
data/lib/hanami/model/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hanami-model
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Luca Guidi
|
|
@@ -10,7 +10,7 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date: 2016-
|
|
13
|
+
date: 2016-02-05 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: hanami-utils
|