iso639-validator 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c0e2c2713cf11ac3f15e9fc64805b5b83211ec4f
4
+ data.tar.gz: 666f08151765a4a0f660e4a262a64bb714e9cd96
5
+ SHA512:
6
+ metadata.gz: 8d7f871c7767b3cd3f0302e352ab3b120fbe298834f48180e22502926344f6bf324275f2914960548cea4eadaaea36e2cb3317c7657602f6f68f16f1de1c625d
7
+ data.tar.gz: 885754b347ebce7782604185ab60e13c62f92d7f8b99e30d4082bc69a7a9873bbaffd3098ec161cc57556a154639ac07a9dd97fbee3e1f00a137dbd56b17e7c1
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,23 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Add dependencies required to use your gem here.
4
+ # Example:
5
+ # gem "activesupport", ">= 2.3.5"
6
+
7
+ gem "activerecord", ">=0"
8
+ gem 'iso-639', :git=>'https://github.com/merlos/iso-639.git'
9
+
10
+
11
+ # Add dependencies to develop your gem here.
12
+ # Include everything needed to run rake, tests, features, etc.
13
+ group :development do
14
+ #gem "minitest", ">=0"
15
+ #gem "shoulda", ">= 0"
16
+ #gem "rdoc", "~> 3.12"
17
+ #gem "bundler", "~> 1.0"
18
+ #gem "jeweler", "~> 2.0.1"
19
+ #gem "simplecov", ">= 0"
20
+ end
21
+
22
+ # Specify your gem's dependencies in iso639-validator.gemspec
23
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 merlos
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Iso639::Validator
2
+ A Rails gem that provides the ISO 639-2 and ISO 639-1 data validations.
3
+
4
+ The data comes from the LOC ISO 639-2 UTF-8 (http://www.loc.gov/standards/iso639-2/ascii_8bits.html) data set which provided by the gem 'iso-639' created by William Melody.
5
+
6
+ Please note that this gem is in an early stage of development and testing.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'iso639-validator'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install iso639-validator
21
+
22
+ ## Usage
23
+
24
+ In your model:
25
+
26
+ class TestModel < ActiveRecord::Base
27
+ include Iso639::Validator
28
+
29
+ # validate alfa-2 codes like 'es', 'en'
30
+ validates :alfa2_code, iso639Code: true, length: { is: 2 }
31
+
32
+ # validate alfa-3 codes like 'esp', 'eng' only if present.
33
+ validates :alfa3_code, iso639Code: true, length: { is: 3 }, if: "alfa3_code.present?"
34
+
35
+ # validate a French name like 'français'
36
+ validates :french, iso639FrenchName: true
37
+
38
+ # Validate an English name like 'English'
39
+ validates :french, iso639EnglishName: true
40
+ end
41
+
42
+
43
+ Please note that validations are *case sensitive*. For example, 'es' is a valid code, but 'ES' is not. 'français' is a valid French name, but 'Français' is not.
44
+
45
+ English and French names shall be exactly the same string as in the specification.
46
+
47
+ The validator launches an :invalid, message by default.
48
+
49
+
50
+ ## Contributing
51
+
52
+ Contributions and bug fixings are really appreciated
53
+
54
+ 1. Fork it ( http://github.com/merlos/iso639-validator/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
59
+
60
+ ## License
61
+
62
+ Copyright (c) 2014 Juan M. Merlos under MIT License
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # test ---
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
12
+ # ----
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iso639/validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "iso639-validator"
8
+ spec.version = Iso639::Validator::VERSION
9
+ spec.authors = ["merlos"]
10
+ spec.email = ["jmmerlos@merlos.org"]
11
+ spec.summary = %q{Validates if a value is an existing ISO-631
12
+ entry}
13
+ spec.description = %q{Validates that a value is either an alfa-2
14
+ code, alfa-3 code, French name or English name}
15
+ spec.homepage = "https://github.com/merlos/iso639-validator.git"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,41 @@
1
+ require "iso639/validator/version"
2
+
3
+ require 'active_model'
4
+ require "iso-639"
5
+
6
+ module Iso639
7
+ module Validator
8
+
9
+ class Iso639CodeValidator < ActiveModel::EachValidator
10
+ def validate_each(record, attribute, value)
11
+ if ISO_639.find_by_code(value).nil?
12
+ record.errors.add(attribute, options[:message] || :invalid)
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+ #checks that the value exists in the database and is unique
19
+ class Iso639EnglishNameValidator < ActiveModel::EachValidator
20
+ def validate_each(record, attribute, value)
21
+ if ISO_639.find_by_english_name(value).nil?
22
+ record.errors.add(attribute, options[:message] || :invalid)
23
+ end
24
+ return true
25
+ end
26
+ end
27
+
28
+
29
+ # Checks that the value exists in the database and is unique
30
+ class Iso639FrenchNameValidator < ActiveModel::EachValidator
31
+ def validate_each(record, attribute, value)
32
+ if ISO_639.find_by_french_name(value).nil?
33
+ record.errors.add(attribute, options[:message] || :invalid)
34
+ end
35
+ return true
36
+ end
37
+ end
38
+
39
+
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Iso639
2
+ module Validator
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,152 @@
1
+ require 'rubygems'
2
+ require "minitest/autorun"
3
+ require 'active_record'
4
+ require 'active_model'
5
+ require 'iso639/validator'
6
+
7
+ module Iso639
8
+ module Validator
9
+
10
+ #
11
+ # Helper class to test Code
12
+ #
13
+ class TestCode
14
+ include ActiveModel::Validations
15
+ include Iso639::Validator
16
+ attr_accessor :code
17
+
18
+ validates :code, iso639Code: true
19
+
20
+ def initialize(attrs = {})
21
+ attrs.each_pair { |k,v| send("#{k}=", v) }
22
+ end
23
+ end
24
+
25
+ # this class is to test a code with other conditions, for docummentation.
26
+ #
27
+ class TestCode3
28
+ include ActiveModel::Validations
29
+ include Iso639::Validator
30
+ attr_accessor :code
31
+
32
+ # validate alfa-3 codes like 'esp', 'eng' only if present.
33
+ validates :code, iso639Code: true, length: { is: 3 }, if: "code.present?"
34
+
35
+ def initialize(attrs = {})
36
+ attrs.each_pair { |k,v| send("#{k}=", v) }
37
+ end
38
+ end
39
+
40
+ #
41
+ # Helper class to test French Name
42
+ #
43
+ class TestFrench
44
+ include ActiveModel::Validations
45
+ include Iso639::Validator
46
+ attr_accessor :french
47
+
48
+ validates :french, iso639FrenchName: true
49
+
50
+ def initialize(attrs = {})
51
+ attrs.each_pair { |k,v| send("#{k}=", v) }
52
+ end
53
+ end
54
+
55
+ #
56
+ # Helper class to test English Name
57
+ #
58
+ class TestEnglish
59
+ include ActiveModel::Validations
60
+ include Iso639::Validator
61
+ attr_accessor :english
62
+
63
+ validates :english, iso639EnglishName: true
64
+
65
+ def initialize(attrs = {})
66
+ attrs.each_pair { |k,v| send("#{k}=", v) }
67
+ end
68
+ end
69
+
70
+ #
71
+ # Tests
72
+ #
73
+ class Iso639ValidatorTests < Minitest::Test #Test::Unit::TestCase
74
+
75
+ # Code Tests
76
+ def test_a_valid_code
77
+ t = TestCode.new(code:'es')
78
+ assert t.valid?
79
+ #puts t.valid?
80
+ #t.errors.each do |f| puts f end
81
+ t1 = TestCode.new(code:'en')
82
+ assert t1.valid?
83
+ #puts t.valid?
84
+ #t.errors.each do |f| puts f end
85
+ end
86
+
87
+ def test_an_empty_code
88
+ t = TestCode.new()
89
+ assert (not t.valid?)
90
+ end
91
+
92
+ def test_a_non_existing_code
93
+ t = TestCode.new(code:'zz')
94
+ assert (not t.valid?)
95
+ end
96
+
97
+ def test_a_name_with_find_by_code
98
+ t = TestCode.new(code:'espagnol');
99
+ assert (not t.valid?)
100
+ t.code = 'Spanish'
101
+ assert (not t.valid?)
102
+ end
103
+
104
+
105
+ def test_an_empty_code_conditional_presence
106
+ t = TestCode3.new()
107
+ assert t.valid? #it's ok is empty
108
+ end
109
+
110
+ def test_a_code2_requiring_length3_presence
111
+ t = TestCode3.new()
112
+ assert t.valid? #it's ok, is empty
113
+ t.code='es'
114
+ assert (not t.valid?)
115
+ end
116
+
117
+ def test_a_code2_requiring_length3_presence
118
+ t = TestCode3.new(code:'esp')
119
+ assert t.valid? #it's ok is empty
120
+ assert (not t.valid?)
121
+ end
122
+
123
+
124
+ # French tests
125
+ def test_valid_french_name
126
+ t = TestFrench.new(french:'français')
127
+ assert t.valid?
128
+ end
129
+
130
+ def test_invalid_french_name
131
+ t = TestFrench.new(french:'Español')
132
+ assert (not t.valid?)
133
+ end
134
+
135
+ # English
136
+ def test_valid_english_name
137
+ t = TestEnglish.new(english:'Russian')
138
+ assert t.valid?
139
+ end
140
+
141
+ def test_invalid_english_name
142
+ t = TestEnglish.new(english:'Español')
143
+ assert (not t.valid?)
144
+ end
145
+
146
+ end
147
+
148
+ end #validator module
149
+ end #iso
150
+
151
+
152
+
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iso639-validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - merlos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |-
42
+ Validates that a value is either an alfa-2
43
+ code, alfa-3 code, French name or English name
44
+ email:
45
+ - jmmerlos@merlos.org
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - iso639-validator.gemspec
56
+ - lib/iso639/validator.rb
57
+ - lib/iso639/validator/version.rb
58
+ - test/test_iso639-validator.rb
59
+ homepage: https://github.com/merlos/iso639-validator.git
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Validates if a value is an existing ISO-631 entry
83
+ test_files:
84
+ - test/test_iso639-validator.rb