hanami-model 1.2.0 → 1.3.3

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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +10 -7
  5. data/hanami-model.gemspec +25 -20
  6. data/lib/hanami-model.rb +3 -1
  7. data/lib/hanami/entity.rb +6 -3
  8. data/lib/hanami/entity/schema.rb +10 -7
  9. data/lib/hanami/model.rb +15 -12
  10. data/lib/hanami/model/association.rb +7 -7
  11. data/lib/hanami/model/associations/belongs_to.rb +3 -1
  12. data/lib/hanami/model/associations/dsl.rb +2 -2
  13. data/lib/hanami/model/associations/has_many.rb +10 -8
  14. data/lib/hanami/model/associations/has_one.rb +9 -7
  15. data/lib/hanami/model/associations/many_to_many.rb +9 -11
  16. data/lib/hanami/model/configuration.rb +29 -10
  17. data/lib/hanami/model/configurator.rb +5 -3
  18. data/lib/hanami/model/entity_name.rb +4 -2
  19. data/lib/hanami/model/error.rb +18 -7
  20. data/lib/hanami/model/mapped_relation.rb +4 -2
  21. data/lib/hanami/model/mapping.rb +3 -1
  22. data/lib/hanami/model/migration.rb +2 -0
  23. data/lib/hanami/model/migrator.rb +7 -5
  24. data/lib/hanami/model/migrator/adapter.rb +14 -12
  25. data/lib/hanami/model/migrator/connection.rb +16 -9
  26. data/lib/hanami/model/migrator/logger.rb +3 -1
  27. data/lib/hanami/model/migrator/mysql_adapter.rb +23 -13
  28. data/lib/hanami/model/migrator/postgres_adapter.rb +31 -31
  29. data/lib/hanami/model/migrator/sqlite_adapter.rb +7 -9
  30. data/lib/hanami/model/plugins.rb +5 -3
  31. data/lib/hanami/model/plugins/mapping.rb +2 -0
  32. data/lib/hanami/model/plugins/schema.rb +2 -0
  33. data/lib/hanami/model/plugins/timestamps.rb +3 -0
  34. data/lib/hanami/model/relation_name.rb +4 -2
  35. data/lib/hanami/model/sql.rb +9 -7
  36. data/lib/hanami/model/sql/console.rb +10 -8
  37. data/lib/hanami/model/sql/consoles/abstract.rb +3 -1
  38. data/lib/hanami/model/sql/consoles/mysql.rb +4 -2
  39. data/lib/hanami/model/sql/consoles/postgresql.rb +10 -8
  40. data/lib/hanami/model/sql/consoles/sqlite.rb +6 -4
  41. data/lib/hanami/model/sql/entity/schema.rb +6 -4
  42. data/lib/hanami/model/sql/types.rb +27 -27
  43. data/lib/hanami/model/sql/types/schema/coercions.rb +5 -4
  44. data/lib/hanami/model/types.rb +4 -4
  45. data/lib/hanami/model/version.rb +3 -1
  46. data/lib/hanami/repository.rb +20 -31
  47. metadata +64 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb2655d298d2a0a66dd13c2d0f8818b8732feb84a5f5260e3b9ccddb1b6e5969
4
- data.tar.gz: d6321500dc644537f9e86051c777f7d6ad37ff4f587fc3f890acb83b0249df04
3
+ metadata.gz: e39e89062478fa891ec0904add24b1ec30bb93435b178f229cc4328995de9acb
4
+ data.tar.gz: 0c3190667360fa53e56797c14bac30fdb9fe3ca4266ce4f33dc2a1b941142efb
5
5
  SHA512:
6
- metadata.gz: 98a2d0bef71b03e6d4bc3998932e1dfbe8be9c45585fc9f96fb4333677ffd824b4d1eb558e0fa36251e12708da560e01f6bd7a0ee7cc737de39bafe65fca857b
7
- data.tar.gz: e8a3a99737f647c0ac10dd8d16a2da99dd3f605b6a32660422b675302336530b67a30805e14320aa91444d7102bb0cc2265f0cbdfda2ba16db3a24b1a058d764
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
@@ -1,4 +1,4 @@
1
- Copyright © 2014-2017 Luca Guidi
1
+ Copyright © 2014-2021 Luca Guidi
2
2
 
3
3
  MIT License
4
4
 
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)](http://badge.fury.io/rb/hanami-model)
18
- [![Build Status](https://secure.travis-ci.org/hanami/model.svg?branch=master)](http://travis-ci.org/hanami/model?branch=master)
19
- [![Coverage](https://codecov.io/gh/hanami/model/branch/master/graph/badge.svg)](https://codecov.io/gh/hanami/model)
20
- [![Code Climate](https://codeclimate.com/github/hanami/model/badges/gpa.svg)](https://codeclimate.com/github/hanami/model)
21
- [![Dependencies](https://gemnasium.com/hanami/model.svg)](https://gemnasium.com/hanami/model)
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-2017 Luca Guidi – Released under MIT License
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
- lib = File.expand_path('../lib', __FILE__)
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 'hanami/model/version'
5
+ require "hanami/model/version"
4
6
 
5
7
  Gem::Specification.new do |spec|
6
- spec.name = 'hanami-model'
8
+ spec.name = "hanami-model"
7
9
  spec.version = Hanami::Model::VERSION
8
- spec.authors = ['Luca Guidi']
9
- spec.email = ['me@lucaguidi.com']
10
- spec.summary = 'A persistence layer for Hanami'
11
- spec.description = 'A persistence framework with entities and repositories'
12
- spec.homepage = 'http://hanamirb.org'
13
- spec.license = 'MIT'
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 = ['lib']
19
- spec.required_ruby_version = '>= 2.3.0'
20
+ spec.require_paths = ["lib"]
21
+ spec.required_ruby_version = ">= 2.3.0", "< 3"
20
22
 
21
- spec.add_runtime_dependency 'hanami-utils', '~> 1.2'
22
- spec.add_runtime_dependency 'rom', '~> 3.3', '>= 3.3.3'
23
- spec.add_runtime_dependency 'rom-sql', '~> 1.3', '>= 1.3.5'
24
- spec.add_runtime_dependency 'rom-repository', '~> 1.4'
25
- spec.add_runtime_dependency 'dry-types', '~> 0.11.0'
26
- spec.add_runtime_dependency 'concurrent-ruby', '~> 1.0'
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 'bundler'
29
- spec.add_development_dependency 'rake', '~> 12'
30
- spec.add_development_dependency 'rspec', '~> 3.7'
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
@@ -1 +1,3 @@
1
- require 'hanami/model' # rubocop:disable Naming/FileName
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/model"
data/lib/hanami/entity.rb CHANGED
@@ -1,4 +1,6 @@
1
- require 'hanami/model/types'
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 'hanami/entity/schema'
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
- alias to_hash to_h
193
+ alias_method :to_hash, :to_h
191
194
 
192
195
  protected
193
196
 
@@ -1,5 +1,7 @@
1
- require 'hanami/model/types'
2
- require 'hanami/utils/hash'
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 => e
185
- raise TypeError.new(e.message)
186
- rescue Dry::Types::MissingKeyError, Dry::Types::UnknownKeysError => e
187
- raise ArgumentError.new(e.message)
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
- alias [] call
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
- require 'rom'
2
- require 'concurrent'
3
- require 'hanami/entity'
4
- require 'hanami/repository'
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 'hanami/model/version'
15
- require 'hanami/model/error'
16
- require 'hanami/model/configuration'
17
- require 'hanami/model/configurator'
18
- require 'hanami/model/mapping'
19
- require 'hanami/model/plugins'
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
- alias loaded? loaded
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 'Not loaded' unless loaded?
75
+ raise "Not loaded" unless loaded?
76
+
74
77
  @container
75
78
  end
76
79
 
@@ -1,8 +1,10 @@
1
- require 'rom-sql'
2
- require 'hanami/model/associations/belongs_to'
3
- require 'hanami/model/associations/has_many'
4
- require 'hanami/model/associations/has_one'
5
- require 'hanami/model/associations/many_to_many'
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,4 +1,6 @@
1
- require 'hanami/model/types'
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/model/types"
2
4
 
3
5
  module Hanami
4
6
  module Model
@@ -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
- require 'hanami/model/types'
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 # rubocop:disable Metrics/ClassLength
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 => e
55
- raise Hanami::Model::Error.for(e)
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 => e
64
- raise Hanami::Model::Error.for(e)
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 => relation, **options)
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
- { foreign_key => nil }
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 => e
57
- raise Hanami::Model::Error.for(e)
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 => e
63
- raise Hanami::Model::Error.for(e)
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 => e
72
- raise Hanami::Model::Error.for(e)
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
- alias remove delete
80
+ alias_method :remove, :delete
79
81
 
80
82
  def replace(data)
81
83
  repository.transaction do