mlj-unicode_normalization_validation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Marius L. Jøhndal
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.
@@ -0,0 +1,7 @@
1
+ README.rdoc
2
+ Rakefile
3
+ MIT-LICENSE
4
+ test/unicode_normalization_validation_test.rb
5
+ lib/unicode_normalization_validation.rb
6
+ init.rb
7
+ Manifest
@@ -0,0 +1,33 @@
1
+ = unicode_normalization_validation
2
+
3
+ A Unicode Normalization Form validation plugin for Rails.
4
+
5
+ == Installation
6
+
7
+ git clone --depth=1 git://github.com/mlj/unicode_normalization_validation.git vendor/plugins/unicode_normalization_validation
8
+
9
+ == Usage
10
+
11
+ class Book < ActiveRecord::Base
12
+ validates_unicode_normalization_of :author
13
+ validates_unicode_normalization_of :editor, :form => :c
14
+ validates_unicode_normalization_of :title, :message => 'Title is not on Unicode Normalization Form KC'
15
+ end
16
+
17
+ == Bugs/limitations
18
+
19
+ This only works if you set
20
+
21
+ $KCODE = 'u'
22
+
23
+ in your environment (at least as per Rails 2.0 and Ruby 1.8).
24
+
25
+ == License
26
+
27
+ MIT
28
+
29
+ == Development
30
+
31
+ The project is hosted on Github on http://github.com/mlj/unicode_normalization_validation.
32
+
33
+ Copyright (c) 2008 Marius L. Jøhndal, released under the MIT license.
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'echoe'
6
+
7
+ Echoe.new('unicode_normalization_validation', '0.1.0') do |p|
8
+ p.summary = "A Unicode Normalization Form validation plugin for Rails"
9
+ p.author = 'Marius L. Jøhndal'
10
+ p.email = "mariuslj (at) ifi [dot] uio (dot) no"
11
+ p.url = "http://github.com/mlj/unicode_normalization_validation"
12
+ p.ignore_pattern = ["*.gemspec"]
13
+ p.rdoc_pattern = ["README.rdoc", "lib/*.rb"]
14
+ end
15
+
16
+ rescue LoadError => boom
17
+ puts "You are missing a dependency required for meta-operations on this gem."
18
+ puts "#{boom.to_s.capitalize}."
19
+ end
20
+
21
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ $:.unshift "#{File.dirname(__FILE__)}/lib"
2
+ require 'unicode_normalization_validation'
@@ -0,0 +1,32 @@
1
+ module ActiveRecord
2
+ module Validations
3
+ module ClassMethods
4
+ # Validates that the value of the specified attribute is on a particular Unicode Normalization
5
+ # Form.
6
+ #
7
+ # class Book < ActiveRecord::Base
8
+ # validates_unicode_normalization_of :author
9
+ # validates_unicode_normalization_of :editor, :form => :c
10
+ # validates_unicode_normalization_of :title, :message => 'Title is not on Unicode Normalization Form KC'
11
+ # end
12
+ #
13
+ # Configuration options:
14
+ # * <tt>:form</tt> - Specifies the normalization form to use. Valid normalization forms are <tt>:c</tt>, <tt>:d</tt>,
15
+ # <tt>:kc</tt>, and </tt>:kd</tt>.
16
+ # * <tt>:message</tt> - Specifies a custom error message (default is: "is not on Unicode Normalization form %s").
17
+ def validates_unicode_normalization_of(*attr_names)
18
+ configuration = {
19
+ :form => :kc,
20
+ :message => 'is not on Unicode Normalization form %s',
21
+ }
22
+ configuration.update(attr_names.extract_options!)
23
+
24
+ raise(ArgumentError, "Invalid normalization form") unless [:c, :kc, :d, :kd].include?(configuration[:form])
25
+
26
+ validates_each(attr_names, configuration) do |record, attr_name, value|
27
+ record.errors.add(attr_name, configuration[:message] % configuration[:form].to_s.upcase) unless value.nil? or value.mb_chars == value.mb_chars.normalize(configuration[:form])
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,62 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ gem 'activerecord', '>= 1.15.4.7794'
5
+ require 'active_record'
6
+ require 'active_support'
7
+
8
+ $KCODE = 'u'
9
+
10
+ require "#{File.dirname(__FILE__)}/../init"
11
+
12
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
13
+
14
+ def setup_db
15
+ ActiveRecord::Schema.define(:version => 1) do
16
+ create_table :books, :force => true do |t|
17
+ t.string :f1
18
+ t.string :f2
19
+ t.string :f3
20
+ end
21
+ end
22
+ end
23
+
24
+ def teardown_db
25
+ ActiveRecord::Base.connection.tables.each do |table|
26
+ ActiveRecord::Base.connection.drop_table(table)
27
+ end
28
+ end
29
+
30
+ class ValidatedBooks < ActiveRecord::Base
31
+ def self.table_name() "books" end
32
+
33
+ validates_unicode_normalization_of :f1
34
+ validates_unicode_normalization_of :f2, :form => :c
35
+ validates_unicode_normalization_of :f3, :form => :c, :message => 'Bad title'
36
+ end
37
+
38
+ class UnicodeNormalizationValidationTest < Test::Unit::TestCase
39
+ def setup
40
+ setup_db
41
+ @valid = ValidatedBooks.create! :f1 => 'ἐγκαλέω', :f2 => 'ἐγκαλέω', :f3 => 'ἐγκαλέω' # on NFC
42
+ @valid_nil = ValidatedBooks.create! :f1 => nil, :f2 => 'ἐγκαλέω', :f3 => 'ἐγκαλέω' # on NFC
43
+ end
44
+
45
+ def teardown
46
+ teardown_db
47
+ end
48
+
49
+ def test_nfc
50
+ assert_equal true, @valid.valid?
51
+ end
52
+
53
+ def test_nil
54
+ assert_equal true, @valid_nil.valid?
55
+ end
56
+
57
+ def test_non_nfc
58
+ assert_raise ActiveRecord::RecordInvalid do
59
+ @invalid = ValidatedBooks.create! :f1 => 'ἐγκαλέω', :f2 => 'ἐγκαλέω', :f3 => 'ἐγκαλέω' # not on NFC
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{unicode_normalization_validation}
3
+ s.version = "0.1.0"
4
+ s.authors = ["Marius L. J\303\270hndal"]
5
+ s.date = %q{2008-11-27}
6
+ s.description = %q{A Unicode Normalization Form validation plugin for Rails}
7
+ s.email = %q{mariuslj (at) ifi [dot] uio (dot) no}
8
+ s.extra_rdoc_files = ["README.rdoc", "lib/unicode_normalization_validation.rb"]
9
+ s.files = ["README.rdoc", "Rakefile", "MIT-LICENSE", "test/unicode_normalization_validation_test.rb", "lib/unicode_normalization_validation.rb", "init.rb", "Manifest", "unicode_normalization_validation.gemspec"]
10
+ s.has_rdoc = true
11
+ s.homepage = %q{http://github.com/mlj/unicode_normalization_validation}
12
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Unicode_normalization_validation", "--main", "README.rdoc"]
13
+ s.require_paths = ["lib"]
14
+ s.summary = %q{A Unicode Normalization Form validation plugin for Rails}
15
+ s.test_files = ["test/unicode_normalization_validation_test.rb"]
16
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mlj-unicode_normalization_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Marius L. J\xC3\xB8hndal"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-27 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Unicode Normalization Form validation plugin for Rails
17
+ email: mariuslj (at) ifi [dot] uio (dot) no
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/unicode_normalization_validation.rb
25
+ files:
26
+ - README.rdoc
27
+ - Rakefile
28
+ - MIT-LICENSE
29
+ - test/unicode_normalization_validation_test.rb
30
+ - lib/unicode_normalization_validation.rb
31
+ - init.rb
32
+ - Manifest
33
+ - unicode_normalization_validation.gemspec
34
+ has_rdoc: true
35
+ homepage: http://github.com/mlj/unicode_normalization_validation
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --line-numbers
39
+ - --inline-source
40
+ - --title
41
+ - Unicode_normalization_validation
42
+ - --main
43
+ - README.rdoc
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: A Unicode Normalization Form validation plugin for Rails
65
+ test_files:
66
+ - test/unicode_normalization_validation_test.rb