dixon 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd4287564dded1221c414b3d82f74530a455bc32
4
- data.tar.gz: 354729bdd37756951627dda36db658f57e07c009
3
+ metadata.gz: a4aa8e4b5a9dc6293366fae03a4c2fb867859edf
4
+ data.tar.gz: e624ff4bbf6c43dbf331641e1786dd5816600524
5
5
  SHA512:
6
- metadata.gz: 4fac674805c6df5e41ffdf367c56568c4abe746ebde49ae664f0c132e52434f7f21c73f57f68eb5d9ee29f51008622c8695c0599ba574a71a995813b02a3c5af
7
- data.tar.gz: 766eabfa151d89352b72ba8cca0f0ee75953cc95e01021e0abfda90225353d977a54eb60b51148b162972610092b540d0a79e0a01ee1b00d8d131f97269bb37b
6
+ metadata.gz: ef071bc29ab77ef85174416f105e97bd7d7629f165bba2f33114605f3eaf5696a552d9019df25a132f20f7f167044b9eecb924591571702e8f35accca8ad1397
7
+ data.tar.gz: 628aa11550933d7e0df1efde482639811b7028df5259765441928397c116659a7625c805dfba9c6839771765632c36f900c64a1793cf2a1237127fab804d691e
data/.gitignore CHANGED
@@ -15,4 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- .pryrc
18
+ .pryrc
19
+ *.rb~
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Dixon
1
+ # Dixon [![Gem Version](https://badge.fury.io/rb/dixon.png)](http://badge.fury.io/rb/dixon)
2
2
 
3
3
  [Frank] Dixon checks your ID number.
4
4
 
@@ -41,10 +41,6 @@ Provides three instance methods: `checks`, `gender`, and `issued_by`.
41
41
  4. Push to the branch (`git push origin my-new-feature`)
42
42
  5. Create new Pull Request
43
43
 
44
- ## TODO
45
-
46
- * add tests.
47
-
48
44
  ## Author
49
45
 
50
46
  Juanito Fatas
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
@@ -25,6 +25,9 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.required_ruby_version = '>= 2.0.0'
27
27
 
28
+ # spec.add_dependency "add-your-runtime-dependency"
29
+
28
30
  spec.add_development_dependency "bundler", "~> 1.3"
29
31
  spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "minitest"
30
33
  end
@@ -1,3 +1,3 @@
1
1
  require 'dixon/version'
2
- require 'dixon/validators/helpers'
2
+ require 'dixon/validators/helper'
3
3
  require 'dixon/validators/taiwan'
@@ -5,6 +5,7 @@ module Dixon
5
5
  INVALID_MESSAGE = 'Invalid ID number.'
6
6
 
7
7
  LETTER_ISSUED_BY = {
8
+ ##
8
9
  # Active Letters
9
10
  'A' => 'Taipei City' , 'B' => 'Taichung City',
10
11
  'C' => 'Keelung City' , 'D' => 'Tainan City',
@@ -17,6 +18,7 @@ module Dixon
17
18
  'T' => 'Pingtung County' , 'U' => 'Hualien County',
18
19
  'V' => 'Taitung County' , 'W' => 'Kinmen County',
19
20
  'X' => 'Penghu County' , 'Z' => 'Lienchiang County',
21
+ ##
20
22
  # Letters no longer issued
21
23
  'L' => 'Taichung County' ,
22
24
  'R' => 'Tainan County' ,
@@ -35,7 +37,8 @@ module Dixon
35
37
 
36
38
  RULE_NUMBERS = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
37
39
 
38
- ## Check if a given ID is valid.
40
+ ##
41
+ # Check if a given ID is valid.
39
42
  # `id` must be a string, case is insensitive.
40
43
  # First convert ID into all numbers, then multiply to RULE_NUMBERS
41
44
  # element by element. Sum up and take a 10 modulo then check
@@ -49,11 +52,11 @@ module Dixon
49
52
  # sum 1, 45, 8, 14, 43, 15, 12, 21, 10, 7, 5
50
53
  # => 180
51
54
  # 180 mod 10, is the remainder equal to zero? If so, return true.
52
- #
53
55
  return (mapcar(-> (a,b) { a * b }, converted, RULE_NUMBERS).reduce(:+).divmod 10)[1] == 0
54
56
  end
55
57
 
56
- ## Returns gender for given ID.
58
+ ##
59
+ # Returns gender for given ID.
57
60
  # By check the second digit of ID.
58
61
  # 1 is male, 2 is female.
59
62
  def gender(id)
@@ -64,19 +67,22 @@ module Dixon
64
67
  end
65
68
  end
66
69
 
67
- ## Returns local agencies who issued your id.
70
+ ##
71
+ # Returns local agencies who issued your id.
68
72
  def issued_by(id)
69
73
  LETTER_ISSUED_BY[id[0]]
70
74
  end
71
75
 
72
76
  private
73
77
 
74
- ## Convert array of strings into integer array.
78
+ ##
79
+ # Convert array of strings into integer array.
75
80
  def to_i_array(str_arr)
76
81
  str_arr.map(&:to_i)
77
82
  end
78
83
 
79
- ## Convert an identification number to array of strings
84
+ ##
85
+ # Convert an identification number to array of strings
80
86
  # First letter conversion rules is defined in LETTER_NUMBERS
81
87
  # `convert 'F127337575'` will result in
82
88
  # [1, 5, 1, 2, 7, 3, 3, 7, 5, 7, 5]
@@ -84,8 +90,14 @@ module Dixon
84
90
  to_i_array((LETTER_NUMBERS[id[0]] + id[1..-1]).split(''))
85
91
  end
86
92
 
87
- ## mapcar operates on successive elements of the arrays.
88
- # fn is applied to the first element of each array, then to the second element of each array, and so on. The iteration terminates when the shortest array runs out, and excess elements in other arrays are ignored. The value returned by mapcar is a array of the results of successive calls to function
93
+ ##
94
+ # mapcar operates on successive elements of the arrays.
95
+ # fn is applied to the first element of each array,
96
+ # then to the secondf element of each array, and so on.
97
+ # The iteration terminates when the shortest array runs out,
98
+ # and excess elements in other arrays are ignored.
99
+ # The value returned by mapcar is a array of the results of
100
+ # successive calls to function.
89
101
  def mapcar(fn, *arrs)
90
102
  return [] if arrs.empty? or arrs.include? []
91
103
  transposed = if arrs.element_of_same_size
@@ -103,20 +115,38 @@ end
103
115
 
104
116
  module ValidateFormat
105
117
 
118
+ ##
119
+ # A123456789
106
120
  TAIWAN_ID_REGEXP = /[a-zA-Z][1|2][0-9]+/i
107
121
 
108
- ## Check if given id's format is valid.
122
+ ##
123
+ # Check if given id's format is valid.
109
124
  # The current ID number has exact 10 digits
110
125
  # The first digit is one capital English letter and
111
126
  # is followed by nine Arabic numerals.
112
- def valid_format?(id)
113
- return true if id =~ TAIWAN_ID_REGEXP and id.size == 10
114
- puts 'Invalid ID number.'
115
- false
127
+ # returns
128
+ # 'Empty ID.' for empty string
129
+ # 'Valid ID.' if the format is correct.
130
+ # otherwise, 'Invalid ID number.'
131
+ def validate_id_format(id)
132
+ id_str = id.to_s
133
+ return 'Empty ID.' if id_str.empty?
134
+ return 'Valid ID.' if id_str =~ TAIWAN_ID_REGEXP and id_str.size == 10
135
+ return Dixon::Validators::Taiwan::INVALID_MESSAGE
116
136
  end
117
137
 
138
+ ##
139
+ # Add a before_hook for methods that
140
+ # defined in literal symbol array.
118
141
  %i(checks gender issued_by convert).each do |m|
119
- define_method(m) { |id| super(id) if valid_format? id }
142
+ define_method(m) do |id|
143
+ is_it_valid = validate_id_format id
144
+ if is_it_valid == 'Valid ID.'
145
+ super(id)
146
+ else
147
+ return is_it_valid
148
+ end
149
+ end
120
150
  end
121
151
 
122
152
  private :convert
@@ -1,3 +1,3 @@
1
1
  module Dixon
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default)
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+ require_relative '../lib/dixon'
@@ -0,0 +1,34 @@
1
+ require_relative '../test_helper'
2
+
3
+ class TaiwanTest < Minitest::Unit::TestCase
4
+
5
+ def setup
6
+ @taiwan = Dixon::Validators::Taiwan.new
7
+ end
8
+
9
+ def test_checks_for_empty_id
10
+ empty_id = ''
11
+ assert_equal 'Empty ID.', @taiwan.checks(empty_id)
12
+ end
13
+
14
+ def test_checks_for_bad_format_id
15
+ bad_format_id = "A12345678"
16
+ assert_equal 'Invalid ID number.', @taiwan.checks(bad_format_id)
17
+ end
18
+
19
+ def test_checks_for_gender_out_of_range_id
20
+ gender_out_of_range_id = "A324567945"
21
+ assert_equal 'Invalid ID number.', @taiwan.checks(gender_out_of_range_id)
22
+ end
23
+
24
+ def test_checks_for_invalid_id
25
+ invalid_id = "A123456788"
26
+ assert_equal false, @taiwan.checks(invalid_id)
27
+ end
28
+
29
+ def test_checks_for_valid_id
30
+ valid_id = 'A123456789'
31
+ assert_equal true, @taiwan.checks(valid_id)
32
+ end
33
+
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dixon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juanito Fatas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-09 00:00:00.000000000 Z
11
+ date: 2013-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
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'
41
55
  description: Dixon checks your identification number!
42
56
  email:
43
57
  - katehuang0320@gmail.com
@@ -52,9 +66,11 @@ files:
52
66
  - Rakefile
53
67
  - dixon.gemspec
54
68
  - lib/dixon.rb
55
- - lib/dixon/validators/helpers.rb
69
+ - lib/dixon/validators/helper.rb
56
70
  - lib/dixon/validators/taiwan.rb
57
71
  - lib/dixon/version.rb
72
+ - test/test_helper.rb
73
+ - test/validators/taiwan_test.rb
58
74
  homepage: https://github.com/JuanitoFatas/dixon
59
75
  licenses:
60
76
  - MIT
@@ -79,5 +95,7 @@ rubygems_version: 2.0.6
79
95
  signing_key:
80
96
  specification_version: 4
81
97
  summary: Dixon checks your identification number!
82
- test_files: []
98
+ test_files:
99
+ - test/test_helper.rb
100
+ - test/validators/taiwan_test.rb
83
101
  has_rdoc: