iso4217-validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5bec44bd4f8ca4f62c52469e6a0c325ec59a7080
4
+ data.tar.gz: 5244ece45f311f193d3c532f2a4ae8d0f4599e56
5
+ SHA512:
6
+ metadata.gz: 7866593c40161948a24d456bda7f69092772b32673c6db6688230c51edc35c6acc4daad6a7f1b4b6180a62d64277382a88ee312cfe8632cf31827fa00434eca5
7
+ data.tar.gz: baccb5d028b4afbd55ac3e18aabd316661d2279d2bf262adad6f0eb322e2f016f916a05295deeea2f71f386caec2cb3d7f442df5dbde9167b2ab6b56549e8d4a
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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in iso4217-validator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Merlos
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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,64 @@
1
+ # Iso4217::Validator
2
+
3
+ Simple ruby on rails validator that checks if a value is a valid [ISO4217] (http://en.wikipedia.org/wiki/ISO_4217) code.
4
+
5
+ Examples of ISO4217 codes: EUR, USD, CHF, TWD, INR.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'iso4217-validator'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install iso4217-validator
20
+
21
+ ## Usage
22
+
23
+ Include the validator in your model attribute this way:
24
+
25
+ ```ruby
26
+ class TestModel < ActiveRecord::Base
27
+ include Iso4217::Validator
28
+ validates :currency_code, iso4217Code: true
29
+ end
30
+ ```
31
+
32
+ currency_code attribute shall be defined as an string.
33
+
34
+ If the currency_code is invalid the validator adds an :invalid message error, if you want to customize the error message you can use:
35
+
36
+ ```ruby
37
+ class TestModel < ActiveRecord::Base
38
+ include Iso4217::Validator
39
+ validates :currency_code, iso4217Code: {message: 'Oh! That's an invalid currency code.'}
40
+ end
41
+ ```
42
+
43
+ Please bear in mind that the validator is case sensitive. So 'EUR' is a valid code but 'eur' is not.
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( http://github.com/merlos/iso4217-validator/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature with tests'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
52
+
53
+ Please add unit tests when including new features.
54
+
55
+ ### Maintaining the list updated
56
+ Source of the valid code list used: http://www.currency-iso.org/en/home/tables/table-a1.html
57
+ The last version included in the gem: August 15, 2014.
58
+
59
+ The gem source includes an script (bin/extractor.rb) that can extract the codes from the excel file and convert it into the
60
+ ruby array which is used by the validator. You can find more instructions within the extractor.rb file.
61
+
62
+ ## License
63
+
64
+ Copyright (c) 2014 Juan M. Merlos. Distributed 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
+ # ----
data/bin/extractor.rb ADDED
@@ -0,0 +1,86 @@
1
+ #!/bin/ruby
2
+
3
+ # Simple code list extractor
4
+ #
5
+ # This file gets the list of codes from a CSV file
6
+ #
7
+ # NOTE
8
+ # This tool is intended to be used by the gem administrators
9
+ #
10
+ # It expects a csv file in which there is a column where the
11
+ # currency codes of ISO4417 are defined.
12
+ # You can find an example here:
13
+ # http://www.currency-iso.org/en/home/tables/table-a1.html
14
+ #
15
+ # The source file previously mentioned is an XLS (Excel), it has to be
16
+ # converted into CSV in order to be processed by this script
17
+ #
18
+ # In the ISO4217 table-a1 there are several currencies that are repeated.
19
+ # basically this script just removes those duplicated.
20
+ #
21
+ # How to use the script:
22
+ #
23
+ # 1) Change the config vars below
24
+ # 2) Run the script:
25
+ # $ ruby extractor.rb
26
+ #
27
+ # 3) copy @output_file (def: tmp/valid_codes.rb) into lib/iso4217/validator/
28
+ #
29
+ # Use tmp/ directory for the files, as it is already ignored on the git repository
30
+ #
31
+ # if you get this error:
32
+ # invalid byte sequence in UTF-8 (ArgumentError)
33
+ # You need to convert the file to UTF-8
34
+ # $ iconv -f ISO-8859-1 -t utf-8 table_a1.csv > table_a1_utf8.csv
35
+
36
+ #################################
37
+ # Config
38
+ ###################################
39
+ #
40
+ # path to the csv file with the codes
41
+ @csv_file_path = '../tmp/table_a1.csv'
42
+ #
43
+ # output file path. Do not change the name "valid_codes.rb"
44
+ @output_file = '../tmp/valid_codes.rb'
45
+ #
46
+ # first row with a code
47
+ @first_row = 4 # starts in 1
48
+ #
49
+ # column in which the codes are
50
+ @column = 2; # starts in 0, so this is the actual 4th row
51
+
52
+ @separator = ';'
53
+ ############################################
54
+
55
+
56
+
57
+ require 'csv'
58
+ code_list = []
59
+ current_row_num = 0
60
+ CSV.foreach(@csv_file_path, {col_sep: @separator}) do |row|
61
+ current_row_num+= 1
62
+ if (current_row_num > @first_row)
63
+ if code_list.include? (row[@column])
64
+ next
65
+ end
66
+ code_list.push(row[@column]) unless row[@column].nil?
67
+ end
68
+
69
+ end
70
+
71
+ # now create the output ruby file
72
+ f = File.new(@output_file, "w+")
73
+ f.puts "module Iso4217"
74
+ f.puts " module Validator"
75
+ f.puts " Iso4217Codes = ["
76
+ code_list.each_with_index do |code, index|
77
+ line = " \"#{code}\""
78
+ line = line + ',' if code != code_list.last
79
+ f.puts line
80
+ end
81
+ f.puts " ]"
82
+ f.puts " end"
83
+ f.puts "end"
84
+ f.close
85
+
86
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iso4217/validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "iso4217-validator"
8
+ spec.version = Iso4217::Validator::VERSION
9
+ spec.authors = ["merlos"]
10
+ spec.email = ["jmmerlos@merlos.org"]
11
+ spec.summary = %q{rails validator to check that a currency code is defined on the ISO4217}
12
+ spec.description = %q{Ruby on Rails validator that verifies that a currency code belongs to the list defined in the ISO4217}
13
+ spec.homepage = "http://github.com/merlos/iso4217-validator"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", "~> 0"
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake", "~> 0"
24
+ spec.add_development_dependency "minitest", "~> 0"
25
+
26
+ end
@@ -0,0 +1,12 @@
1
+ require "iso4217/validator/version"
2
+ require 'iso4217/validator/valid_codes'
3
+ module Iso4217
4
+ module Validator
5
+
6
+ class Iso4217CodeValidator < ActiveModel::EachValidator
7
+ def validate_each(record, attribute, value)
8
+ record.errors.add(attribute, options[:message] || :invalid) unless Iso4217Codes.include?(value)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,185 @@
1
+ module Iso4217
2
+ module Validator
3
+ Iso4217Codes = [
4
+ "AFN",
5
+ "EUR",
6
+ "ALL",
7
+ "DZD",
8
+ "USD",
9
+ "AOA",
10
+ "XCD",
11
+ "ARS",
12
+ "AMD",
13
+ "AWG",
14
+ "AUD",
15
+ "AZN",
16
+ "BSD",
17
+ "BHD",
18
+ "BDT",
19
+ "BBD",
20
+ "BYR",
21
+ "BZD",
22
+ "XOF",
23
+ "BMD",
24
+ "BTN",
25
+ "INR",
26
+ "BOB",
27
+ "BOV",
28
+ "BAM",
29
+ "BWP",
30
+ "NOK",
31
+ "BRL",
32
+ "BND",
33
+ "BGN",
34
+ "BIF",
35
+ "KHR",
36
+ "XAF",
37
+ "CAD",
38
+ "CVE",
39
+ "KYD",
40
+ "CLF",
41
+ "CLP",
42
+ "CNY",
43
+ "COP",
44
+ "COU",
45
+ "KMF",
46
+ "CDF",
47
+ "NZD",
48
+ "CRC",
49
+ "HRK",
50
+ "CUC",
51
+ "CUP",
52
+ "ANG",
53
+ "CZK",
54
+ "DKK",
55
+ "DJF",
56
+ "DOP",
57
+ "EGP",
58
+ "SVC",
59
+ "ERN",
60
+ "ETB",
61
+ "FKP",
62
+ "FJD",
63
+ "XPF",
64
+ "GMD",
65
+ "GEL",
66
+ "GHS",
67
+ "GIP",
68
+ "GTQ",
69
+ "GBP",
70
+ "GNF",
71
+ "GYD",
72
+ "HTG",
73
+ "HNL",
74
+ "HKD",
75
+ "HUF",
76
+ "ISK",
77
+ "IDR",
78
+ "XDR",
79
+ "IRR",
80
+ "IQD",
81
+ "ILS",
82
+ "JMD",
83
+ "JPY",
84
+ "JOD",
85
+ "KZT",
86
+ "KES",
87
+ "KPW",
88
+ "KRW",
89
+ "KWD",
90
+ "KGS",
91
+ "LAK",
92
+ "LBP",
93
+ "LSL",
94
+ "ZAR",
95
+ "LRD",
96
+ "LYD",
97
+ "CHF",
98
+ "LTL",
99
+ "MOP",
100
+ "MKD",
101
+ "MGA",
102
+ "MWK",
103
+ "MYR",
104
+ "MVR",
105
+ "MRO",
106
+ "MUR",
107
+ "XUA",
108
+ "MXN",
109
+ "MXV",
110
+ "MDL",
111
+ "MNT",
112
+ "MAD",
113
+ "MZN",
114
+ "MMK",
115
+ "NAD",
116
+ "NPR",
117
+ "NIO",
118
+ "NGN",
119
+ "OMR",
120
+ "PKR",
121
+ "PAB",
122
+ "PGK",
123
+ "PYG",
124
+ "PEN",
125
+ "PHP",
126
+ "PLN",
127
+ "QAR",
128
+ "RON",
129
+ "RUB",
130
+ "RWF",
131
+ "SHP",
132
+ "WST",
133
+ "STD",
134
+ "SAR",
135
+ "RSD",
136
+ "SCR",
137
+ "SLL",
138
+ "SGD",
139
+ "XSU",
140
+ "SBD",
141
+ "SOS",
142
+ "SSP",
143
+ "LKR",
144
+ "SDG",
145
+ "SRD",
146
+ "SZL",
147
+ "SEK",
148
+ "CHE",
149
+ "CHW",
150
+ "SYP",
151
+ "TWD",
152
+ "TJS",
153
+ "TZS",
154
+ "THB",
155
+ "TOP",
156
+ "TTD",
157
+ "TND",
158
+ "TRY",
159
+ "TMT",
160
+ "UGX",
161
+ "UAH",
162
+ "AED",
163
+ "USN",
164
+ "UYI",
165
+ "UYU",
166
+ "UZS",
167
+ "VUV",
168
+ "VEF",
169
+ "VND",
170
+ "YER",
171
+ "ZMW",
172
+ "ZWL",
173
+ "XBA",
174
+ "XBB",
175
+ "XBC",
176
+ "XBD",
177
+ "XTS",
178
+ "XXX",
179
+ "XAU",
180
+ "XPD",
181
+ "XPT",
182
+ "XAG"
183
+ ]
184
+ end
185
+ end
@@ -0,0 +1,5 @@
1
+ module Iso4217
2
+ module Validator
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,77 @@
1
+ require 'rubygems'
2
+ require "minitest/autorun"
3
+
4
+ require 'active_record'
5
+ require 'active_model'
6
+ require 'iso4217/validator'
7
+
8
+ module Iso4217
9
+ module Validator
10
+
11
+ class SampleModel
12
+ include ActiveModel::Validations
13
+ include Iso4217::Validator
14
+ attr_accessor :code, :code_or_nil
15
+
16
+ validates :code, iso4217Code: true
17
+
18
+ def initialize(attrs = {})
19
+ I18n.enforce_available_locales = false # added to avoid a deprecation message
20
+ attrs.each_pair { |k,v| send("#{k}=", v) }
21
+ end
22
+ end
23
+
24
+ class SampleModelWithNil
25
+ include ActiveModel::Validations
26
+ include Iso4217::Validator
27
+ attr_accessor :code
28
+
29
+ validates :code, iso4217Code: { message: "custom message"}, allow_nil: true
30
+
31
+ def initialize(attrs = {})
32
+ I18n.enforce_available_locales = false # added to avoid a deprecation message
33
+ attrs.each_pair { |k,v| send("#{k}=", v) }
34
+ end
35
+ end
36
+
37
+ class Iso4217ValidatorTests < Minitest::Test
38
+ def test_valid_code
39
+ m = SampleModel.new(code: 'EUR')
40
+ assert m.valid?
41
+ end
42
+
43
+ def test_code_in_lowercase
44
+ m = SampleModel.new(code: 'eur')
45
+ assert m.invalid?
46
+
47
+ end
48
+
49
+ def test_invalid_code
50
+ m = SampleModel.new(code: 123)
51
+ assert m.invalid?
52
+ end
53
+
54
+ def test_nil_code
55
+ m = SampleModel.new(code: nil)
56
+ assert m.invalid?
57
+ end
58
+
59
+ def test_not_assigned
60
+ m = SampleModel.new
61
+ assert m.invalid?
62
+ end
63
+
64
+ def test_not_assigned_in_sample_code_with_nil
65
+ m = SampleModelWithNil.new
66
+ assert m.valid?
67
+ end
68
+
69
+ def test_custom_message
70
+ m = SampleModelWithNil.new(code: "invalid")
71
+ m.valid?
72
+ assert_equal 'custom message', m.errors[:code][0]
73
+ end
74
+ end
75
+
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iso4217-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-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Ruby on Rails validator that verifies that a currency code belongs to
70
+ the list defined in the ISO4217
71
+ email:
72
+ - jmmerlos@merlos.org
73
+ executables:
74
+ - extractor.rb
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/extractor.rb
85
+ - iso4217-validator.gemspec
86
+ - lib/iso4217/validator.rb
87
+ - lib/iso4217/validator/valid_codes.rb
88
+ - lib/iso4217/validator/version.rb
89
+ - test/test_iso4217-validator.rb
90
+ homepage: http://github.com/merlos/iso4217-validator
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: rails validator to check that a currency code is defined on the ISO4217
114
+ test_files:
115
+ - test/test_iso4217-validator.rb
116
+ has_rdoc: