validates_not_shouting 0.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 +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +12 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +13 -0
- data/lib/validates_not_shouting.rb +7 -0
- data/lib/validates_not_shouting/not_shouting_validator.rb +16 -0
- data/lib/validates_not_shouting/version.rb +3 -0
- data/test/model_test.rb +52 -0
- data/test/test_helper.rb +5 -0
- data/validates_not_shouting.gemspec +25 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 47526e07aaebbf01f2c81d7b36a3882c1bb2e5e9
|
4
|
+
data.tar.gz: 1d297c90370169b4d83cb518a66af3bea7020b97
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 77a93d81652381c33568b563517604a1e1bb3d51702faaad3340274f26550a4d617d43af5b2b3833af2f726d4c5d910f1a89aa36ff846e6684dfc042b56852f6
|
7
|
+
data.tar.gz: df2d5d612fb337faaa25171a86c01f85c38c93e7f403249c4cf52f03f766af33e65b61ab156b46d72956305eaf1ad289fb6abe1a2dbd669bf5d5bd5d01751890
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in validates_not_shouting.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
am_version = ENV["AM_VERSION"] || "default"
|
7
|
+
|
8
|
+
am_version = case am_version
|
9
|
+
when "default"
|
10
|
+
">= 3.2.0"
|
11
|
+
else
|
12
|
+
"~> #{am_version}"
|
13
|
+
end
|
14
|
+
|
15
|
+
gem "activesupport", am_version
|
16
|
+
|
17
|
+
gem "codeclimate-test-reporter", group: :test, require: nil
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Jeff Ching
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# ValidatesNotShouting [](https://travis-ci.org/avvo/validates_not_shouting) [](https://codeclimate.com/github/avvo/validates_not_shouting) [](https://codeclimate.com/github/avvo/validates_not_shouting)
|
2
|
+
|
3
|
+
To prevent noobs from being uber noobs.
|
4
|
+
|
5
|
+
Originally idea conceived by [bvandenbos](https://github.com/bvandenbos). ActiveModel::Validator
|
6
|
+
to prevent the percentage of capital letters.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'validates_not_shouting'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install validates_not_shouting
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```
|
27
|
+
|
28
|
+
class Foo
|
29
|
+
include ActiveModel::Validations
|
30
|
+
attr_accessor :foo
|
31
|
+
|
32
|
+
validates :foo, not_shouting: true
|
33
|
+
end
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
## Configuration
|
38
|
+
|
39
|
+
You can set the message and threshold as configuration for the validator. The default threshold is 0.5 (half of the characters).
|
40
|
+
|
41
|
+
validates :foo, not_shouting: {threshold: 0.1, message: 'cannot be so shouty.'}
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( https://github.com/avvo/validates_not_shouting/fork )
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# this is not namespaced so that we can do
|
2
|
+
# validates :body, not_shouting: true
|
3
|
+
class NotShoutingValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
return true if value.blank?
|
6
|
+
|
7
|
+
if shouting?(value)
|
8
|
+
record.errors[attribute] << options.fetch(:message, "cannot be mostly caps. It's not polite to shout.")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def shouting?(value)
|
13
|
+
threshold = options.fetch(:threshold, 0.5)
|
14
|
+
(value.gsub(/[^A-Z]/, '').length.to_f / value.length) > threshold
|
15
|
+
end
|
16
|
+
end
|
data/test/model_test.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ModelTest < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
class TestModel
|
6
|
+
attr_accessor :body, :subject, :blurb
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
validates :body, :subject, not_shouting: {threshold: 0.1}
|
10
|
+
end
|
11
|
+
|
12
|
+
class TestModelNoThreshold
|
13
|
+
attr_accessor :body, :subject, :blurb
|
14
|
+
include ActiveModel::Validations
|
15
|
+
|
16
|
+
validates :body, :subject, not_shouting: true
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_validates_nil
|
20
|
+
m = TestModel.new
|
21
|
+
assert(m.valid?)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_validates_shouting
|
25
|
+
m = TestModel.new
|
26
|
+
m.body = "THIS IS SHOUTING AND SHOULD NOT BE OK"
|
27
|
+
m.subject = "THIS IS NOT OK"
|
28
|
+
assert !m.valid?
|
29
|
+
assert m.errors[:body].present?
|
30
|
+
assert m.errors[:subject].present?
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_validates_with_threshold
|
34
|
+
m = TestModel.new
|
35
|
+
m.subject = "This is actually ok" # < 10%
|
36
|
+
assert m.valid?
|
37
|
+
|
38
|
+
m = TestModel.new
|
39
|
+
m.subject = "THIs is actually ok" # > 10%
|
40
|
+
assert !m.valid?
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_without_threshold
|
44
|
+
m = TestModelNoThreshold.new
|
45
|
+
m.body = "ABCabc" # 50% ok
|
46
|
+
assert(m.valid?)
|
47
|
+
|
48
|
+
m.body = "ABCDabc" # >50% not ok
|
49
|
+
assert(!m.valid?)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'validates_not_shouting/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "validates_not_shouting"
|
8
|
+
spec.version = ValidatesNotShouting::VERSION
|
9
|
+
spec.authors = ["Jeff Ching"]
|
10
|
+
spec.email = ["jching@avvo.com"]
|
11
|
+
spec.summary = "To prevent noobs from being uber noobs"
|
12
|
+
spec.description = "To prevent noobs from being uber noobs"
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel", ">= 3.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_not_shouting
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Ching
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: To prevent noobs from being uber noobs
|
56
|
+
email:
|
57
|
+
- jching@avvo.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/validates_not_shouting.rb
|
69
|
+
- lib/validates_not_shouting/not_shouting_validator.rb
|
70
|
+
- lib/validates_not_shouting/version.rb
|
71
|
+
- test/model_test.rb
|
72
|
+
- test/test_helper.rb
|
73
|
+
- validates_not_shouting.gemspec
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: To prevent noobs from being uber noobs
|
98
|
+
test_files:
|
99
|
+
- test/model_test.rb
|
100
|
+
- test/test_helper.rb
|
101
|
+
has_rdoc:
|