strip_control_chars 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +54 -0
- data/Rakefile +22 -0
- data/init.rb +2 -0
- data/lib/strip_control_chars.rb +35 -0
- data/rails/init.rb +2 -0
- data/test/strip_control_chars_test.rb +90 -0
- data/test/test_helper.rb +20 -0
- metadata +63 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Peter Yandell
|
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,54 @@
|
|
1
|
+
== Strip Control Chars
|
2
|
+
|
3
|
+
At the lower end of the ASCII range are a few control characters that are
|
4
|
+
generally innocent but useless. Often benign, they sometimes jump out and bite
|
5
|
+
you when you're trying to do something simple like generate some XML.
|
6
|
+
|
7
|
+
If you model doesn't need them, then remove them before they even make it into
|
8
|
+
your database.
|
9
|
+
|
10
|
+
=== Examples
|
11
|
+
|
12
|
+
class Product < ActiveRecord::Base
|
13
|
+
strip_control_chars!
|
14
|
+
end
|
15
|
+
|
16
|
+
class Product < ActiveRecord::Base
|
17
|
+
strip_control_chars!, :except => :title
|
18
|
+
end
|
19
|
+
|
20
|
+
class Product < ActiveRecord::Base
|
21
|
+
strip_control_chars!, :only => :description
|
22
|
+
end
|
23
|
+
|
24
|
+
=== Installation
|
25
|
+
|
26
|
+
Option 1. Load the plugin as a gem
|
27
|
+
|
28
|
+
gem install strip_control_chars
|
29
|
+
add "config.gem 'strip_control_chars'" to your environment.rb
|
30
|
+
|
31
|
+
Option 2. Use the standard Rails plugin install (assuming Rails >= 2.1).
|
32
|
+
|
33
|
+
./script/plugin install git://github.com/yob/strip_control_chars.git
|
34
|
+
|
35
|
+
=== Caveats
|
36
|
+
|
37
|
+
The translation is done with raw bytes. If you happen to be using anything other than
|
38
|
+
UTF-8 in your models, you're likely to munge data.
|
39
|
+
|
40
|
+
This also means it's almost certainly not ruby 1.9 compatible. Patches welcome.
|
41
|
+
|
42
|
+
=== Credits
|
43
|
+
|
44
|
+
This plugin is essentially a fork of the strip attributes plugin, released
|
45
|
+
under the MIT License by Ryan McGeary.
|
46
|
+
|
47
|
+
http://github.com/rmm5t/strip_attributes
|
48
|
+
|
49
|
+
=== License
|
50
|
+
|
51
|
+
Copyright (c) 2007-2008 Ryan McGeary released under the MIT license
|
52
|
+
Copyright (c) 2009 James Healy released under the MIT license
|
53
|
+
|
54
|
+
http://en.wikipedia.org/wiki/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 dumb quotes 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 dumb quotes plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Dumb Quotes'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module StripControlChars
|
2
|
+
# Strips ASCII control chars from attributes before they get saved
|
3
|
+
def strip_control_chars!(options = nil)
|
4
|
+
before_validation do |record|
|
5
|
+
attributes = StripControlChars.narrow(record.attributes, options)
|
6
|
+
attributes.each do |attr, value|
|
7
|
+
if value.respond_to?(:tr)
|
8
|
+
value = value.tr("\x00\x01\x02\x03\x04\x05\x06\x07\x08"," ")
|
9
|
+
value = value.tr("\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14"," ")
|
10
|
+
value = value.tr("\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D"," ")
|
11
|
+
value = value.tr("\x1E\x1F"," ")
|
12
|
+
record[attr] = value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Necessary because Rails has removed the narrowing of attributes using :only
|
19
|
+
# and :except on Base#attributes
|
20
|
+
def self.narrow(attributes, options)
|
21
|
+
if options.nil?
|
22
|
+
attributes
|
23
|
+
else
|
24
|
+
if except = options[:except]
|
25
|
+
except = Array(except).collect { |attribute| attribute.to_s }
|
26
|
+
attributes.except(*except)
|
27
|
+
elsif only = options[:only]
|
28
|
+
only = Array(only).collect { |attribute| attribute.to_s }
|
29
|
+
attributes.slice(*only)
|
30
|
+
else
|
31
|
+
raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/test_helper"
|
2
|
+
|
3
|
+
module MockAttributes
|
4
|
+
def self.included(base)
|
5
|
+
base.column :foo, :string
|
6
|
+
base.column :bar, :string
|
7
|
+
base.column :biz, :string
|
8
|
+
base.column :baz, :string
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ConvertAllMockRecord < ActiveRecord::Base
|
13
|
+
include MockAttributes
|
14
|
+
strip_control_chars!
|
15
|
+
end
|
16
|
+
|
17
|
+
class ConvertOnlyOneMockRecord < ActiveRecord::Base
|
18
|
+
include MockAttributes
|
19
|
+
strip_control_chars! :only => :foo
|
20
|
+
end
|
21
|
+
|
22
|
+
class ConvertOnlyThreeMockRecord < ActiveRecord::Base
|
23
|
+
include MockAttributes
|
24
|
+
strip_control_chars! :only => [:foo, :bar, :biz]
|
25
|
+
end
|
26
|
+
|
27
|
+
class ConvertExceptOneMockRecord < ActiveRecord::Base
|
28
|
+
include MockAttributes
|
29
|
+
strip_control_chars! :except => :foo
|
30
|
+
end
|
31
|
+
|
32
|
+
class ConvertExceptThreeMockRecord < ActiveRecord::Base
|
33
|
+
include MockAttributes
|
34
|
+
strip_control_chars! :except => [:foo, :bar, :biz]
|
35
|
+
end
|
36
|
+
|
37
|
+
class DumbQuotesTest < Test::Unit::TestCase
|
38
|
+
def setup
|
39
|
+
@init_params = { :foo => "foo\x00", :bar => "bar\x07", :biz => "biz\x09", :baz => "baz\x1E" }
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_should_exist
|
43
|
+
assert Object.const_defined?(:StripControlChars)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_fix_all_fields
|
47
|
+
record = ConvertAllMockRecord.new(@init_params)
|
48
|
+
record.valid?
|
49
|
+
assert_equal "foo ", record.foo
|
50
|
+
assert_equal "bar ", record.bar
|
51
|
+
assert_equal "biz\x09", record.biz
|
52
|
+
assert_equal "baz ", record.baz
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_should_convert_only_one_field
|
56
|
+
record = ConvertOnlyOneMockRecord.new(@init_params)
|
57
|
+
record.valid?
|
58
|
+
assert_equal "foo ", record.foo
|
59
|
+
assert_equal "bar\x07", record.bar
|
60
|
+
assert_equal "biz\x09", record.biz
|
61
|
+
assert_equal "baz\x1E", record.baz
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_should_convert_only_three_fields
|
65
|
+
record = ConvertOnlyThreeMockRecord.new(@init_params)
|
66
|
+
record.valid?
|
67
|
+
assert_equal "foo ", record.foo
|
68
|
+
assert_equal "bar ", record.bar
|
69
|
+
assert_equal "biz\x09", record.biz
|
70
|
+
assert_equal "baz\x1E", record.baz
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_should_convert_all_except_one_field
|
74
|
+
record = ConvertExceptOneMockRecord.new(@init_params)
|
75
|
+
record.valid?
|
76
|
+
assert_equal "foo\x00", record.foo
|
77
|
+
assert_equal "bar ", record.bar
|
78
|
+
assert_equal "biz\x09", record.biz
|
79
|
+
assert_equal "baz ", record.baz
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_should_convert_all_except_three_fields
|
83
|
+
record = ConvertExceptThreeMockRecord.new(@init_params)
|
84
|
+
record.valid?
|
85
|
+
assert_equal "foo\x00", record.foo
|
86
|
+
assert_equal "bar\x07", record.bar
|
87
|
+
assert_equal "biz\x09", record.biz
|
88
|
+
assert_equal "baz ", record.baz
|
89
|
+
end
|
90
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
PLUGIN_ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift "#{PLUGIN_ROOT}/lib"
|
8
|
+
require "#{PLUGIN_ROOT}/init"
|
9
|
+
|
10
|
+
class ActiveRecord::Base
|
11
|
+
alias_method :save, :valid?
|
12
|
+
def self.columns()
|
13
|
+
@columns ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
17
|
+
@columns ||= []
|
18
|
+
@columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strip_control_chars
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.0"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Healy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-06 00:00:00 +10:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: a small ActiveRecord plugin that removes ASCII control chars from attributes
|
17
|
+
email: james@yob.id.au
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- init.rb
|
26
|
+
- rails/init.rb
|
27
|
+
- lib/strip_control_chars.rb
|
28
|
+
- Rakefile
|
29
|
+
- MIT-LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/yob/strip_control_chars/tree/master
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --title
|
38
|
+
- Strip Control Chars
|
39
|
+
- --line-numbers
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project: yob-projects
|
57
|
+
rubygems_version: 1.3.2
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: a small ActiveRecord plugin that removes ASCII control chars from attributes
|
61
|
+
test_files:
|
62
|
+
- test/strip_control_chars_test.rb
|
63
|
+
- test/test_helper.rb
|