word-count-validator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/active_model/locale/en.yml +5 -0
- data/lib/active_model/validations/word_count.rb +62 -0
- data/lib/word-count-validator.rb +1 -0
- data/test/helper.rb +10 -0
- data/test/test_word-count-validator.rb +7 -0
- metadata +79 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Darcy Laycock
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= word-count-validator
|
2
|
+
|
3
|
+
A simple ActiveModel validator for requiring a model has a word count in a specific range.
|
4
|
+
Uses a very basic method for generating the count (scans for \w+)
|
5
|
+
|
6
|
+
== Note on Patches/Pull Requests
|
7
|
+
|
8
|
+
* Fork the project.
|
9
|
+
* Make your feature addition or bug fix.
|
10
|
+
* Add tests for it. This is important so I don't break it in a
|
11
|
+
future version unintentionally.
|
12
|
+
* Commit, do not mess with rakefile, version, or history.
|
13
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2010 Darcy Laycock. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "word-count-validator"
|
8
|
+
gem.summary = %Q{Makes validating word counts in active_model easy}
|
9
|
+
gem.description = %Q{Provides validates_word_count which lets users validate a string has a minimum / maximum number of words.}
|
10
|
+
gem.email = "sutto@sutto.net"
|
11
|
+
gem.homepage = "http://github.com/Sutto/word-count-validator"
|
12
|
+
gem.authors = ["Darcy Laycock"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/test_*.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task :test => :check_dependencies
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "word-count-validator #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support/i18n'
|
3
|
+
I18n.load_path << File.expand_path(File.dirname(__FILE__) + "/../locale/en.yml")
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
module Validations
|
7
|
+
|
8
|
+
class WordCountValidator < ActiveModel::EachValidator
|
9
|
+
|
10
|
+
def check_validity!
|
11
|
+
if options.has_key?(:in)
|
12
|
+
range = options.delete(:in)
|
13
|
+
unless range.present? && range.respond_to?(:min) && range.respond_to?(:max)
|
14
|
+
raise ArgumentError, "You must provide a valid range for the number of words e.g. 2..100"
|
15
|
+
end
|
16
|
+
options[:min], options[:max] = range.min, range.max
|
17
|
+
end
|
18
|
+
options[:min] = options.delete(:minimum) if options.has_key?(:minimum)
|
19
|
+
options[:max] = options.delete(:maximum) if options.has_key?(:maximum)
|
20
|
+
options[:max] ||= 100
|
21
|
+
options[:min] ||= 0
|
22
|
+
raise ArgumentError, "The min value must respond to to_i" unless options[:min].respond_to?(:to_i)
|
23
|
+
raise ArgumentError, "The max value must respond to to_i" unless options[:max].respond_to?(:to_i)
|
24
|
+
# Finally, normalize the min values.
|
25
|
+
options[:min] = options[:min].to_i
|
26
|
+
options[:max] = options[:max].to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_each(record, attribute, value)
|
30
|
+
min_words, max_words = options[:min], options[:max]
|
31
|
+
count = word_count_for(value)
|
32
|
+
if !options[:skip_min] && count < min_words
|
33
|
+
record.errors.add attribute, :too_few_words, options_for(min_words, count)
|
34
|
+
elsif !options[:skip_max] && count > max_words
|
35
|
+
record.errors.add attribute, :too_many_words, options_for(max_words, count)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def word_count_for(value)
|
40
|
+
value.to_s.scan(/\w+/).size
|
41
|
+
end
|
42
|
+
|
43
|
+
def base_options
|
44
|
+
options.except(:in, :min, :max, :skip_max, :skip_min)
|
45
|
+
end
|
46
|
+
|
47
|
+
def options_for(current, expected)
|
48
|
+
base_options.merge :expected_count => expected_count, :actual_count => current
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
module HelperMethods
|
54
|
+
|
55
|
+
# Lets you validate that given attributes have a word count within a specific range.
|
56
|
+
def validates_word_count(*attr_names)
|
57
|
+
validates_with WordCountValidator, _merge_attributes(attr_names)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_model/validations/word_count'
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: word-count-validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Darcy Laycock
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-05 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Provides validates_word_count which lets users validate a string has a minimum / maximum number of words.
|
23
|
+
email: sutto@sutto.net
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- .document
|
33
|
+
- .gitignore
|
34
|
+
- LICENSE
|
35
|
+
- README.rdoc
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- lib/active_model/locale/en.yml
|
39
|
+
- lib/active_model/validations/word_count.rb
|
40
|
+
- lib/word-count-validator.rb
|
41
|
+
- test/helper.rb
|
42
|
+
- test/test_word-count-validator.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/Sutto/word-count-validator
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Makes validating word counts in active_model easy
|
77
|
+
test_files:
|
78
|
+
- test/helper.rb
|
79
|
+
- test/test_word-count-validator.rb
|