seamusabshere-decency_validation 1.0

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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
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 ADDED
@@ -0,0 +1,31 @@
1
+ DecencyValidation
2
+ =================
3
+
4
+ Uses George Carlin's list of "seven dirty words" to "check for decency" (ahem)
5
+ http://en.wikipedia.org/wiki/Seven_dirty_words
6
+
7
+ It gives you String#is_decent? and a new ActiveRecord validation, validates_decency_of.
8
+
9
+ Example
10
+ =======
11
+
12
+ Using it from String...
13
+
14
+ >> "hello world".is_decent?
15
+ => true
16
+
17
+ >> "-s-h-i-t-t-y-".is_decent?
18
+ => false
19
+
20
+ >> "the town of Scunthorpe".is_decent?
21
+ => false
22
+
23
+ (sorry to the residents of http://en.wikipedia.org/wiki/Scunthorpe)
24
+
25
+ Using it in ActiveRecord...
26
+
27
+ class Message < ActiveRecord::Base
28
+ validates_decency_of :title, :description
29
+ end
30
+
31
+ Copyright (c) 2008 Seamus Abshere, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the decency_validation plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the decency_validation plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'DecencyValidation'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "decency_validation"
3
+ s.version = "1.0"
4
+ s.date = "2008-12-29"
5
+ s.summary = "Rails plugin that uses George Carlin's list of seven dirty words (aka swear words, aka cuss words) to check for decency on ActiveRecord model attributes."
6
+ s.email = "seamus@abshere.net"
7
+ s.homepage = "http://github.com/seamusabshere/decency_validation"
8
+ s.description = "Rails plugin that uses George Carlin's list of seven dirty words (aka swear words, aka cuss words) to check for decency on ActiveRecord model attributes."
9
+ s.has_rdoc = false
10
+ s.authors = "Seamus Abshere"
11
+ s.files = [
12
+ "decency_validation.gemspec",
13
+ "lib/decency_validation.rb",
14
+ "lib/is_decent.rb",
15
+ "lib/validates_decency_of.rb",
16
+ "MIT-LICENSE",
17
+ "Rakefile",
18
+ "README",
19
+ "tasks/decency_validation_tasks.rake",
20
+ ]
21
+ s.test_files = [
22
+ "test/decency_validation_test.rb",
23
+ "test/test_helper.rb"
24
+ ]
25
+ end
@@ -0,0 +1,2 @@
1
+ require 'is_decent'
2
+ require 'validates_decency_of'
data/lib/is_decent.rb ADDED
@@ -0,0 +1,7 @@
1
+ class String
2
+ INDECENT_WORDS = %w(shit piss fuck cunt cocksucker motherfucker tits)
3
+ def is_decent?
4
+ essence = self.downcase.gsub(/[^a-zA-Z]/, '')
5
+ INDECENT_WORDS.detect { |c| essence.include? c }.nil?
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ module ValidatesDecencyOf
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ # Uses George Carlin's list of "seven dirty words" to "check for decency" (ahem)
8
+ # http://en.wikipedia.org/wiki/Seven_dirty_words
9
+ # Future versions will support adding/removing from this list
10
+ # Ex:
11
+ #
12
+ # class Message < ActiveRecord::Base
13
+ # validates_decency_of :title, :description
14
+ # end
15
+ #
16
+ # Configuration Options
17
+ #
18
+ # [<tt>:message</tt>] A custom error message (default is: "is invalid")
19
+ #
20
+ def validates_decency_of(*attribute_names)
21
+ options = { :message => 'is invalid' }
22
+ options.merge!(attribute_names.pop) if attribute_names.last.kind_of?(Hash)
23
+ options.merge! :on => :save
24
+ validates_each(attribute_names, options) do |record, attribute_name, value|
25
+ record.errors.add attribute_name, options[:message] unless value.to_s.is_decent?
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ ActiveRecord::Base.class_eval { include ValidatesDecencyOf }
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :decency_validation do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ describe "IsDecent" do
4
+ before do
5
+ @decent = 'Hello world'
6
+ @indecent = 'Hello f*u*c*k*i*n*g world'
7
+ end
8
+
9
+ it "should not die on empty strings" do
10
+ assert_nothing_raised do
11
+ ''.is_decent?
12
+ end
13
+ end
14
+
15
+ it "should recognize decent strings" do
16
+ assert @decent.is_decent?
17
+ end
18
+
19
+ it "should recognize indecent strings" do
20
+ assert !@indecent.is_decent?
21
+ end
22
+ end
23
+
24
+ describe "ValidatesDecencyOf" do
25
+ before do
26
+ DecencyValidationTest::Initializer.setup_database
27
+ @decent = Message.create :title => 'Hello world', :description => 'I am clean'
28
+ @indecent = Message.create :title => 'Hello f*u*c*k*i*n*g world', :description => 'I am ~-S-h-I-t-T-y-~'
29
+ end
30
+ after do
31
+ DecencyValidationTest::Initializer.teardown_database
32
+ end
33
+
34
+ it "should not die on empty strings" do
35
+ @decent.description = nil
36
+ assert @decent.valid?
37
+ end
38
+
39
+ it "should allow decent strings" do
40
+ assert @decent.valid?
41
+ end
42
+
43
+ it "should disallow indecent strings" do
44
+ assert !@indecent.valid?
45
+ assert_not_nil @indecent.errors.on(:title)
46
+ assert_not_nil @indecent.errors.on(:description)
47
+ end
48
+ end
@@ -0,0 +1,64 @@
1
+ module DecencyValidationTest
2
+ module Initializer
3
+ VENDOR_RAILS = File.expand_path('../../../../rails', __FILE__)
4
+ OTHER_RAILS = File.expand_path('../../../rails', __FILE__)
5
+ PLUGIN_ROOT = File.expand_path('../../', __FILE__)
6
+
7
+ def self.rails_directory
8
+ if File.exist?(VENDOR_RAILS)
9
+ VENDOR_RAILS
10
+ elsif File.exist?(OTHER_RAILS)
11
+ OTHER_RAILS
12
+ end
13
+ end
14
+
15
+ def self.load_dependencies
16
+ if rails_directory
17
+ $:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
18
+ $:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
19
+ else
20
+ require 'rubygems' rescue LoadError
21
+ end
22
+
23
+ require 'activesupport'
24
+ require 'activerecord'
25
+ require 'active_support/testing/core_ext/test/unit/assertions'
26
+
27
+ require 'rubygems' rescue LoadError
28
+
29
+ require 'test/spec'
30
+ require 'decency_validation'
31
+ end
32
+
33
+ def self.configure_database
34
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
35
+ ActiveRecord::Migration.verbose = false
36
+ end
37
+
38
+ def self.setup_database
39
+ ActiveRecord::Schema.define do
40
+ create_table :messages do |t|
41
+ t.string :title
42
+ t.string :description
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.teardown_database
48
+ ActiveRecord::Base.connection.tables.each do |table|
49
+ ActiveRecord::Base.connection.drop_table(table)
50
+ end
51
+ end
52
+
53
+ def self.start
54
+ load_dependencies
55
+ configure_database
56
+ end
57
+ end
58
+ end
59
+
60
+ DecencyValidationTest::Initializer.start
61
+
62
+ class Message < ActiveRecord::Base
63
+ validates_decency_of :title, :description
64
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seamusabshere-decency_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Seamus Abshere
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-29 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rails plugin that uses George Carlin's list of seven dirty words (aka swear words, aka cuss words) to check for decency on ActiveRecord model attributes.
17
+ email: seamus@abshere.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - decency_validation.gemspec
26
+ - lib/decency_validation.rb
27
+ - lib/is_decent.rb
28
+ - lib/validates_decency_of.rb
29
+ - MIT-LICENSE
30
+ - Rakefile
31
+ - README
32
+ - tasks/decency_validation_tasks.rake
33
+ has_rdoc: false
34
+ homepage: http://github.com/seamusabshere/decency_validation
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Rails plugin that uses George Carlin's list of seven dirty words (aka swear words, aka cuss words) to check for decency on ActiveRecord model attributes.
59
+ test_files:
60
+ - test/decency_validation_test.rb
61
+ - test/test_helper.rb