limitable 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6c12a16cc4494f377161bf1391268d66e74cce94d4483c0f62a9d6cca7aa7ec
4
+ data.tar.gz: 5d691e45c1b9a244289781285dcaf05445e2b965ff3116c1b5492da1c2284741
5
+ SHA512:
6
+ metadata.gz: 2b9c14a552cd790b9dd0ddce4d8e8dda51b9407eef62abf5a72c86ea0f7de08343699a6e88809632f2630048cd6f5a696fb7af414eb1e4493f6f943b246a7e49
7
+ data.tar.gz: 06ab70e18d5063028f850fca942a96638456f14611f6ff1c51d7c26c3e1c5f2a70c3794e214036aa574bb97dfa5ed416e145532c6100bae3512d3a845249f229
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # 1.0.0 (2023-05-30)
2
+
3
+
4
+ ### Features
5
+
6
+ * limitable ([#1](https://github.com/benmelz/limitable/issues/1)) ([0040aeb](https://github.com/benmelz/limitable/commit/0040aeba6d2b6f40feefe9040b10e412ccc33b59))
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Benjamin Melz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # Limitable
2
+
3
+ Limitable scans your ActiveRecord database schema for column size limits and defines corresponding model validations
4
+ so that you don't have to.
5
+
6
+ It aims to make your database schema the "one source of truth" about the maximum data sizes your columns can handle.
7
+ More practically, it removes the redundant need for explicit guards such as:
8
+
9
+ ```ruby
10
+ validates :my_string_column, length: { less_than: 256 }
11
+
12
+ validates :my_integer_column, numericality: { less_than: 2_147_483_647 }
13
+
14
+ begin
15
+ # ...
16
+ rescue ActiveRecord::ValueTooLong
17
+ # ...
18
+ end
19
+ ```
20
+
21
+ ## Installation
22
+
23
+ Install the gem and add to the application's Gemfile by executing:
24
+
25
+ ```shell
26
+ bundle add limitable
27
+ ```
28
+
29
+ If bundler is not being used to manage dependencies, install the gem by executing:
30
+
31
+ ```shell
32
+ gem install limitable
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ Once included in a model, `Limitable` will scan `integer`, `string` and `text` columns for size limits
38
+ and define _byte size_ validations accordingly. Limits are configurable through `ActiveRecord` migrations.
39
+
40
+ ### Quick Start
41
+
42
+ To enable these database limit validations globally:
43
+
44
+ ```ruby
45
+ class ApplicationRecord < ActiveRecord::Base
46
+ include Limitable::Base
47
+
48
+ # ...
49
+ end
50
+ ```
51
+
52
+ To enable database limit validations on a per-model basis:
53
+
54
+ ```ruby
55
+ class MyModel < ApplicationRecord
56
+ include Limitable
57
+
58
+ # ...
59
+ end
60
+ ```
61
+
62
+ ### SQL Adapters
63
+
64
+ `Limitable` is designed to be SQL adapter agnostic, however although some adapters have different default
65
+ default behaviors than others.
66
+
67
+ #### `mysql2`
68
+
69
+ MySQL/mariadb has and reports hard limits on all supported column types. As such, you won't need to specify explicit
70
+ limits in your database migrations/schema unless you want to change them from their default values.
71
+
72
+ #### `pg`
73
+
74
+ PostgreSQL has and reports hard limits on its integer columns, however it supports and defaults to unlimited
75
+ string/text columns. If you wish for limits to be set, they must be explicitly set in your database migrations/schema.
76
+
77
+ #### `sqlite3`
78
+
79
+ SQLite has hard limits on most of its column types, but it does not report them to active record. If you wish for limits
80
+ to be picked up, they must be explicitly set in your database migrations/schema.
81
+
82
+ ## Development
83
+
84
+ - Run `bin/setup` to install dependencies.
85
+ - Run `bin/rake appraisal rspec` to run the tests.
86
+ - Run `bin/rake rubocop` to run the linter.
87
+ - Run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/benmelz/limitable.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Limitable
4
+ # == Limitable::Base
5
+ #
6
+ # Module that allows for automatic inclusion of Limitable within subclasses when extended in a superclass.
7
+ #
8
+ module Base
9
+ def inherited(klass)
10
+ super
11
+ klass.include Limitable
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Limitable
4
+ VERSION = '1.0.0'
5
+ end
data/lib/limitable.rb ADDED
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'i18n'
5
+ require_relative 'limitable/base'
6
+ require_relative 'limitable/version'
7
+
8
+ # == Limitable
9
+ #
10
+ # Module that declares database limit validations when included in an ActiveRecord model class. Supports limit
11
+ # inferences on integer, string and text columns.
12
+ #
13
+ module Limitable
14
+ class << self
15
+ def included(klass)
16
+ safe_column_names(klass)&.each do |column_name|
17
+ add_limit_validation klass, klass.column_for_attribute(column_name), klass.type_for_attribute(column_name)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def safe_column_names(klass)
24
+ klass.column_names
25
+ rescue ActiveRecord::ActiveRecordError, ArgumentError
26
+ nil
27
+ end
28
+
29
+ def add_limit_validation(klass, column, type)
30
+ limit = column.sql_type_metadata.limit
31
+ return if limit.blank?
32
+
33
+ case column.type
34
+ when :integer
35
+ add_integer_limit_validation klass, column.name, limit, type
36
+ when :string, :text
37
+ add_string_limit_validation klass, column.name, limit, type
38
+ end
39
+ end
40
+
41
+ def add_integer_limit_validation(klass, column_name, limit, type)
42
+ min, max = integer_limit_range limit
43
+ integer_type_normalizer = method :integer_type_normalizer
44
+ klass.validate do
45
+ value = integer_type_normalizer.call self[column_name], type
46
+ next unless value.is_a?(Integer)
47
+
48
+ errors.add column_name, I18n.t('errors.messages.greater_than_or_equal_to', count: min) if value < min
49
+ errors.add column_name, I18n.t('errors.messages.less_than_or_equal_to', count: max) if value > max
50
+ end
51
+ end
52
+
53
+ def integer_limit_range(limit)
54
+ max = ('1' * (limit * 8)).to_i(2) / 2
55
+ min = -max
56
+ [min, max]
57
+ end
58
+
59
+ def integer_type_normalizer(value, type)
60
+ type.serialize value
61
+ rescue ActiveModel::RangeError
62
+ value.to_i
63
+ end
64
+
65
+ def add_string_limit_validation(klass, column_name, limit, type)
66
+ klass.validate do
67
+ value = type.serialize self[column_name]
68
+ next unless value.is_a?(String) && value.bytesize > limit
69
+
70
+ errors.add column_name, I18n.t('errors.messages.too_long.other', count: limit)
71
+ end
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: limitable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Melz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '6'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8'
33
+ - !ruby/object:Gem::Dependency
34
+ name: i18n
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '1.6'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '2'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '1.6'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '2'
53
+ description: 'Limitable scans your ActiveRecord database schema for column size limits
54
+ and defines corresponding model validations so that you don''t have to. '
55
+ email:
56
+ - ben@melz.me
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - CHANGELOG.md
62
+ - LICENSE.md
63
+ - README.md
64
+ - lib/limitable.rb
65
+ - lib/limitable/base.rb
66
+ - lib/limitable/version.rb
67
+ homepage: https://github.com/benmelz/limitable
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://github.com/benmelz/limitable
72
+ source_code_uri: https://github.com/benmelz/limitable
73
+ changelog_uri: https://github.com/benmelz/limitable/blob/v1.0.0/CHANGELOG.md
74
+ rubygems_mfa_required: 'true'
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.4.10
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Inferred database limit validations for ActiveRecord.
94
+ test_files: []