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
@@ -1,6 +1,8 @@
1
- require 'pathname'
2
- require 'hanami/utils'
3
- require 'English'
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "hanami/utils"
5
+ require "English"
4
6
 
5
7
  module Hanami
6
8
  module Model
@@ -71,7 +73,7 @@ module Hanami
71
73
  # @api private
72
74
  def path
73
75
  root.join(
74
- @connection.uri.sub(/\A(jdbc:sqlite:\/\/|sqlite:\/\/)/, '')
76
+ @connection.uri.sub(/\A(jdbc:sqlite:\/\/|sqlite:\/\/)/, "")
75
77
  )
76
78
  end
77
79
 
@@ -104,13 +106,11 @@ module Hanami
104
106
  # @since 0.4.0
105
107
  # @api private
106
108
  #
107
- # rubocop:disable Metrics/AbcSize
108
- # rubocop:disable Metrics/MethodLength
109
109
  def dump_migrations_data
110
110
  execute "sqlite3 #{escape(path)} .dump" do |stdout|
111
111
  begin
112
112
  contents = stdout.read.split($INPUT_RECORD_SEPARATOR)
113
- contents = contents.grep(/^INSERT INTO "#{migrations_table}"/)
113
+ contents = contents.grep(/^INSERT INTO "?#{migrations_table}"?/)
114
114
 
115
115
  ::File.open(schema, ::File::CREAT | ::File::BINARY | ::File::WRONLY | ::File::APPEND) do |file|
116
116
  file.write(contents.join($INPUT_RECORD_SEPARATOR))
@@ -120,8 +120,6 @@ module Hanami
120
120
  end
121
121
  end
122
122
  end
123
- # rubocop:enable Metrics/MethodLength
124
- # rubocop:enable Metrics/AbcSize
125
123
  end
126
124
  end
127
125
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hanami
2
4
  module Model
3
5
  # Plugins to extend read/write operations from/to the database
@@ -17,9 +19,9 @@ module Hanami
17
19
  end
18
20
  end
19
21
 
20
- require 'hanami/model/plugins/mapping'
21
- require 'hanami/model/plugins/schema'
22
- require 'hanami/model/plugins/timestamps'
22
+ require "hanami/model/plugins/mapping"
23
+ require "hanami/model/plugins/schema"
24
+ require "hanami/model/plugins/timestamps"
23
25
  end
24
26
  end
25
27
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hanami
2
4
  module Model
3
5
  module Plugins
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hanami
2
4
  module Model
3
5
  module Plugins
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hanami
2
4
  module Model
3
5
  module Plugins
@@ -1,5 +1,7 @@
1
- require_relative 'entity_name'
2
- require 'hanami/utils/string'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "entity_name"
4
+ require "hanami/utils/string"
3
5
 
4
6
  module Hanami
5
7
  module Model
@@ -1,12 +1,14 @@
1
- require 'rom-sql'
2
- require 'hanami/utils'
1
+ # frozen_string_literal: true
2
+
3
+ require "rom-sql"
4
+ require "hanami/utils"
3
5
 
4
6
  module Hanami
5
7
  # Hanami::Model migrations
6
8
  module Model
7
- require 'hanami/model/error'
8
- require 'hanami/model/association'
9
- require 'hanami/model/migration'
9
+ require "hanami/model/error"
10
+ require "hanami/model/association"
11
+ require "hanami/model/migration"
10
12
 
11
13
  # Define a migration
12
14
  #
@@ -53,8 +55,8 @@ module Hanami
53
55
  #
54
56
  # @since 0.7.0
55
57
  module Sql
56
- require 'hanami/model/sql/types'
57
- require 'hanami/model/sql/entity/schema'
58
+ require "hanami/model/sql/types"
59
+ require "hanami/model/sql/entity/schema"
58
60
 
59
61
  # Returns a SQL fragment that references a database function by the given name
60
62
  # This is useful for database migrations
@@ -1,4 +1,6 @@
1
- require 'uri'
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
2
4
 
3
5
  module Hanami
4
6
  module Model
@@ -24,16 +26,16 @@ module Hanami
24
26
 
25
27
  # @since 0.7.0
26
28
  # @api private
27
- def console # rubocop:disable Metrics/MethodLength
29
+ def console
28
30
  case @uri.scheme
29
- when 'sqlite'
30
- require 'hanami/model/sql/consoles/sqlite'
31
+ when "sqlite"
32
+ require "hanami/model/sql/consoles/sqlite"
31
33
  Sql::Consoles::Sqlite.new(@uri)
32
- when 'postgres', 'postgresql'
33
- require 'hanami/model/sql/consoles/postgresql'
34
+ when "postgres", "postgresql"
35
+ require "hanami/model/sql/consoles/postgresql"
34
36
  Sql::Consoles::Postgresql.new(@uri)
35
- when 'mysql', 'mysql2'
36
- require 'hanami/model/sql/consoles/mysql'
37
+ when "mysql", "mysql2"
38
+ require "hanami/model/sql/consoles/mysql"
37
39
  Sql::Consoles::Mysql.new(@uri)
38
40
  end
39
41
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hanami
2
4
  module Model
3
5
  module Sql
@@ -18,7 +20,7 @@ module Hanami
18
20
  # @since 0.7.0
19
21
  # @api private
20
22
  def database_name
21
- @uri.path.sub(/^\//, '')
23
+ @uri.path.sub(/^\//, "")
22
24
  end
23
25
 
24
26
  # @since 0.7.0
@@ -1,4 +1,6 @@
1
- require_relative 'abstract'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "abstract"
2
4
 
3
5
  module Hanami
4
6
  module Model
@@ -11,7 +13,7 @@ module Hanami
11
13
  class Mysql < Abstract
12
14
  # @since 0.7.0
13
15
  # @api private
14
- COMMAND = 'mysql'.freeze
16
+ COMMAND = "mysql"
15
17
 
16
18
  # @since 0.7.0
17
19
  # @api private
@@ -1,5 +1,7 @@
1
- require_relative 'abstract'
2
- require 'cgi'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "abstract"
4
+ require "cgi"
3
5
 
4
6
  module Hanami
5
7
  module Model
@@ -12,11 +14,11 @@ module Hanami
12
14
  class Postgresql < Abstract
13
15
  # @since 0.7.0
14
16
  # @api private
15
- COMMAND = 'psql'.freeze
17
+ COMMAND = "psql"
16
18
 
17
19
  # @since 0.7.0
18
20
  # @api private
19
- PASSWORD = 'PGPASSWORD'.freeze
21
+ PASSWORD = "PGPASSWORD"
20
22
 
21
23
  # @since 0.7.0
22
24
  # @api private
@@ -48,22 +50,22 @@ module Hanami
48
50
  # @since 0.7.0
49
51
  # @api private
50
52
  def port
51
- port = query['port'] || @uri.port
53
+ port = query["port"] || @uri.port
52
54
  " -p #{port}" if port
53
55
  end
54
56
 
55
57
  # @since 0.7.0
56
58
  # @api private
57
59
  def username
58
- username = query['user'] || @uri.user
60
+ username = query["user"] || @uri.user
59
61
  " -U #{username}" if username
60
62
  end
61
63
 
62
64
  # @since 0.7.0
63
65
  # @api private
64
66
  def configure_password
65
- password = query['password'] || @uri.password
66
- ENV[PASSWORD] = CGI.unescape(query['password'] || @uri.password) if password
67
+ password = query["password"] || @uri.password
68
+ ENV[PASSWORD] = CGI.unescape(query["password"] || @uri.password) if password
67
69
  end
68
70
 
69
71
  # @since 1.1.0
@@ -1,5 +1,7 @@
1
- require_relative 'abstract'
2
- require 'shellwords'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "abstract"
4
+ require "shellwords"
3
5
 
4
6
  module Hanami
5
7
  module Model
@@ -12,12 +14,12 @@ module Hanami
12
14
  class Sqlite < Abstract
13
15
  # @since 0.7.0
14
16
  # @api private
15
- COMMAND = 'sqlite3'.freeze
17
+ COMMAND = "sqlite3"
16
18
 
17
19
  # @since 0.7.0
18
20
  # @api private
19
21
  def connection_string
20
- concat(command, ' ', host, database)
22
+ concat(command, " ", host, database)
21
23
  end
22
24
 
23
25
  private
@@ -1,6 +1,8 @@
1
- require 'hanami/entity/schema'
2
- require 'hanami/model/types'
3
- require 'hanami/model/association'
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/entity/schema"
4
+ require "hanami/model/types"
5
+ require "hanami/model/association"
4
6
 
5
7
  module Hanami
6
8
  module Model
@@ -52,7 +54,7 @@ module Hanami
52
54
 
53
55
  # @since 1.0.1
54
56
  # @api private
55
- alias [] call
57
+ alias_method :[], :call
56
58
 
57
59
  # Check if the attribute is known
58
60
  #
@@ -1,5 +1,7 @@
1
- require 'hanami/model/types'
2
- require 'rom/types'
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/model/types"
4
+ require "rom/types"
3
5
 
4
6
  module Hanami
5
7
  module Model
@@ -14,7 +16,7 @@ module Hanami
14
16
  #
15
17
  # @since 0.7.0
16
18
  module Schema
17
- require 'hanami/model/sql/types/schema/coercions'
19
+ require "hanami/model/sql/types/schema/coercions"
18
20
 
19
21
  String = Types::Optional::Coercible::String
20
22
 
@@ -93,8 +95,8 @@ module Hanami
93
95
  # @since 1.0.2
94
96
  # @api private
95
97
  def self.pg_json?(pristine)
96
- pristine == pg_json_pristines['JSONB'.freeze] || # rubocop:disable Style/MultipleComparison
97
- pristine == pg_json_pristines['JSON'.freeze]
98
+ pristine == pg_json_pristines["JSONB"] || # rubocop:disable Style/MultipleComparison
99
+ pristine == pg_json_pristines["JSON"]
98
100
  end
99
101
 
100
102
  private_class_method :pg_json?
@@ -1,5 +1,7 @@
1
- require 'hanami/utils/string'
2
- require 'hanami/utils/hash'
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/utils/string"
4
+ require "hanami/utils/hash"
3
5
 
4
6
  module Hanami
5
7
  module Model
@@ -12,7 +14,6 @@ module Hanami
12
14
  # @api private
13
15
  #
14
16
  # rubocop:disable Metrics/ModuleLength
15
- # rubocop:disable Metrics/MethodLength
16
17
  module Coercions
17
18
  # Coerces given argument into Integer
18
19
  #
@@ -215,7 +216,7 @@ module Hanami
215
216
  end
216
217
  end
217
218
  end
218
- # rubocop:enable Metrics/MethodLength
219
+
219
220
  # rubocop:enable Metrics/ModuleLength
220
221
  end
221
222
  end
@@ -1,4 +1,6 @@
1
- require 'rom/types'
1
+ # frozen_string_literal: true
2
+
3
+ require "rom/types"
2
4
 
3
5
  module Hanami
4
6
  module Model
@@ -17,7 +19,6 @@ module Hanami
17
19
  # Class level interface
18
20
  #
19
21
  # @since 0.7.0
20
- # rubocop:disable Naming/MethodName
21
22
  module ClassMethods
22
23
  # Define an entity of the given type
23
24
  #
@@ -77,7 +78,6 @@ module Hanami
77
78
  Types::Array.member(type)
78
79
  end
79
80
  end
80
- # rubocop:enable Naming/MethodName
81
81
 
82
82
  # Types for schema definitions
83
83
  #
@@ -101,7 +101,7 @@ module Hanami
101
101
  def call(value)
102
102
  return if value.nil?
103
103
 
104
- if valid?(value) # rubocop:disable Style/GuardClause
104
+ if valid?(value)
105
105
  coerce(value)
106
106
  else
107
107
  raise TypeError.new("#{value.inspect} must be coercible into #{object}")
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hanami
2
4
  module Model
3
5
  # Defines the version
4
6
  #
5
7
  # @since 0.1.0
6
- VERSION = '1.3.2'.freeze
8
+ VERSION = "1.3.3"
7
9
  end
8
10
  end
@@ -1,12 +1,14 @@
1
- require 'rom-repository'
2
- require 'hanami/model/entity_name'
3
- require 'hanami/model/relation_name'
4
- require 'hanami/model/mapped_relation'
5
- require 'hanami/model/associations/dsl'
6
- require 'hanami/model/association'
7
- require 'hanami/utils/class'
8
- require 'hanami/utils/class_attribute'
9
- require 'hanami/utils/io'
1
+ # frozen_string_literal: true
2
+
3
+ require "rom-repository"
4
+ require "hanami/model/entity_name"
5
+ require "hanami/model/relation_name"
6
+ require "hanami/model/mapped_relation"
7
+ require "hanami/model/associations/dsl"
8
+ require "hanami/model/association"
9
+ require "hanami/utils/class"
10
+ require "hanami/utils/class_attribute"
11
+ require "hanami/utils/io"
10
12
 
11
13
  module Hanami
12
14
  # Mediates between the entities and the persistence layer, by offering an API
@@ -107,7 +109,7 @@ module Hanami
107
109
  # @see Hanami::Entity
108
110
  # @see http://martinfowler.com/eaaCatalog/repository.html
109
111
  # @see http://en.wikipedia.org/wiki/Dependency_inversion_principle
110
- class Repository < ROM::Repository::Root # rubocop:disable Metrics/ClassLength
112
+ class Repository < ROM::Repository::Root
111
113
  # Plugins for database commands
112
114
  #
113
115
  # @since 0.7.0
@@ -155,7 +157,6 @@ module Hanami
155
157
  def command(*args, **opts, &block)
156
158
  opts[:use] = COMMAND_PLUGINS | Array(opts[:use])
157
159
  opts[:mapper] = opts.fetch(:mapper, Model::MappedRelation.mapper_name)
158
-
159
160
  super(*args, **opts, &block)
160
161
  end
161
162
 
@@ -167,8 +168,6 @@ module Hanami
167
168
  # @since 0.7.0
168
169
  # @api private
169
170
  #
170
- # rubocop:disable Metrics/MethodLength
171
- # rubocop:disable Metrics/AbcSize
172
171
  def self.define_relation
173
172
  a = @associations
174
173
  s = @schema
@@ -191,8 +190,6 @@ module Hanami
191
190
  end
192
191
  }, __FILE__, __LINE__ - 4
193
192
  end
194
- # rubocop:enable Metrics/AbcSize
195
- # rubocop:enable Metrics/MethodLength
196
193
 
197
194
  # Defines the mapping between a database table and an entity.
198
195
  #
@@ -201,8 +198,6 @@ module Hanami
201
198
  # @since 0.7.0
202
199
  # @api private
203
200
  #
204
- # rubocop:disable Metrics/MethodLength
205
- # rubocop:disable Metrics/AbcSize
206
201
  def self.define_mapping
207
202
  self.entity = Utils::Class.load!(entity_name)
208
203
  e = entity
@@ -219,8 +214,6 @@ module Hanami
219
214
  configuration.define_mappings(root, &blk)
220
215
  configuration.register_entity(relation, entity_name.underscore, e)
221
216
  end
222
- # rubocop:enable Metrics/AbcSize
223
- # rubocop:enable Metrics/MethodLength
224
217
 
225
218
  # It defines associations, by adding relations to the repository
226
219
  #
@@ -302,8 +295,6 @@ module Hanami
302
295
  # @since 0.7.0
303
296
  # @api private
304
297
  #
305
- # rubocop:disable Metrics/MethodLength
306
- # rubocop:disable Metrics/AbcSize
307
298
  def self.inherited(klass)
308
299
  klass.class_eval do
309
300
  include Utils::ClassAttribute
@@ -332,8 +323,6 @@ module Hanami
332
323
 
333
324
  Hanami::Model.repositories << klass
334
325
  end
335
- # rubocop:enable Metrics/AbcSize
336
- # rubocop:enable Metrics/MethodLength
337
326
 
338
327
  # Extend commands from ROM::Repository with error management
339
328
  #
@@ -358,8 +347,8 @@ module Hanami
358
347
  # entity.id # => nil - It doesn't mutate original entity
359
348
  def create(*args)
360
349
  super
361
- rescue => e
362
- raise Hanami::Model::Error.for(e)
350
+ rescue => exception
351
+ raise Hanami::Model::Error.for(exception)
363
352
  end
364
353
 
365
354
  # Update a record
@@ -387,8 +376,8 @@ module Hanami
387
376
  # entity.id # => nil - It doesn't mutate original entity
388
377
  def update(*args)
389
378
  super
390
- rescue => e
391
- raise Hanami::Model::Error.for(e)
379
+ rescue => exception
380
+ raise Hanami::Model::Error.for(exception)
392
381
  end
393
382
 
394
383
  # Delete a record
@@ -406,8 +395,8 @@ module Hanami
406
395
  # user = repository.delete(user.id)
407
396
  def delete(*args)
408
397
  super
409
- rescue => e
410
- raise Hanami::Model::Error.for(e)
398
+ rescue => exception
399
+ raise Hanami::Model::Error.for(exception)
411
400
  end
412
401
  end
413
402
 
@@ -436,8 +425,8 @@ module Hanami
436
425
  # user = repository.find(user.id)
437
426
  def find(id)
438
427
  root.by_pk(id).as(:entity).one
439
- rescue => e
440
- raise Hanami::Model::Error.for(e)
428
+ rescue => exception
429
+ raise Hanami::Model::Error.for(exception)
441
430
  end
442
431
 
443
432
  # Return all the records for the relation