limitable 1.0.0 → 1.0.1
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 +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +8 -7
- data/lib/limitable/version.rb +1 -1
- data/lib/limitable.rb +14 -13
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e90558c6154eec0d84bc96b53b16feb809a6dfab2135a054e5d59a6714c49cfc
|
4
|
+
data.tar.gz: 5a2ac5366290ab0fe20904dd80166ca76a7d7f9aa0d1591d43a5d850312dd652
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f575314e180d4b218aaea9a062acaaa6aa26ac5193ae9a37d699f9bf0f4ed352bdd3960de99526b7d21a8ce1f5ea2a247e80b60ce565dd899ed50a229d8fe367
|
7
|
+
data.tar.gz: d08094e1249af6b703cd84c255d1e0b8fcfd23c0d7fe7c7f5c3d67fca369860ece3bcad070af83c1856ec639dabbf9a26d08bfa7aae848f62a054707f01f0bca
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## [1.0.1](https://github.com/benmelz/limitable/compare/v1.0.0...v1.0.1) (2023-06-09)
|
2
|
+
|
3
|
+
|
4
|
+
### Bug Fixes
|
5
|
+
|
6
|
+
* consider serialized values for custom types ([#7](https://github.com/benmelz/limitable/issues/7)) ([86cf913](https://github.com/benmelz/limitable/commit/86cf91307211411db3582fa3d780df6f96663bfb))
|
7
|
+
|
1
8
|
# 1.0.0 (2023-05-30)
|
2
9
|
|
3
10
|
|
data/README.md
CHANGED
@@ -35,15 +35,15 @@ gem install limitable
|
|
35
35
|
## Usage
|
36
36
|
|
37
37
|
Once included in a model, `Limitable` will scan `integer`, `string` and `text` columns for size limits
|
38
|
-
and define
|
38
|
+
and define byte size validations accordingly. Limits are configurable through `ActiveRecord` migrations.
|
39
39
|
|
40
40
|
### Quick Start
|
41
41
|
|
42
|
-
To enable
|
42
|
+
To enable database limit validations globally:
|
43
43
|
|
44
44
|
```ruby
|
45
45
|
class ApplicationRecord < ActiveRecord::Base
|
46
|
-
|
46
|
+
extend Limitable::Base
|
47
47
|
|
48
48
|
# ...
|
49
49
|
end
|
@@ -61,8 +61,8 @@ end
|
|
61
61
|
|
62
62
|
### SQL Adapters
|
63
63
|
|
64
|
-
`Limitable` is designed to be SQL adapter agnostic, however
|
65
|
-
|
64
|
+
`Limitable` is designed to be SQL adapter agnostic, however different adapters have different default behaviors that
|
65
|
+
affect their integration with this library.
|
66
66
|
|
67
67
|
#### `mysql2`
|
68
68
|
|
@@ -72,12 +72,13 @@ limits in your database migrations/schema unless you want to change them from th
|
|
72
72
|
#### `pg`
|
73
73
|
|
74
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
|
75
|
+
string/text columns. If you wish for limits to be validated on those columns, they must be explicitly set in your
|
76
|
+
database migrations/schema.
|
76
77
|
|
77
78
|
#### `sqlite3`
|
78
79
|
|
79
80
|
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
|
81
|
+
to be validated, they must be explicitly set in your database migrations/schema.
|
81
82
|
|
82
83
|
## Development
|
83
84
|
|
data/lib/limitable/version.rb
CHANGED
data/lib/limitable.rb
CHANGED
@@ -14,7 +14,7 @@ module Limitable
|
|
14
14
|
class << self
|
15
15
|
def included(klass)
|
16
16
|
safe_column_names(klass)&.each do |column_name|
|
17
|
-
add_limit_validation klass,
|
17
|
+
add_limit_validation klass, column_name
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -26,23 +26,24 @@ module Limitable
|
|
26
26
|
nil
|
27
27
|
end
|
28
28
|
|
29
|
-
def add_limit_validation(klass,
|
29
|
+
def add_limit_validation(klass, column_name)
|
30
|
+
column = klass.column_for_attribute column_name
|
30
31
|
limit = column.sql_type_metadata.limit
|
31
32
|
return if limit.blank?
|
32
33
|
|
33
34
|
case column.type
|
34
35
|
when :integer
|
35
|
-
add_integer_limit_validation klass,
|
36
|
+
add_integer_limit_validation klass, column_name, limit
|
36
37
|
when :string, :text
|
37
|
-
add_string_limit_validation klass,
|
38
|
+
add_string_limit_validation klass, column_name, limit
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
41
|
-
def add_integer_limit_validation(klass, column_name, limit
|
42
|
+
def add_integer_limit_validation(klass, column_name, limit)
|
42
43
|
min, max = integer_limit_range limit
|
43
44
|
integer_type_normalizer = method :integer_type_normalizer
|
44
45
|
klass.validate do
|
45
|
-
value = integer_type_normalizer.call self[column_name]
|
46
|
+
value = integer_type_normalizer.call klass, column_name, self[column_name]
|
46
47
|
next unless value.is_a?(Integer)
|
47
48
|
|
48
49
|
errors.add column_name, I18n.t('errors.messages.greater_than_or_equal_to', count: min) if value < min
|
@@ -51,20 +52,20 @@ module Limitable
|
|
51
52
|
end
|
52
53
|
|
53
54
|
def integer_limit_range(limit)
|
54
|
-
max = (
|
55
|
+
max = (1 << ((limit * 8) - 1)) - 1
|
55
56
|
min = -max
|
56
57
|
[min, max]
|
57
58
|
end
|
58
59
|
|
59
|
-
def integer_type_normalizer(
|
60
|
-
|
61
|
-
rescue ActiveModel::RangeError
|
62
|
-
|
60
|
+
def integer_type_normalizer(klass, column_name, value)
|
61
|
+
klass.type_for_attribute(column_name).serialize value
|
62
|
+
rescue ActiveModel::RangeError => e
|
63
|
+
e.message.match(/(?<number>\d+) is out of range/)[:number].to_i
|
63
64
|
end
|
64
65
|
|
65
|
-
def add_string_limit_validation(klass, column_name, limit
|
66
|
+
def add_string_limit_validation(klass, column_name, limit)
|
66
67
|
klass.validate do
|
67
|
-
value =
|
68
|
+
value = klass.type_for_attribute(column_name).serialize self[column_name]
|
68
69
|
next unless value.is_a?(String) && value.bytesize > limit
|
69
70
|
|
70
71
|
errors.add column_name, I18n.t('errors.messages.too_long.other', count: limit)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: limitable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Melz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -70,7 +70,7 @@ licenses:
|
|
70
70
|
metadata:
|
71
71
|
homepage_uri: https://github.com/benmelz/limitable
|
72
72
|
source_code_uri: https://github.com/benmelz/limitable
|
73
|
-
changelog_uri: https://github.com/benmelz/limitable/blob/v1.0.
|
73
|
+
changelog_uri: https://github.com/benmelz/limitable/blob/v1.0.1/CHANGELOG.md
|
74
74
|
rubygems_mfa_required: 'true'
|
75
75
|
post_install_message:
|
76
76
|
rdoc_options: []
|