limitable 1.0.1 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e90558c6154eec0d84bc96b53b16feb809a6dfab2135a054e5d59a6714c49cfc
4
- data.tar.gz: 5a2ac5366290ab0fe20904dd80166ca76a7d7f9aa0d1591d43a5d850312dd652
3
+ metadata.gz: 40858400b0402bc9997ba697359789cf32255fc366d746a79d9fefb01c725ef1
4
+ data.tar.gz: d8719386f9528e0f9c508ece436f02259cc620a9692f84466eef205c2d4eab09
5
5
  SHA512:
6
- metadata.gz: f575314e180d4b218aaea9a062acaaa6aa26ac5193ae9a37d699f9bf0f4ed352bdd3960de99526b7d21a8ce1f5ea2a247e80b60ce565dd899ed50a229d8fe367
7
- data.tar.gz: d08094e1249af6b703cd84c255d1e0b8fcfd23c0d7fe7c7f5c3d67fca369860ece3bcad070af83c1856ec639dabbf9a26d08bfa7aae848f62a054707f01f0bca
6
+ metadata.gz: cf6db368c846bd59f870c569d31a49f7bcb6a2d9e5cb508256b9acdbd9cf8b28582db914d0320af77354f8ea2517bcbbbcbb106861934fcbba96a52d25a89513
7
+ data.tar.gz: 205f0c6ced808e91f8c3475c0377093f52688e2b7e2c7b26e06e7071969f1c55a6692d1d271470b6f035eed45151910e604e04e23b6c66c28bfa3cd3c66013c4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [1.1.0](https://github.com/benmelz/limitable/compare/v1.0.1...v1.1.0) (2023-07-17)
2
+
3
+
4
+ ### Features
5
+
6
+ * support for binary column limits ([#14](https://github.com/benmelz/limitable/issues/14)) ([43224d1](https://github.com/benmelz/limitable/commit/43224d1351052833246b94666b7aca76f8c89aa5)), closes [#3](https://github.com/benmelz/limitable/issues/3)
7
+
1
8
  ## [1.0.1](https://github.com/benmelz/limitable/compare/v1.0.0...v1.0.1) (2023-06-09)
2
9
 
3
10
 
data/README.md CHANGED
@@ -34,8 +34,8 @@ gem install limitable
34
34
 
35
35
  ## Usage
36
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.
37
+ Once included in a model, `Limitable` will scan `integer`, `string`, `text` and `binary` columns for size limits,
38
+ defining byte size validations accordingly. Limits are configurable through `ActiveRecord` migrations.
39
39
 
40
40
  ### Quick Start
41
41
 
@@ -72,7 +72,7 @@ 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 validated on those columns, they must be explicitly set in your
75
+ string/text/binary columns. If you wish for limits to be validated on those columns, they must be explicitly set in your
76
76
  database migrations/schema.
77
77
 
78
78
  #### `sqlite3`
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limitable
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1.0'
5
5
  end
data/lib/limitable.rb CHANGED
@@ -8,13 +8,13 @@ require_relative 'limitable/version'
8
8
  # == Limitable
9
9
  #
10
10
  # Module that declares database limit validations when included in an ActiveRecord model class. Supports limit
11
- # inferences on integer, string and text columns.
11
+ # inferences on integer, string, text and binary columns.
12
12
  #
13
13
  module Limitable
14
14
  class << self
15
15
  def included(klass)
16
- safe_column_names(klass)&.each do |column_name|
17
- add_limit_validation klass, column_name
16
+ safe_column_names(klass).each do |column_name|
17
+ attach_limit_validator_if_needed klass, column_name
18
18
  end
19
19
  end
20
20
 
@@ -23,53 +23,51 @@ module Limitable
23
23
  def safe_column_names(klass)
24
24
  klass.column_names
25
25
  rescue ActiveRecord::ActiveRecordError, ArgumentError
26
- nil
26
+ []
27
27
  end
28
28
 
29
- def add_limit_validation(klass, column_name)
29
+ def attach_limit_validator_if_needed(klass, column_name)
30
30
  column = klass.column_for_attribute column_name
31
31
  limit = column.sql_type_metadata.limit
32
32
  return if limit.blank?
33
33
 
34
34
  case column.type
35
35
  when :integer
36
- add_integer_limit_validation klass, column_name, limit
37
- when :string, :text
38
- add_string_limit_validation klass, column_name, limit
36
+ klass.validate(&build_integer_limit_validator(column_name, limit))
37
+ when :binary, :string, :text
38
+ klass.validate(&build_string_limit_validator(column_name, limit))
39
39
  end
40
40
  end
41
41
 
42
- def add_integer_limit_validation(klass, column_name, limit)
42
+ def build_integer_limit_validator(column_name, limit)
43
43
  min, max = integer_limit_range limit
44
- integer_type_normalizer = method :integer_type_normalizer
45
- klass.validate do
46
- value = integer_type_normalizer.call klass, column_name, self[column_name]
47
- next unless value.is_a?(Integer)
44
+ lambda do
45
+ value = begin
46
+ self.class.type_for_attribute(column_name).serialize self[column_name]
47
+ rescue ActiveModel::RangeError => e
48
+ e.message.match(/(?<number>\d+) is out of range/)[:number].to_i
49
+ end
50
+ next unless value.is_a? Integer
48
51
 
49
52
  errors.add column_name, I18n.t('errors.messages.greater_than_or_equal_to', count: min) if value < min
50
53
  errors.add column_name, I18n.t('errors.messages.less_than_or_equal_to', count: max) if value > max
51
54
  end
52
55
  end
53
56
 
54
- def integer_limit_range(limit)
55
- max = (1 << ((limit * 8) - 1)) - 1
56
- min = -max
57
- [min, max]
58
- end
59
-
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
64
- end
65
-
66
- def add_string_limit_validation(klass, column_name, limit)
67
- klass.validate do
68
- value = klass.type_for_attribute(column_name).serialize self[column_name]
57
+ def build_string_limit_validator(column_name, limit)
58
+ lambda do
59
+ value = self.class.type_for_attribute(column_name).serialize self[column_name]
60
+ value = value.to_s if value.is_a? ActiveModel::Type::Binary::Data
69
61
  next unless value.is_a?(String) && value.bytesize > limit
70
62
 
71
63
  errors.add column_name, I18n.t('errors.messages.too_long.other', count: limit)
72
64
  end
73
65
  end
66
+
67
+ def integer_limit_range(limit)
68
+ max = (1 << ((limit * 8) - 1)) - 1
69
+ min = -max
70
+ [min, max]
71
+ end
74
72
  end
75
73
  end
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.1
4
+ version: 1.1.0
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-06-09 00:00:00.000000000 Z
11
+ date: 2023-07-17 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.1/CHANGELOG.md
73
+ changelog_uri: https://github.com/benmelz/limitable/blob/v1.1.0/CHANGELOG.md
74
74
  rubygems_mfa_required: 'true'
75
75
  post_install_message:
76
76
  rdoc_options: []