validatious 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -65,6 +65,34 @@ to the correct format of an email address.
65
65
  or
66
66
 
67
67
  validates_email_format_of :email
68
+
69
+
70
+ ### Text Content
71
+
72
+ Extracted from [validates_text_content](https://github.com/aarongough/validates_text_content), this validates the content of a text attribute, to determine
73
+ whether or not a block of English text meets certain quality standards.
74
+
75
+ validates :comment, :text_content => true
76
+
77
+ or
78
+
79
+ validates_text_content_of :comment
80
+
81
+ #### Examples
82
+
83
+ - Rejects:
84
+ - "this comment has absolutely no capitalization."
85
+ - "This comment has no type of punctuation"
86
+ - "Fdkjd dk dkkjhd kjdhkjd kjdhdhdl alkasla lka alk"
87
+ - "THIS COMMENT IS ALL IN UPPERCASE. ME NO WANT!"
88
+ - "Thiscommenthasabsolutelynospacesthereisnowaythisisgood."
89
+ - "THIS COMMENT is MORE THAN 80% CAPITALS."
90
+ - "THIS pERSON hAS tHEIR cAPS-lOCK oN."
91
+
92
+ - Accepts:
93
+ - "This is a very simple test comment."
94
+ - "Supercalifragilisticexpialadocious: a super long word!"
95
+ - "How many words can I think of that don't contain an 'e'?"
68
96
 
69
97
 
70
98
  ## CONTRIBUTE
data/lib/validatious.rb CHANGED
@@ -8,6 +8,7 @@ end
8
8
 
9
9
  require "validatious/validators/url_validator"
10
10
  require "validatious/validators/email_validator"
11
+ require "validatious/validators/text_content_validator"
11
12
  require "validatious/helper_methods"
12
13
 
13
14
  require 'active_support/i18n'
@@ -10,6 +10,10 @@ module ActiveModel
10
10
  validates_with EmailValidator, _merge_attributes(attr_names)
11
11
  end
12
12
 
13
+ def validates_text_content_of(*attr_names)
14
+ validates_with TextContentValidator, _merge_attributes(attr_names)
15
+ end
16
+
13
17
  end
14
18
  end
15
19
  end
@@ -2,4 +2,5 @@ en:
2
2
  errors:
3
3
  messages:
4
4
  invalid_url: "is not a valid URL"
5
- invalid_email: "is not a valid email address"
5
+ invalid_email: "is not a valid email address"
6
+ invalid_text_content: "has incorrect capitalization and punctuation. (ie: All sentences should begin with a capital letter and end with a punctuation mark, etc.)"
@@ -0,0 +1,33 @@
1
+ module Validatious
2
+ module Validators
3
+
4
+ # == Active Model Text Content Validator
5
+ class TextContentValidator < ActiveModel::EachValidator
6
+ def validate_each(record, attribute, value)
7
+ return if value.nil?
8
+
9
+ error = false
10
+
11
+ # The text should start with a capital letter
12
+ error = true if value.match(/[A-Z]/).nil?
13
+
14
+ # The text should contain at least one punctuation mark
15
+ error = true if value.match(/[\.?!]/).nil?
16
+
17
+ # There should be at least one 'e' per 30 characters
18
+ error = true if value.downcase.count('e') < (value.downcase.gsub(/[^a-z]*/, '').length / 30)
19
+
20
+ # There should be at least one space per 20 characters
21
+ error = true if value.downcase.count(' ') < (value.length / 20)
22
+
23
+ # The text should be at least 25% lowercase
24
+ error = true if value.gsub(/[^a-z]*/, '').length < (value.downcase.gsub(/[^a-z]*/, '').length / 4)
25
+
26
+ record.errors.add(attribute, :invalid_text_content, options) if error
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ # Compatibility with ActiveModel validates method which matches option keys to their validator class
33
+ ActiveModel::Validations::TextContentValidator = Validatious::Validators::TextContentValidator
@@ -1,3 +1,3 @@
1
1
  module Validatious
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -2,7 +2,7 @@ class Post
2
2
  include ActiveModel::Validations
3
3
  include ActiveModel::Validations::Callbacks
4
4
 
5
- attr_accessor :title, :url
5
+ attr_accessor :title, :url, :comment
6
6
 
7
7
  def initialize(attributes = {})
8
8
  attributes.each do |key, value|
@@ -5,6 +5,7 @@ describe Validatious, 'HelperMethods' do
5
5
  it 'should define class validation methods' do
6
6
  Post.should respond_to(:validates_url_format_of)
7
7
  Post.should respond_to(:validates_email_format_of)
8
+ Post.should respond_to(:validates_text_content_of)
8
9
  end
9
10
 
10
11
  end
@@ -8,7 +8,7 @@ describe Validatious::Validators::EmailValidator do
8
8
  Topic.validates_email_format_of(:email)
9
9
  end
10
10
 
11
- let(:topic) { post = Topic.new(:title => "The title", :email => "invalid email") }
11
+ let(:topic) { Topic.new(:title => "The title", :email => "invalid email") }
12
12
 
13
13
  [
14
14
  'bob@bones.com',
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe Validatious::Validators::TextContentValidator do
5
+
6
+ before(:each) do
7
+ Post.validates_text_content_of(:comment)
8
+ end
9
+
10
+ let(:post) { Post.new }
11
+
12
+ [
13
+ "",
14
+ 'this comment has absolutely no capitalization.',
15
+ 'This comment has no type of punctuation',
16
+ "Fdkjd dk dkkjhd kjdhkjd kjdhdhdl alkasla lka alk.",
17
+ "THIS COMMENT IS ALL IN UPPERCASE. ME NO WANT.",
18
+ "Thiscommenthasabsolutelynospacesthereisnowaythisisgood.",
19
+ "THIS COMMENT is MORE THAN 80% CAPITALS.",
20
+ "THIS pERSON hAS tHEIR cAPS-lOCK oN."
21
+ ].each do |text|
22
+ describe text.inspect do
23
+ it "should be invalid" do
24
+ post.comment = text
25
+ post.should_not be_valid
26
+ end
27
+ end
28
+ end
29
+
30
+ [
31
+ 'This is a very simple test comment.',
32
+ 'Supercalifragilisticexpialadocious: a super long word!',
33
+ 'How many words can I think of that don\'t contain an "e"?',
34
+ 'WHAT?! You mean to tell *ME* that SONY/ERICSSON *CAN\'T* make a phone?',
35
+ 'WHAT?! YOU MEAN TO TELL ME that SONY/ERICSSON *CAN\'T* make a phone?',
36
+ 'ROFL! Who do I have to shag to get a comment around here?',
37
+ 'I\'m a Klingon. It\'s kind of my thing to be angry for almost no good reason. Yesterday I tore the door off my fridge cause I was out of margarine. These things happen.'
38
+ ].each do |text|
39
+ describe text.inspect do
40
+ it "should be valid" do
41
+ post.comment = text
42
+ post.should be_valid
43
+ end
44
+ end
45
+ end
46
+
47
+ end
@@ -8,7 +8,7 @@ describe Validatious::Validators::UrlValidator do
8
8
  Post.validates_url_format_of(:url)
9
9
  end
10
10
 
11
- let(:post) { post = Post.new(:title => "The title", :url => "invalid URL") }
11
+ let(:post) { Post.new(:title => "The title", :url => "invalid URL") }
12
12
 
13
13
  [
14
14
  'http://example.com',
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: validatious
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Joel Moss
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-29 00:00:00 +01:00
13
+ date: 2011-07-06 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -55,6 +55,7 @@ files:
55
55
  - lib/validatious/helper_methods.rb
56
56
  - lib/validatious/locale/en.yml
57
57
  - lib/validatious/validators/email_validator.rb
58
+ - lib/validatious/validators/text_content_validator.rb
58
59
  - lib/validatious/validators/url_validator.rb
59
60
  - lib/validatious/version.rb
60
61
  - spec/spec_helper.rb
@@ -63,6 +64,7 @@ files:
63
64
  - spec/support/models/topic.rb
64
65
  - spec/validatious/helper_methods_spec.rb
65
66
  - spec/validatious/validators/email_validator_spec.rb
67
+ - spec/validatious/validators/text_content_validator_spec.rb
66
68
  - spec/validatious/validators/url_validator_spec.rb
67
69
  - validatious.gemspec
68
70
  has_rdoc: true
@@ -100,4 +102,5 @@ test_files:
100
102
  - spec/support/models/topic.rb
101
103
  - spec/validatious/helper_methods_spec.rb
102
104
  - spec/validatious/validators/email_validator_spec.rb
105
+ - spec/validatious/validators/text_content_validator_spec.rb
103
106
  - spec/validatious/validators/url_validator_spec.rb