diff --git a/Rakefile b/Rakefile index 308670b..ee39853 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,5 @@ require "bundler/gem_tasks" +require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) do |t| t.pattern = 'tests/*_spec.rb' diff --git a/src/pincode_validator.rb b/src/pincode_validator.rb index 4aa3d73..52fe036 100644 --- a/src/pincode_validator.rb +++ b/src/pincode_validator.rb @@ -1,6 +1,23 @@ require "pincode_validator/version" module PincodeValidator + VERSION = "1.0.3" + FILENAME='regex.txt' + class Error < StandardError; end - # Your code goes here... + + def self.root + File.dirname __dir__ + end + + @@regexes ||= IO.readlines(File.join root, FILENAME).map do |line| + Regexp.new("^#{line.strip[1...-1]}$") + end + + def self.valid?(pincode) + @@regexes.each do |r| + return true if r.match? pincode + end + false + end end diff --git a/src/pincode_validator/version.rb b/src/pincode_validator/version.rb deleted file mode 100644 index 8246a87..0000000 --- a/src/pincode_validator/version.rb +++ /dev/null @@ -1,3 +0,0 @@ -module PincodeValidator - VERSION = "1.0.3" -end diff --git a/tests/validate_spec.rb b/tests/validate_spec.rb index 0a59546..247eab7 100644 --- a/tests/validate_spec.rb +++ b/tests/validate_spec.rb @@ -6,4 +6,10 @@ describe PincodeValidator do expect(described_class::valid? pin).to eq(true) end end + + it 'should validate incorrect pincodes' do + ['999999', '99999', '9999', '999', '99', '9', '111111', '2447131'].each do |pin| + expect(described_class::valid? pin).to eq(false), "#{pin} should be invalid" + end + end end