word-count-validator 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -7,32 +7,40 @@ module ActiveModel
7
7
 
8
8
  class WordCountValidator < ActiveModel::EachValidator
9
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
10
+ # Provide option defaults and normalize options. This is done
11
+ # prior to check_validity! as options becomes frozen and
12
+ # check_validity! can only report errors but not modify the
13
+ # options.
14
+ def initialize(options)
18
15
  options[:min] = options.delete(:minimum) if options.has_key?(:minimum)
19
16
  options[:max] = options.delete(:maximum) if options.has_key?(:maximum)
20
17
  options[:max] ||= 100
21
18
  options[:min] ||= 0
19
+ options[:strip_tags] = true unless options.has_key?(:strip_tags)
20
+ if options.has_key?(:in)
21
+ range = options[:in]
22
+ if range.present? && range.respond_to?(:min) && range.respond_to?(:max)
23
+ options[:min], options[:max] = range.min, range.max
24
+ options.delete :in
25
+ end
26
+ end
27
+ super
28
+ end
29
+
30
+ def check_validity!
31
+ raise ArgumentError, "You must provide a valid range for the number of words e.g. 2..100" if options.has_key?(:in)
22
32
  raise ArgumentError, "The min value must respond to to_i" unless options[:min].respond_to?(:to_i)
23
33
  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
34
  end
28
35
 
29
36
  def validate_each(record, attribute, value)
30
- min_words, max_words = options[:min], options[:max]
37
+ min_words, max_words = options[:min].to_i, options[:max].to_i
38
+ value = ActionController::Base.helpers.strip_tags value if options[:strip_tags]
31
39
  count = word_count_for(value)
32
40
  if !options[:skip_min] && count < min_words
33
- record.errors.add attribute, :too_few_words, options_for(min_words, count)
41
+ record.errors.add attribute, :too_few_words, options_for(count, min_words)
34
42
  elsif !options[:skip_max] && count > max_words
35
- record.errors.add attribute, :too_many_words, options_for(max_words, count)
43
+ record.errors.add attribute, :too_many_words, options_for(count, max_words)
36
44
  end
37
45
  end
38
46
 
@@ -45,7 +53,7 @@ module ActiveModel
45
53
  end
46
54
 
47
55
  def options_for(current, expected)
48
- base_options.merge :expected_count => expected_count, :actual_count => current
56
+ base_options.merge :expected_count => expected, :actual_count => current
49
57
  end
50
58
 
51
59
  end
@@ -59,4 +67,4 @@ module ActiveModel
59
67
 
60
68
  end
61
69
  end
62
- end
70
+ end
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{word-count-validator}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Darcy Laycock"]
12
+ s.date = %q{2010-11-30}
13
+ s.description = %q{Provides validates_word_count which lets users validate a string has a minimum / maximum number of words.}
14
+ s.email = %q{sutto@sutto.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/active_model/locale/en.yml",
26
+ "lib/active_model/validations/word_count.rb",
27
+ "lib/word-count-validator.rb",
28
+ "test/helper.rb",
29
+ "test/test_word-count-validator.rb",
30
+ "word-count-validator.gemspec"
31
+ ]
32
+ s.homepage = %q{http://github.com/Sutto/word-count-validator}
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.7}
35
+ s.summary = %q{Makes validating word counts in active_model easy}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_word-count-validator.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
51
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: word-count-validator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Darcy Laycock
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-05 00:00:00 +08:00
18
+ date: 2010-11-30 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,7 +30,6 @@ extra_rdoc_files:
30
30
  - README.rdoc
31
31
  files:
32
32
  - .document
33
- - .gitignore
34
33
  - LICENSE
35
34
  - README.rdoc
36
35
  - Rakefile
@@ -40,13 +39,14 @@ files:
40
39
  - lib/word-count-validator.rb
41
40
  - test/helper.rb
42
41
  - test/test_word-count-validator.rb
42
+ - word-count-validator.gemspec
43
43
  has_rdoc: true
44
44
  homepage: http://github.com/Sutto/word-count-validator
45
45
  licenses: []
46
46
 
47
47
  post_install_message:
48
- rdoc_options:
49
- - --charset=UTF-8
48
+ rdoc_options: []
49
+
50
50
  require_paths:
51
51
  - lib
52
52
  required_ruby_version: !ruby/object:Gem::Requirement
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC