ico-validator 0.1.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50743c6d700c5303c17049cd4273aa8b585b3593
4
- data.tar.gz: 3fa527cb8662e3f426c1399dee62e778a15e6641
3
+ metadata.gz: 6cd1b0fb28c0955404b04e7508c5b8cfce2c0b75
4
+ data.tar.gz: c4ae1c9962c5221705327bb67af973f4aeb60cc6
5
5
  SHA512:
6
- metadata.gz: e2422b3869edd000b82734f01c955f1143962b4e448825327f3962e6f30b49fd0a72f1e51650ed2749d3da56dc99132c5044d8d63584cd701e4a9c43d01e3bf3
7
- data.tar.gz: 833e9d51572e0c34fc65ea3bfadb3f7f015709122b76919b7b296f6c246e982a1a2c8d382003c662f6885da6acf7ca35bed6fb05425654d48f540928ba9325be
6
+ metadata.gz: c3c3f4cdd367dbe7e200bdb1e1992fa5914247dcf23b9b9e0bd82a508b8e16083ac368f3e717235fc7479a5b810c4cd9750ebbfb23e17c7c1507749830d3a17c
7
+ data.tar.gz: 26f387d947bcd1d3aeb67cb861ba217623ad85ba25444b7ee62fd5b44ec18151ee70cc63de79294fd68ef7e52cf1e221daca02c9d329754787c5ac252cd62083
@@ -3,4 +3,6 @@ rvm:
3
3
  - 2.1.1
4
4
  - 2.0.0
5
5
  - 1.9.3
6
- script: bundle exec rspec
6
+ script:
7
+ - bundle exec rspec
8
+ - bundle exec rake jasmine:ci
@@ -1,3 +1,7 @@
1
+ ## v0.2.0
2
+
3
+ * Added JS validator
4
+
1
5
  ## v0.1.1
2
6
 
3
7
  * Changed the dependency on activemodel so the gem can be used in Rails 4.1+
data/Gemfile CHANGED
@@ -1,4 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ group :test, :development do
4
+ gem 'rails'
5
+ gem 'jasmine'
6
+ end
7
+
3
8
  # Specify your gem's dependencies in ico_validator.gemspec
4
9
  gemspec
data/README.md CHANGED
@@ -14,6 +14,17 @@ validates :ico,
14
14
 
15
15
  ```
16
16
 
17
+ ### JS validation
18
+ ```js
19
+ application.js
20
+ //= require ico-validator
21
+ ```
22
+ ```ruby
23
+ <%= form_for(...) do |f| %>
24
+ <%= f.text_field :ico, class: 'ico-validate' %>
25
+ <% end %>
26
+ ```
27
+
17
28
  ### What is validated
18
29
 
19
30
  * ICO length - must be exactly 8 characters
data/Rakefile CHANGED
@@ -1,2 +1,3 @@
1
1
  require "bundler/gem_tasks"
2
-
2
+ require 'jasmine'
3
+ load 'jasmine/tasks/jasmine.rake'
@@ -0,0 +1,52 @@
1
+ var ico_validator = {
2
+ valid_ico: function(value) {
3
+ return value.length === 8 && value.match(/^\d+$/) && ico_validator.last_number_valid(value);
4
+ },
5
+ last_number_valid: function(value) {
6
+ return parseInt(value[7], 10) === ico_validator.calculate_valid_last_number(value);
7
+ },
8
+ calculate_valid_last_number: function(value) {
9
+ var sum = 0;
10
+ for( var i = 0; i <= 6; i++ ) {
11
+ sum += value[i] * (8 - i);
12
+ }
13
+ var mod = sum % 11;
14
+ if( mod === 0 || mod === 10 ) {
15
+ return 1;
16
+ }
17
+ if( mod === 1 ) {
18
+ return 0;
19
+ }
20
+ return 11 - mod;
21
+ }
22
+ };
23
+
24
+ jQuery( function($) {
25
+ var ico_fields = $('.ico-validate');
26
+
27
+ function validate(el) {
28
+ var value = $(el).val();
29
+ if( ico_validator.valid_ico(value) ) {
30
+ $(el).parent().removeClass('field_with_errors').removeClass('error');
31
+ } else {
32
+ $(el).parent().addClass('field_with_errors').addClass('error');
33
+ }
34
+ }
35
+
36
+ ico_fields.on('change', function() {
37
+ validate(this);
38
+ });
39
+
40
+ ico_fields.on('keyup', function() {
41
+ validate(this);
42
+ });
43
+
44
+ ico_fields.closest('form').on('submit', function() {
45
+ var wrong = $(this).find('.field_with_errors .ico-validate');
46
+ if( wrong.length <= 0 ) {
47
+ return;
48
+ }
49
+ wrong.focus();
50
+ return false;
51
+ });
52
+ });
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'ico-validator'
7
- spec.version = '0.1.1'
7
+ spec.version = '0.2.0'
8
8
  spec.authors = ['Premysl Donat']
9
9
  spec.email = ['donat@uol.cz']
10
10
  spec.summary = %q{Rails validator for validating format of czech identification number = IC}
@@ -3,3 +3,4 @@ require 'active_support/i18n'
3
3
  I18n.load_path << File.dirname(__FILE__) + '/locale/en.yml'
4
4
  I18n.load_path << File.dirname(__FILE__) + '/locale/cs.yml'
5
5
  require "ico-validator/ico_validator"
6
+ require "ico-validator/engine"
@@ -0,0 +1,4 @@
1
+ module IcoValidation
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ src_files:
2
+ - 'app/assets/javascripts/*.js'
3
+
4
+ spec_files:
5
+ - '**/*[sS]pec.js'
@@ -0,0 +1,15 @@
1
+ #Use this file to set/override Jasmine configuration options
2
+ #You can remove it if you don't need it.
3
+ #This file is loaded *after* jasmine.yml is interpreted.
4
+ #
5
+ #Example: using a different boot file.
6
+ #Jasmine.configure do |config|
7
+ # config.boot_dir = '/absolute/path/to/boot_dir'
8
+ # config.boot_files = lambda { ['/absolute/path/to/boot_dir/file.js'] }
9
+ #end
10
+ #
11
+ #Example: prevent PhantomJS auto install, uses PhantomJS already on your path.
12
+ #Jasmine.configure do |config|
13
+ # config.prevent_phantom_js_auto_install = true
14
+ #end
15
+ #
@@ -0,0 +1,18 @@
1
+ describe("ICO validator", function() {
2
+ var ico_list, i;
3
+ ico_list = ['61499609', '25275500', '29233011'];
4
+ for( i in ico_list ) if( ico_list.hasOwnProperty(i) ) {
5
+ ico_valid = ico_list[i];
6
+ it("ICO " + ico_valid + " is valid", function() {
7
+ expect(ico_validator.valid_ico(ico_valid)).toBeTruthy();
8
+ });
9
+ }
10
+ ico_list = [null, '', '1', 123, '1111111X', '00000000', '614996097', '123456789'];
11
+ for( i in ico_list ) if( ico_list.hasOwnProperty(i) ) {
12
+ ico_invalid = ico_list[i];
13
+ it("ICO is invalid with value " + ico_invalid, function() {
14
+ expect(ico_validator.valid_ico(ico_invalid)).toBeFalsy();
15
+ });
16
+ }
17
+ });
18
+
@@ -1,3 +1,4 @@
1
1
  require "codeclimate-test-reporter"
2
2
  CodeClimate::TestReporter.start
3
+ require 'rails/all'
3
4
  require 'ico-validator'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ico-validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Premysl Donat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-17 00:00:00.000000000 Z
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -95,12 +95,17 @@ files:
95
95
  - LICENSE
96
96
  - README.md
97
97
  - Rakefile
98
+ - app/assets/javascripts/ico-validator.js
98
99
  - ico-validator.gemspec
99
100
  - lib/ico-validator.rb
101
+ - lib/ico-validator/engine.rb
100
102
  - lib/ico-validator/ico_validator.rb
101
103
  - lib/locale/cs.yml
102
104
  - lib/locale/en.yml
103
105
  - spec/ico_validator_spec.rb
106
+ - spec/javascripts/support/jasmine.yml
107
+ - spec/javascripts/support/jasmine_helper.rb
108
+ - spec/javascripts/validator_spec.js
104
109
  - spec/spec_helper.rb
105
110
  homepage: http://www.uol.cz
106
111
  licenses:
@@ -122,11 +127,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
127
  version: '0'
123
128
  requirements: []
124
129
  rubyforge_project:
125
- rubygems_version: 2.2.2
130
+ rubygems_version: 2.4.5
126
131
  signing_key:
127
132
  specification_version: 4
128
133
  summary: Rails validator for validating format of czech identification number = IC
129
134
  test_files:
130
135
  - spec/ico_validator_spec.rb
136
+ - spec/javascripts/support/jasmine.yml
137
+ - spec/javascripts/support/jasmine_helper.rb
138
+ - spec/javascripts/validator_spec.js
131
139
  - spec/spec_helper.rb
132
- has_rdoc: