string_length_conformable 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e9d964ace0305d966851b56fbe9cf38d88fb370f4b35b452705cf6609ad9dd37
4
+ data.tar.gz: 9017049d48b51d9db9ef431fc387caf21ad1a2f89dbe1b52daac4fd29d3f412c
5
+ SHA512:
6
+ metadata.gz: a1871ac7599f6dc6da4b83f4e945a9af523f8a9fa44b6305ebe83e05e389f428b578d8eb52c6095cc6eee26574704e3acc4e19f9c17e5ccb037e7d3d38d1f445
7
+ data.tar.gz: cbf5e0eb93cb1ff22ea511425a71b6c3cff62cc2f2ced99a97e52074c6d499bb5fad3284ee039bfab92869960d5ffb29a5f740632f315d8cf0fbf6ebb16adf18
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ *.gem
12
+
13
+ # byebug
14
+ .byebug_history
15
+
16
+ # macOS
17
+ *.DS_Store
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in string_length_conformable.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Vladimir Railean
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,59 @@
1
+ # StringLengthConformable
2
+
3
+ # USE WITH MySQL
4
+
5
+ If you have some attributes in your models with type string (varchar(255) by default).
6
+ And you haven't validate length of it, you can face once with a problem when user will
7
+ pass in form field more characters. Then, your database(MySQL), most probably will throw an error 500.
8
+ To avoid such kind a problems I've createt this gem.
9
+
10
+ This gem will throw a human readable exception when there are not validations applied for string, and user tries to pass longer string, then is specified in DB.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'string_length_conformable'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install string_length_conformable
27
+
28
+ ## Usage
29
+
30
+ ### To validate all strings in certain model, add `acts_as_string_length_conformable` method in that model
31
+
32
+ ```ruby
33
+ # model_name.rb
34
+ class ModelName < ApplicationRecord
35
+ acts_as_string_length_conformable
36
+
37
+ end
38
+ ```
39
+ ### To validate all strings in whole app, add `acts_as_string_length_conformable` in ApplicationRecord
40
+
41
+ ```ruby
42
+ class ApplicationRecord < ActiveRecord::Base
43
+ acts_as_string_length_conformable
44
+ self.abstract_class = true
45
+ end
46
+
47
+ ```
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( https://github.com/Yaponcik/string_length_conformable/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "string_length_conformable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ require "string_length_conformable/version"
2
+
3
+ ActiveSupport.on_load(:active_record) do
4
+ class ActiveRecord::Base
5
+ class NotMysqlDb < StandardError; end
6
+ def self.acts_as_string_length_conformable
7
+
8
+ unless ActiveRecord::Base.connection.adapter_name == 'MySQL'
9
+ raise NotMysqlDb, 'Please use this gem with mysql. But better use PostgreSQL and be happy!'
10
+ end
11
+
12
+ validates_with LengthOfStringValidator
13
+ end
14
+ class LengthOfStringValidator < ActiveModel::Validator
15
+ def validate(record)
16
+ record.attribute_names.each do |attr_name|
17
+
18
+ next if record.instance_eval(attr_name).nil?
19
+
20
+ next unless record.column_for_attribute(attr_name).type == :string
21
+
22
+ symbol_attr = attr_name.parameterize.underscore.to_sym
23
+
24
+ next if record.class.validators_on(symbol_attr).select { |v| v.class == ActiveRecord::Validations::LengthValidator}.any?
25
+
26
+ permited_length = record.class.columns_hash[attr_name].limit
27
+ new_length = record.instance_eval(attr_name).length
28
+
29
+ if new_length > permited_length.to_i
30
+ record.errors.add attr_name, "too long, #{permited_length.to_i} characters is the maximum allowed"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module StringLengthConformable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "string_length_conformable/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "string_length_conformable"
8
+ spec.version = StringLengthConformable::VERSION
9
+ spec.authors = ["Vladimir"]
10
+ spec.email = ["raileanv@gmail.com"]
11
+
12
+ spec.summary = %q{Gem for validate string length}
13
+ spec.description = <<-TEXT
14
+ If you have some attributes in your models with type string (varchar(255) by default).
15
+ And you haven't validate length of it, you can face once with a problem when user will
16
+ pass in form field more characters. Then, your database, most probably will throw an error 500.
17
+ To avoid such kind a problems I've createt this gem.
18
+ TEXT
19
+
20
+ spec.homepage = "https://github.com/Yaponcik/string_length_conformable"
21
+ spec.license = "MIT"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: string_length_conformable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |2
56
+ If you have some attributes in your models with type string (varchar(255) by default).
57
+ And you haven't validate length of it, you can face once with a problem when user will
58
+ pass in form field more characters. Then, your database, most probably will throw an error 500.
59
+ To avoid such kind a problems I've createt this gem.
60
+ email:
61
+ - raileanv@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - ".gitignore"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/string_length_conformable.rb
74
+ - lib/string_length_conformable/version.rb
75
+ - string_length_conformable.gemspec
76
+ homepage: https://github.com/Yaponcik/string_length_conformable
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.7.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Gem for validate string length
100
+ test_files: []