hanami-model 1.2.0 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/LICENSE.md +1 -1
- data/README.md +10 -7
- data/hanami-model.gemspec +25 -20
- data/lib/hanami-model.rb +3 -1
- data/lib/hanami/entity.rb +6 -3
- data/lib/hanami/entity/schema.rb +10 -7
- data/lib/hanami/model.rb +15 -12
- data/lib/hanami/model/association.rb +7 -7
- data/lib/hanami/model/associations/belongs_to.rb +3 -1
- data/lib/hanami/model/associations/dsl.rb +2 -2
- data/lib/hanami/model/associations/has_many.rb +10 -8
- data/lib/hanami/model/associations/has_one.rb +9 -7
- data/lib/hanami/model/associations/many_to_many.rb +9 -11
- data/lib/hanami/model/configuration.rb +29 -10
- data/lib/hanami/model/configurator.rb +5 -3
- data/lib/hanami/model/entity_name.rb +4 -2
- data/lib/hanami/model/error.rb +18 -7
- data/lib/hanami/model/mapped_relation.rb +4 -2
- data/lib/hanami/model/mapping.rb +3 -1
- data/lib/hanami/model/migration.rb +2 -0
- data/lib/hanami/model/migrator.rb +7 -5
- data/lib/hanami/model/migrator/adapter.rb +14 -12
- data/lib/hanami/model/migrator/connection.rb +16 -9
- data/lib/hanami/model/migrator/logger.rb +3 -1
- data/lib/hanami/model/migrator/mysql_adapter.rb +23 -13
- data/lib/hanami/model/migrator/postgres_adapter.rb +31 -31
- data/lib/hanami/model/migrator/sqlite_adapter.rb +7 -9
- data/lib/hanami/model/plugins.rb +5 -3
- data/lib/hanami/model/plugins/mapping.rb +2 -0
- data/lib/hanami/model/plugins/schema.rb +2 -0
- data/lib/hanami/model/plugins/timestamps.rb +3 -0
- data/lib/hanami/model/relation_name.rb +4 -2
- data/lib/hanami/model/sql.rb +9 -7
- data/lib/hanami/model/sql/console.rb +10 -8
- data/lib/hanami/model/sql/consoles/abstract.rb +3 -1
- data/lib/hanami/model/sql/consoles/mysql.rb +4 -2
- data/lib/hanami/model/sql/consoles/postgresql.rb +10 -8
- data/lib/hanami/model/sql/consoles/sqlite.rb +6 -4
- data/lib/hanami/model/sql/entity/schema.rb +6 -4
- data/lib/hanami/model/sql/types.rb +27 -27
- data/lib/hanami/model/sql/types/schema/coercions.rb +5 -4
- data/lib/hanami/model/types.rb +4 -4
- data/lib/hanami/model/version.rb +3 -1
- data/lib/hanami/repository.rb +20 -31
- metadata +64 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e39e89062478fa891ec0904add24b1ec30bb93435b178f229cc4328995de9acb
|
4
|
+
data.tar.gz: 0c3190667360fa53e56797c14bac30fdb9fe3ca4266ce4f33dc2a1b941142efb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3420b442c9015651d7e0f6bd55861252919604c02784601d3177f28c959c986cd35308fa73db94e452624cec4a85a443fa9472354eafe8c8b5fb78dea46162c9
|
7
|
+
data.tar.gz: 99c9779f14b14484f33bd4789105bb96c83ce6165d1048eb9e0db0a7cc573ead79308ca00f7e135e743f22c506231c4a6f8b348da4c4596da4f23f00784a3121
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,27 @@
|
|
1
1
|
# Hanami::Model
|
2
2
|
A persistence layer for Hanami
|
3
3
|
|
4
|
+
## v1.3.3 - 2021-05-22
|
5
|
+
### Fixed
|
6
|
+
- [Sean Collins] Specify dependency on BigDecimal v1.4
|
7
|
+
- [Adam Daniels] Use environment variables for PostgreSQL CLI tools
|
8
|
+
|
9
|
+
## v1.3.2 - 2019-01-31
|
10
|
+
### Fixed
|
11
|
+
- [Luca Guidi] Depend on `dry-logic` `~> 0.4.2`, `< 0.5`
|
12
|
+
|
13
|
+
## v1.3.1 - 2019-01-18
|
14
|
+
### Added
|
15
|
+
- [Luca Guidi] Official support for Ruby: MRI 2.6
|
16
|
+
- [Luca Guidi] Support `bundler` 2.0+
|
17
|
+
|
18
|
+
## v1.3.0 - 2018-10-24
|
19
|
+
|
20
|
+
## v1.3.0.beta1 - 2018-08-08
|
21
|
+
### Fixed
|
22
|
+
- [Luca Guidi] Print meaningful error message when connection URL is misconfigured (eg. `Unknown database adapter for URL: "". Please check your database configuration (hint: ENV['DATABASE_URL']).`)
|
23
|
+
- [Ian Ker-Seymer] Reliably parse query params from connection string
|
24
|
+
|
4
25
|
## v1.2.0 - 2018-04-11
|
5
26
|
|
6
27
|
## v1.2.0.rc2 - 2018-04-06
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -14,12 +14,11 @@ Like all the other Hanami components, it can be used as a standalone framework o
|
|
14
14
|
|
15
15
|
## Status
|
16
16
|
|
17
|
-
[![Gem Version](https://badge.fury.io/rb/hanami-model.svg)](
|
18
|
-
[![
|
19
|
-
[![Coverage](https://codecov.io/gh/hanami/model/branch/master/graph/badge.svg)](https://codecov.io/gh/hanami/model)
|
20
|
-
[![
|
21
|
-
[![
|
22
|
-
[![Inline docs](http://inch-ci.org/github/hanami/model.png)](http://inch-ci.org/github/hanami/model)
|
17
|
+
[![Gem Version](https://badge.fury.io/rb/hanami-model.svg)](https://badge.fury.io/rb/hanami-model)
|
18
|
+
[![CI](https://github.com/hanami/model/workflows/ci/badge.svg?branch=master)](https://github.com/hanami/model/actions?query=workflow%3Aci+branch%3Amaster)
|
19
|
+
[![Test Coverage](https://codecov.io/gh/hanami/model/branch/master/graph/badge.svg)](https://codecov.io/gh/hanami/model)
|
20
|
+
[![Depfu](https://badges.depfu.com/badges/3a5d3f9e72895493bb6f39402ac4f129/overview.svg)](https://depfu.com/github/hanami/model?project=Bundler)
|
21
|
+
[![Inline Docs](http://inch-ci.org/github/hanami/model.svg)](http://inch-ci.org/github/hanami/model)
|
23
22
|
|
24
23
|
## Contact
|
25
24
|
|
@@ -56,6 +55,7 @@ This class provides a DSL to configure the connection.
|
|
56
55
|
|
57
56
|
```ruby
|
58
57
|
require 'hanami/model'
|
58
|
+
require 'hanami/model/sql'
|
59
59
|
|
60
60
|
class User < Hanami::Entity
|
61
61
|
end
|
@@ -192,6 +192,8 @@ This is a **huge improvement**, because:
|
|
192
192
|
|
193
193
|
Hanami::Model can **_automap_** columns from relations and entities attributes.
|
194
194
|
|
195
|
+
When using a `sql` adapter, you must require `hanami/model/sql` before `Hanami::Model.load!` is called so the relations are loaded correctly.
|
196
|
+
|
195
197
|
However, there are cases where columns and attribute names do not match (mainly **legacy databases**).
|
196
198
|
|
197
199
|
```ruby
|
@@ -234,6 +236,7 @@ If an entity has the following accessors: `:created_at` and `:updated_at`, they
|
|
234
236
|
|
235
237
|
```ruby
|
236
238
|
require 'hanami/model'
|
239
|
+
require 'hanami/model/sql'
|
237
240
|
|
238
241
|
class User < Hanami::Entity
|
239
242
|
end
|
@@ -291,6 +294,6 @@ __Hanami::Model__ uses [Semantic Versioning 2.0.0](http://semver.org)
|
|
291
294
|
|
292
295
|
## Copyright
|
293
296
|
|
294
|
-
Copyright © 2014-
|
297
|
+
Copyright © 2014-2021 Luca Guidi – Released under MIT License
|
295
298
|
|
296
299
|
This project was formerly known as Lotus (`lotus-model`).
|
data/hanami-model.gemspec
CHANGED
@@ -1,31 +1,36 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
require
|
5
|
+
require "hanami/model/version"
|
4
6
|
|
5
7
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
8
|
+
spec.name = "hanami-model"
|
7
9
|
spec.version = Hanami::Model::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
10
|
-
spec.summary =
|
11
|
-
spec.description =
|
12
|
-
spec.homepage =
|
13
|
-
spec.license =
|
10
|
+
spec.authors = ["Luca Guidi"]
|
11
|
+
spec.email = ["me@lucaguidi.com"]
|
12
|
+
spec.summary = "A persistence layer for Hanami"
|
13
|
+
spec.description = "A persistence framework with entities and repositories"
|
14
|
+
spec.homepage = "http://hanamirb.org"
|
15
|
+
spec.license = "MIT"
|
14
16
|
|
15
17
|
spec.files = `git ls-files -z -- lib/* CHANGELOG.md EXAMPLE.md LICENSE.md README.md hanami-model.gemspec`.split("\x0")
|
16
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
-
spec.require_paths = [
|
19
|
-
spec.required_ruby_version =
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
spec.required_ruby_version = ">= 2.3.0", "< 3"
|
20
22
|
|
21
|
-
spec.add_runtime_dependency
|
22
|
-
spec.add_runtime_dependency
|
23
|
-
spec.add_runtime_dependency
|
24
|
-
spec.add_runtime_dependency
|
25
|
-
spec.add_runtime_dependency
|
26
|
-
spec.add_runtime_dependency
|
23
|
+
spec.add_runtime_dependency "hanami-utils", "~> 1.3"
|
24
|
+
spec.add_runtime_dependency "rom", "~> 3.3", ">= 3.3.3"
|
25
|
+
spec.add_runtime_dependency "rom-sql", "~> 1.3", ">= 1.3.5"
|
26
|
+
spec.add_runtime_dependency "rom-repository", "~> 1.4"
|
27
|
+
spec.add_runtime_dependency "dry-types", "~> 0.11.0"
|
28
|
+
spec.add_runtime_dependency "dry-logic", "~> 0.4.2", "< 0.5"
|
29
|
+
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
|
30
|
+
spec.add_runtime_dependency "bigdecimal", "~> 1.4"
|
27
31
|
|
28
|
-
spec.add_development_dependency
|
29
|
-
spec.add_development_dependency
|
30
|
-
spec.add_development_dependency
|
32
|
+
spec.add_development_dependency "bundler", ">= 1.6", "< 3"
|
33
|
+
spec.add_development_dependency "rake", "~> 12"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.7"
|
35
|
+
spec.add_development_dependency "rubocop", "0.81" # rubocop 0.81+ removed support for Ruby 2.3
|
31
36
|
end
|
data/lib/hanami-model.rb
CHANGED
data/lib/hanami/entity.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/model/types"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
# An object that is defined by its identity.
|
@@ -50,7 +52,7 @@ module Hanami
|
|
50
52
|
#
|
51
53
|
# @see Hanami::Repository
|
52
54
|
class Entity
|
53
|
-
require
|
55
|
+
require "hanami/entity/schema"
|
54
56
|
|
55
57
|
# Syntactic shortcut to reference types in custom schema DSL
|
56
58
|
#
|
@@ -94,6 +96,7 @@ module Hanami
|
|
94
96
|
# @api private
|
95
97
|
def schema=(value)
|
96
98
|
return if defined?(@attributes)
|
99
|
+
|
97
100
|
@schema = value
|
98
101
|
end
|
99
102
|
|
@@ -187,7 +190,7 @@ module Hanami
|
|
187
190
|
end
|
188
191
|
|
189
192
|
# @since 0.7.0
|
190
|
-
|
193
|
+
alias_method :to_hash, :to_h
|
191
194
|
|
192
195
|
protected
|
193
196
|
|
data/lib/hanami/entity/schema.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/model/types"
|
4
|
+
require "hanami/utils/hash"
|
3
5
|
|
4
6
|
module Hanami
|
5
7
|
class Entity
|
@@ -165,6 +167,7 @@ module Hanami
|
|
165
167
|
# @api private
|
166
168
|
def initialize(type = nil, &blk)
|
167
169
|
raise LocalJumpError unless block_given?
|
170
|
+
|
168
171
|
@attributes, @schema = Dsl.build(type, &blk)
|
169
172
|
@attributes = Hash[@attributes.map { |k, _| [k, true] }]
|
170
173
|
freeze
|
@@ -181,10 +184,10 @@ module Hanami
|
|
181
184
|
# @api private
|
182
185
|
def call(attributes)
|
183
186
|
schema.call(attributes)
|
184
|
-
rescue Dry::Types::SchemaError =>
|
185
|
-
raise TypeError.new(
|
186
|
-
rescue Dry::Types::MissingKeyError, Dry::Types::UnknownKeysError =>
|
187
|
-
raise ArgumentError.new(
|
187
|
+
rescue Dry::Types::SchemaError => exception
|
188
|
+
raise TypeError.new(exception.message)
|
189
|
+
rescue Dry::Types::MissingKeyError, Dry::Types::UnknownKeysError => exception
|
190
|
+
raise ArgumentError.new(exception.message)
|
188
191
|
end
|
189
192
|
|
190
193
|
# Check if the attribute is known
|
@@ -242,7 +245,7 @@ module Hanami
|
|
242
245
|
|
243
246
|
# @since 0.7.0
|
244
247
|
# @api private
|
245
|
-
|
248
|
+
alias_method :[], :call
|
246
249
|
|
247
250
|
# Check if the attribute is known
|
248
251
|
#
|
data/lib/hanami/model.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rom"
|
4
|
+
require "concurrent"
|
5
|
+
require "hanami/entity"
|
6
|
+
require "hanami/repository"
|
5
7
|
|
6
8
|
# Hanami
|
7
9
|
#
|
@@ -11,12 +13,12 @@ module Hanami
|
|
11
13
|
#
|
12
14
|
# @since 0.1.0
|
13
15
|
module Model
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
16
|
+
require "hanami/model/version"
|
17
|
+
require "hanami/model/error"
|
18
|
+
require "hanami/model/configuration"
|
19
|
+
require "hanami/model/configurator"
|
20
|
+
require "hanami/model/mapping"
|
21
|
+
require "hanami/model/plugins"
|
20
22
|
|
21
23
|
# @api private
|
22
24
|
# @since 0.7.0
|
@@ -33,7 +35,7 @@ module Hanami
|
|
33
35
|
|
34
36
|
# @since 0.7.0
|
35
37
|
# @api private
|
36
|
-
|
38
|
+
alias_method :loaded?, :loaded
|
37
39
|
end
|
38
40
|
|
39
41
|
# Configure the framework
|
@@ -70,7 +72,8 @@ module Hanami
|
|
70
72
|
# @since 0.7.0
|
71
73
|
# @api private
|
72
74
|
def self.container
|
73
|
-
raise
|
75
|
+
raise "Not loaded" unless loaded?
|
76
|
+
|
74
77
|
@container
|
75
78
|
end
|
76
79
|
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rom-sql"
|
4
|
+
require "hanami/model/associations/belongs_to"
|
5
|
+
require "hanami/model/associations/has_many"
|
6
|
+
require "hanami/model/associations/has_one"
|
7
|
+
require "hanami/model/associations/many_to_many"
|
6
8
|
|
7
9
|
module Hanami
|
8
10
|
module Model
|
@@ -24,7 +26,6 @@ module Hanami
|
|
24
26
|
#
|
25
27
|
# @since 0.7.0
|
26
28
|
# @api private
|
27
|
-
# rubocop:disable Metrics/MethodLength
|
28
29
|
def self.lookup(association)
|
29
30
|
case association
|
30
31
|
when ROM::SQL::Association::ManyToMany
|
@@ -39,7 +40,6 @@ module Hanami
|
|
39
40
|
raise "Unsupported association: #{association}"
|
40
41
|
end
|
41
42
|
end
|
42
|
-
# rubocop:enable Metrics/MethodLength
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Hanami
|
2
4
|
module Model
|
3
5
|
module Associations
|
@@ -6,7 +8,6 @@ module Hanami
|
|
6
8
|
# @since 0.7.0
|
7
9
|
# @api private
|
8
10
|
#
|
9
|
-
# rubocop:disable Naming/PredicateName
|
10
11
|
class Dsl
|
11
12
|
# @since 0.7.0
|
12
13
|
# @api private
|
@@ -34,7 +35,6 @@ module Hanami
|
|
34
35
|
@repository.__send__(:relations, Hanami::Utils::String.pluralize(relation).to_sym)
|
35
36
|
end
|
36
37
|
end
|
37
|
-
# rubocop:enable Naming/PredicateName
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/model/types"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
module Model
|
@@ -7,7 +9,7 @@ module Hanami
|
|
7
9
|
#
|
8
10
|
# @since 0.7.0
|
9
11
|
# @api private
|
10
|
-
class HasMany
|
12
|
+
class HasMany
|
11
13
|
# @since 0.7.0
|
12
14
|
# @api private
|
13
15
|
def self.schema_type(entity)
|
@@ -51,8 +53,8 @@ module Hanami
|
|
51
53
|
def create(data)
|
52
54
|
entity.new(command(:create, aggregate(target), mapper: nil, use: [:timestamps])
|
53
55
|
.call(serialize(data)))
|
54
|
-
rescue =>
|
55
|
-
raise Hanami::Model::Error.for(
|
56
|
+
rescue => exception
|
57
|
+
raise Hanami::Model::Error.for(exception)
|
56
58
|
end
|
57
59
|
|
58
60
|
# @since 0.7.0
|
@@ -60,8 +62,8 @@ module Hanami
|
|
60
62
|
def add(data)
|
61
63
|
command(:create, relation(target), use: [:timestamps])
|
62
64
|
.call(associate(serialize(data)))
|
63
|
-
rescue =>
|
64
|
-
raise Hanami::Model::Error.for(
|
65
|
+
rescue => exception
|
66
|
+
raise Hanami::Model::Error.for(exception)
|
65
67
|
end
|
66
68
|
|
67
69
|
# @since 0.7.0
|
@@ -113,7 +115,7 @@ module Hanami
|
|
113
115
|
# @since 0.7.0
|
114
116
|
# @api private
|
115
117
|
def command(target, relation, options = {})
|
116
|
-
repository.command(target
|
118
|
+
repository.command(target, relation, options)
|
117
119
|
end
|
118
120
|
|
119
121
|
# @since 0.7.0
|
@@ -151,7 +153,7 @@ module Hanami
|
|
151
153
|
# @since 0.7.0
|
152
154
|
# @api private
|
153
155
|
def unassociate
|
154
|
-
{
|
156
|
+
{foreign_key => nil}
|
155
157
|
end
|
156
158
|
|
157
159
|
# @since 0.7.0
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "hanami/utils/hash"
|
2
4
|
|
3
5
|
module Hanami
|
@@ -53,14 +55,14 @@ module Hanami
|
|
53
55
|
entity.new(
|
54
56
|
command(:create, aggregate(target), mapper: nil).call(serialize(data))
|
55
57
|
)
|
56
|
-
rescue =>
|
57
|
-
raise Hanami::Model::Error.for(
|
58
|
+
rescue => exception
|
59
|
+
raise Hanami::Model::Error.for(exception)
|
58
60
|
end
|
59
61
|
|
60
62
|
def add(data)
|
61
63
|
command(:create, relation(target), mapper: nil).call(associate(serialize(data)))
|
62
|
-
rescue =>
|
63
|
-
raise Hanami::Model::Error.for(
|
64
|
+
rescue => exception
|
65
|
+
raise Hanami::Model::Error.for(exception)
|
64
66
|
end
|
65
67
|
|
66
68
|
def update(data)
|
@@ -68,14 +70,14 @@ module Hanami
|
|
68
70
|
.by_pk(
|
69
71
|
one.public_send(relation(target).primary_key)
|
70
72
|
).call(serialize(data))
|
71
|
-
rescue =>
|
72
|
-
raise Hanami::Model::Error.for(
|
73
|
+
rescue => exception
|
74
|
+
raise Hanami::Model::Error.for(exception)
|
73
75
|
end
|
74
76
|
|
75
77
|
def delete
|
76
78
|
scope.delete
|
77
79
|
end
|
78
|
-
|
80
|
+
alias_method :remove, :delete
|
79
81
|
|
80
82
|
def replace(data)
|
81
83
|
repository.transaction do
|