hanami-model 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +6 -3
  5. data/hanami-model.gemspec +25 -21
  6. data/lib/hanami-model.rb +3 -1
  7. data/lib/hanami/entity.rb +5 -3
  8. data/lib/hanami/entity/schema.rb +9 -7
  9. data/lib/hanami/model.rb +14 -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 +15 -13
  17. data/lib/hanami/model/configurator.rb +5 -3
  18. data/lib/hanami/model/entity_name.rb +3 -1
  19. data/lib/hanami/model/error.rb +9 -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 +13 -11
  25. data/lib/hanami/model/migrator/connection.rb +10 -8
  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 +30 -28
  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 +2 -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 +7 -5
  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 +34 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 919b36ea881919a3c6bba3cee7fa026bde46df57d62bc0d9f63bc08a6f2a4e32
4
- data.tar.gz: ac679f8031a4d47e0c5a633f70ade7c8390aff56da8b1dff3231612273e097ef
3
+ metadata.gz: e39e89062478fa891ec0904add24b1ec30bb93435b178f229cc4328995de9acb
4
+ data.tar.gz: 0c3190667360fa53e56797c14bac30fdb9fe3ca4266ce4f33dc2a1b941142efb
5
5
  SHA512:
6
- metadata.gz: 243f59fbe8c436e2e36bfe4979d34d9eaf2a5ea3c53dacb026d2f04b62a6764a84d88a744c24e734ee8a57599890ed7c260dd798da0eb510adda7fbdeaec512a
7
- data.tar.gz: 683516b7122cc953796c25e1be8144242b60fcb125c810061789e6ced6aff4aa5a3032a5fe3ef919f886ab5dc6a5ecce0111c5f0666bffa272ad30e149e054ec
6
+ metadata.gz: 3420b442c9015651d7e0f6bd55861252919604c02784601d3177f28c959c986cd35308fa73db94e452624cec4a85a443fa9472354eafe8c8b5fb78dea46162c9
7
+ data.tar.gz: 99c9779f14b14484f33bd4789105bb96c83ce6165d1048eb9e0db0a7cc573ead79308ca00f7e135e743f22c506231c4a6f8b348da4c4596da4f23f00784a3121
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
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
+
4
9
  ## v1.3.2 - 2019-01-31
5
10
  ### Fixed
6
11
  - [Luca Guidi] Depend on `dry-logic` `~> 0.4.2`, `< 0.5`
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
@@ -15,8 +15,7 @@ Like all the other Hanami components, it can be used as a standalone framework o
15
15
  ## Status
16
16
 
17
17
  [![Gem Version](https://badge.fury.io/rb/hanami-model.svg)](https://badge.fury.io/rb/hanami-model)
18
- [![TravisCI](https://travis-ci.org/hanami/model.svg?branch=master)](https://travis-ci.org/hanami/model)
19
- [![CircleCI](https://circleci.com/gh/hanami/model/tree/master.svg?style=svg)](https://circleci.com/gh/hanami/model/tree/master)
18
+ [![CI](https://github.com/hanami/model/workflows/ci/badge.svg?branch=master)](https://github.com/hanami/model/actions?query=workflow%3Aci+branch%3Amaster)
20
19
  [![Test Coverage](https://codecov.io/gh/hanami/model/branch/master/graph/badge.svg)](https://codecov.io/gh/hanami/model)
21
20
  [![Depfu](https://badges.depfu.com/badges/3a5d3f9e72895493bb6f39402ac4f129/overview.svg)](https://depfu.com/github/hanami/model?project=Bundler)
22
21
  [![Inline Docs](http://inch-ci.org/github/hanami/model.svg)](http://inch-ci.org/github/hanami/model)
@@ -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,32 +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.3'
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 'dry-logic', '~> 0.4.2', '< 0.5'
27
- 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"
28
31
 
29
- spec.add_development_dependency 'bundler', '>= 1.6', '< 3'
30
- spec.add_development_dependency 'rake', '~> 12'
31
- 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
32
36
  end
data/lib/hanami-model.rb CHANGED
@@ -1 +1,3 @@
1
- require 'hanami/model'
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
  #
@@ -188,7 +190,7 @@ module Hanami
188
190
  end
189
191
 
190
192
  # @since 0.7.0
191
- alias to_hash to_h
193
+ alias_method :to_hash, :to_h
192
194
 
193
195
  protected
194
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
@@ -182,10 +184,10 @@ module Hanami
182
184
  # @api private
183
185
  def call(attributes)
184
186
  schema.call(attributes)
185
- rescue Dry::Types::SchemaError => e
186
- raise TypeError.new(e.message)
187
- rescue Dry::Types::MissingKeyError, Dry::Types::UnknownKeysError => e
188
- 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)
189
191
  end
190
192
 
191
193
  # Check if the attribute is known
@@ -243,7 +245,7 @@ module Hanami
243
245
 
244
246
  # @since 0.7.0
245
247
  # @api private
246
- alias [] call
248
+ alias_method :[], :call
247
249
 
248
250
  # Check if the attribute is known
249
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,7 @@ 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?
74
76
 
75
77
  @container
76
78
  end
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "hanami/utils/hash"
2
4
 
3
5
  module Hanami
@@ -7,7 +9,7 @@ module Hanami
7
9
  #
8
10
  # @since 0.7.0
9
11
  # @api private
10
- class ManyToMany # rubocop:disable Metrics/ClassLength
12
+ class ManyToMany
11
13
  # @since 0.7.0
12
14
  # @api private
13
15
  def self.schema_type(entity)
@@ -76,8 +78,8 @@ module Hanami
76
78
  def add(*data)
77
79
  command(:create, relation(through), use: [:timestamps])
78
80
  .call(associate(serialize(data)))
79
- rescue => e
80
- raise Hanami::Model::Error.for(e)
81
+ rescue => exception
82
+ raise Hanami::Model::Error.for(exception)
81
83
  end
82
84
 
83
85
  # @since 1.1.0
@@ -88,18 +90,16 @@ module Hanami
88
90
 
89
91
  # @since 1.1.0
90
92
  # @api private
91
- # rubocop:disable Metrics/AbcSize
92
93
  def remove(target_id)
93
94
  association_record = relation(through)
94
- .where(target_foreign_key => target_id, source_foreign_key => subject.fetch(source_primary_key))
95
- .one
95
+ .where(target_foreign_key => target_id, source_foreign_key => subject.fetch(source_primary_key))
96
+ .one
96
97
 
97
98
  return if association_record.nil?
98
99
 
99
100
  ar_id = association_record.public_send relation(through).primary_key
100
101
  command(:delete, relation(through)).by_pk(ar_id).call
101
102
  end
102
- # rubocop:enable Metrics/AbcSize
103
103
 
104
104
  private
105
105
 
@@ -172,17 +172,15 @@ module Hanami
172
172
  # @since 1.1.0
173
173
  #
174
174
  # @api private
175
- # rubocop:disable Metrics/AbcSize
176
175
  def _build_scope
177
176
  result = relation(association.target.to_sym).qualified
178
177
  unless subject.nil?
179
178
  result = result
180
- .join(through, target_foreign_key => target_primary_key)
181
- .where(source_foreign_key => subject.fetch(source_primary_key))
179
+ .join(through, target_foreign_key => target_primary_key)
180
+ .where(source_foreign_key => subject.fetch(source_primary_key))
182
181
  end
183
182
  result.as(Model::MappedRelation.mapper_name)
184
183
  end
185
- # rubocop:enable Metrics/AbcSize
186
184
 
187
185
  # @since 1.1.0
188
186
  # @api private