Class: ActiveModel::Validations::WordCountValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::WordCountValidator
- Defined in:
- lib/active_model/validations/word_count.rb
Instance Method Summary collapse
- #base_options ⇒ Object
- #check_validity! ⇒ Object
-
#initialize(options) ⇒ WordCountValidator
constructor
Provide option defaults and normalize options.
- #options_for(current, expected) ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
- #word_count_for(value) ⇒ Object
Constructor Details
#initialize(options) ⇒ WordCountValidator
Provide option defaults and normalize options. This is done prior to check_validity! as options becomes frozen and check_validity! can only report errors but not modify the options.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_model/validations/word_count.rb', line 16 def initialize() [:min] = .delete(:minimum) if .has_key?(:minimum) [:max] = .delete(:maximum) if .has_key?(:maximum) [:max] ||= 100 [:min] ||= 0 [:strip_tags] = true unless .has_key?(:strip_tags) [:strip_punctuation] = true unless .has_key?(:strip_punctuation) if .has_key?(:in) range = [:in] if range.present? && range.respond_to?(:min) && range.respond_to?(:max) [:min], [:max] = range.min, range.max .delete :in end end super end |
Instance Method Details
#base_options ⇒ Object
61 62 63 |
# File 'lib/active_model/validations/word_count.rb', line 61 def .except(:in, :min, :max, :skip_max, :skip_min) end |
#check_validity! ⇒ Object
33 34 35 36 37 |
# File 'lib/active_model/validations/word_count.rb', line 33 def check_validity! raise ArgumentError, "You must provide a valid range for the number of words e.g. 2..100" if .has_key?(:in) raise ArgumentError, "The min value must respond to to_i or call" unless [:min].respond_to?(:to_i) || [:min].respond_to?(:call) raise ArgumentError, "The max value must respond to to_i or call" unless [:max].respond_to?(:to_i) || [:max].respond_to?(:call) end |
#options_for(current, expected) ⇒ Object
65 66 67 |
# File 'lib/active_model/validations/word_count.rb', line 65 def (current, expected) .merge :expected_count => expected, :actual_count => current end |
#validate_each(record, attribute, value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_model/validations/word_count.rb', line 39 def validate_each(record, attribute, value) min_words = [:min].respond_to?(:call) ? [:min][record, attribute, value] : [:min].to_i max_words = [:max].respond_to?(:call) ? [:max][record, attribute, value] : [:max].to_i value = ActionController::Base.helpers.(value || '').gsub(/ | /i, ' ') if [:strip_tags] value.gsub! /[.(),;:!?%#\$'"_+=\/-]*/, '' if [:strip_punctuation] count = word_count_for(value) if ![:skip_min] && count < min_words record.errors.add attribute, :too_few_words, (count, min_words) elsif ![:skip_max] && count > max_words record.errors.add attribute, :too_many_words, (count, max_words) end end |
#word_count_for(value) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/active_model/validations/word_count.rb', line 52 def word_count_for(value) if RUBY_VERSION =~ /1\.[0-8]\.*/ regexp = /\w+/u else regexp = /\p{Word}+/ end value.to_s.scan(regexp).size end |