validate-limits 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ecde4cbd325010441374a00b6f378198f79f9f1
4
+ data.tar.gz: 33d892f0e51278258a77d0358c2c2aa9aafa23db
5
+ SHA512:
6
+ metadata.gz: b1c4268c406b52f28ecd64ff3bd76665ec972333fb41f6bbb5f5f54e49860624187addef517b9ebdf1c036115ab6f2930ae9a72ac548e05193048fbebca7747f
7
+ data.tar.gz: bc21b0334c1c218b3dfc0a9ba73997cc46d5a04dece52120506c71976e26b293919295e78c32169a9b574a55ad4014f865e12bb89d15de4c578d1722a22f68d0
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ .ruby-version
4
+ pkg/
5
+ tmp/
6
+ /.idea/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ source 'https://rubygems.org'
5
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ validate-limits (1.0.0)
5
+ activerecord (>= 3.0, < 6.0)
6
+ activesupport (>= 3.0, < 6.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.0.2)
12
+ activesupport (= 5.0.2)
13
+ activerecord (5.0.2)
14
+ activemodel (= 5.0.2)
15
+ activesupport (= 5.0.2)
16
+ arel (~> 7.0)
17
+ activesupport (5.0.2)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (~> 0.7)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ arel (7.1.4)
23
+ concurrent-ruby (1.0.5)
24
+ i18n (0.8.1)
25
+ minitest (5.10.1)
26
+ thread_safe (0.3.6)
27
+ tzinfo (1.2.2)
28
+ thread_safe (~> 0.1)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ validate-limits!
35
+
36
+ BUNDLED WITH
37
+ 1.14.3
@@ -0,0 +1,12 @@
1
+ ## Automatically validate limits for ActiveRecord attributes.
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/validate-limits.svg)](https://badge.fury.io/rb/validate-limits)
4
+
5
+ ## About
6
+ This gem enables automatic limit validation for every ActiveRecord attribute.
7
+
8
+ ## Installing gem
9
+ Add to your Gemfile:
10
+ ```ruby
11
+ gem 'validate-limits', '~> 1.0'
12
+ ```
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new { |t| t.libs << 'test' }
7
+
8
+ task default: :test
@@ -0,0 +1,49 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'active_support/concern'
5
+ require 'active_support/lazy_load_hooks'
6
+
7
+ module ValidateLimits
8
+ module Extension
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ class_attribute :attributes_with_limit_validation, instance_accessor: false, instance_predicate: false
13
+ class_eval { self.attributes_with_limit_validation = [] }
14
+ end
15
+
16
+ module ClassMethods
17
+ def inherited(cls)
18
+ super.tap { cls.validate_limits }
19
+ end
20
+
21
+ def validate_limits
22
+ return if defined?(ActiveRecord::SchemaMigration) && self <= ActiveRecord::SchemaMigration
23
+ return if abstract_class?
24
+ return remove_instance_variable(:@table_name) unless table_name.in?(ActiveRecord::Base.connection.tables)
25
+
26
+ columns_hash.values.each do |column|
27
+ next if attributes_with_limit_validation.include?(column.name)
28
+
29
+ case column.type
30
+ when :string, :text
31
+ self.attributes_with_limit_validation += [column.name]
32
+ validates_length_of column.name, maximum: column.limit
33
+
34
+ when :integer
35
+ self.attributes_with_limit_validation += [column.name]
36
+ bits = column.limit * 8
37
+ min = -(2**(bits-1))
38
+ max = +(2**(bits-1))-1
39
+ validates_numericality_of column.name, greater_than_or_equal_to: min,
40
+ less_than_or_equal_to: max,
41
+ allow_nil: column.null
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ ActiveSupport.on_load(:active_record) { ActiveRecord::Base.include ValidateLimits::Extension }
@@ -0,0 +1,6 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ module ValidateLimits
5
+ VERSION = '1.0.0'
6
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ require File.expand_path('../lib/validate-limits/version', __FILE__)
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'validate-limits'
8
+ s.version = ValidateLimits::VERSION
9
+ s.author = 'Yaroslav Konoplov'
10
+ s.email = 'eahome00@gmail.com'
11
+ s.summary = 'Automatically validate limits for ActiveRecord attributes.'
12
+ s.description = 'Automatically validate limits for ActiveRecord attributes.'
13
+ s.homepage = 'https://github.com/yivo/validate-limits'
14
+ s.license = 'MIT'
15
+
16
+ s.files = `git ls-files -z`.split("\x0")
17
+ s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_dependency 'activesupport', '>= 3.0', '< 6.0'
21
+ s.add_dependency 'activerecord', '>= 3.0', '< 6.0'
22
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validate-limits
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yaroslav Konoplov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activerecord
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '6.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '6.0'
53
+ description: Automatically validate limits for ActiveRecord attributes.
54
+ email: eahome00@gmail.com
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - ".gitignore"
60
+ - Gemfile
61
+ - Gemfile.lock
62
+ - README.md
63
+ - Rakefile
64
+ - lib/validate-limits.rb
65
+ - lib/validate-limits/version.rb
66
+ - validate-limits.gemspec
67
+ homepage: https://github.com/yivo/validate-limits
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.6.8
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Automatically validate limits for ActiveRecord attributes.
91
+ test_files: []