limitable 1.0.0 → 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: d6c12a16cc4494f377161bf1391268d66e74cce94d4483c0f62a9d6cca7aa7ec
4
- data.tar.gz: 5d691e45c1b9a244289781285dcaf05445e2b965ff3116c1b5492da1c2284741
3
+ metadata.gz: 40858400b0402bc9997ba697359789cf32255fc366d746a79d9fefb01c725ef1
4
+ data.tar.gz: d8719386f9528e0f9c508ece436f02259cc620a9692f84466eef205c2d4eab09
5
5
  SHA512:
6
- metadata.gz: 2b9c14a552cd790b9dd0ddce4d8e8dda51b9407eef62abf5a72c86ea0f7de08343699a6e88809632f2630048cd6f5a696fb7af414eb1e4493f6f943b246a7e49
7
- data.tar.gz: 06ab70e18d5063028f850fca942a96638456f14611f6ff1c51d7c26c3e1c5f2a70c3794e214036aa574bb97dfa5ed416e145532c6100bae3512d3a845249f229
6
+ metadata.gz: cf6db368c846bd59f870c569d31a49f7bcb6a2d9e5cb508256b9acdbd9cf8b28582db914d0320af77354f8ea2517bcbbbcbb106861934fcbba96a52d25a89513
7
+ data.tar.gz: 205f0c6ced808e91f8c3475c0377093f52688e2b7e2c7b26e06e7071969f1c55a6692d1d271470b6f035eed45151910e604e04e23b6c66c28bfa3cd3c66013c4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
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
+
8
+ ## [1.0.1](https://github.com/benmelz/limitable/compare/v1.0.0...v1.0.1) (2023-06-09)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * consider serialized values for custom types ([#7](https://github.com/benmelz/limitable/issues/7)) ([86cf913](https://github.com/benmelz/limitable/commit/86cf91307211411db3582fa3d780df6f96663bfb))
14
+
1
15
  # 1.0.0 (2023-05-30)
2
16
 
3
17
 
data/README.md CHANGED
@@ -34,16 +34,16 @@ 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
 
42
- To enable these database limit validations globally:
42
+ To enable database limit validations globally:
43
43
 
44
44
  ```ruby
45
45
  class ApplicationRecord < ActiveRecord::Base
46
- include Limitable::Base
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 although some adapters have different default
65
- default behaviors than others.
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 set, they must be explicitly set in your database migrations/schema.
75
+ string/text/binary 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 picked up, they must be explicitly set in your database migrations/schema.
81
+ to be validated, they must be explicitly set in your database migrations/schema.
81
82
 
82
83
  ## Development
83
84
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limitable
4
- VERSION = '1.0.0'
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, klass.column_for_attribute(column_name), klass.type_for_attribute(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,52 +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, type)
29
+ def attach_limit_validator_if_needed(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, column.name, limit, type
36
- when :string, :text
37
- add_string_limit_validation klass, column.name, limit, type
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))
38
39
  end
39
40
  end
40
41
 
41
- def add_integer_limit_validation(klass, column_name, limit, type)
42
+ def build_integer_limit_validator(column_name, limit)
42
43
  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)
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
47
51
 
48
52
  errors.add column_name, I18n.t('errors.messages.greater_than_or_equal_to', count: min) if value < min
49
53
  errors.add column_name, I18n.t('errors.messages.less_than_or_equal_to', count: max) if value > max
50
54
  end
51
55
  end
52
56
 
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]
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
68
61
  next unless value.is_a?(String) && value.bytesize > limit
69
62
 
70
63
  errors.add column_name, I18n.t('errors.messages.too_long.other', count: limit)
71
64
  end
72
65
  end
66
+
67
+ def integer_limit_range(limit)
68
+ max = (1 << ((limit * 8) - 1)) - 1
69
+ min = -max
70
+ [min, max]
71
+ end
73
72
  end
74
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.0
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-05-30 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.0/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: []