Permalink
Browse files

Release 0.0.1

  • Loading branch information...
akonan committed Sep 27, 2012
0 parents commit dae5076dba249db79463b4a52972253a9e4e836c
Showing with 203 additions and 0 deletions.
  1. +3 −0 CHANGELOG.md
  2. +2 −0 Gemfile
  3. +27 −0 Gemfile.lock
  4. +20 −0 LICENSE
  5. +31 −0 README.md
  6. +14 −0 Rakefile
  7. +48 −0 lib/reference_number.rb
  8. +3 −0 lib/reference_number/version.rb
  9. +19 −0 reference_number.gemspec
  10. +29 −0 spec/reference_number_spec.rb
  11. +7 −0 spec/spec_helper.rb
@@ -0,0 +1,3 @@
+### ReferenceNumber 0.0.1 (2012-09-28)
+
+* First version created.
@@ -0,0 +1,2 @@
+source "http://rubygems.org"
+gemspec
@@ -0,0 +1,27 @@
+PATH
+ remote: .
+ specs:
+ reference_number (0.0.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ diff-lcs (1.1.3)
+ rake (0.9.2.2)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.3)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler
+ rake
+ reference_number!
+ rspec (~> 2.11.0)
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Antti Akonniemi and contributors.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
+# ReferenceNumber
+
+Original idea and code was gotten from Christian:
+http://snippets.aktagon.com/snippets/51-How-to-Calculate-a-reference-number-with-Ruby
+
+## Usage
+
+Add this to your gem file:
+
+ gem "reference_number"
+
+Example usage:
+
+ ReferenceNumber.new("1111").to_s
+ ReferenceNumber.new(1111).to_s
+
+The above example prints out “11112”.
+
+ ReferenceNumber.new(1111, :zero_padding => true).to_s
+
+The above example prints out zero padded reference number "00000000000000011112".
+
+This example shows how to generate a series of reference number:
+
+ (1000..1023).to_a.each do |number|
+ puts ReferenceNumber.new(number).to_s
+ end
+
+## License
+
+ReferenceNumber is licensed under the MIT License, for more details see the LICENSE file.
@@ -0,0 +1,14 @@
+require "bundler"
+Bundler.setup
+
+require "rspec/core/rake_task"
+RSpec::Core::RakeTask.new(:spec)
+
+gemspec = eval(File.read("reference_number.gemspec"))
+
+task :build => "#{gemspec.full_name}.gem"
+
+file "#{gemspec.full_name}.gem" => gemspec.files + ["reference_number.gemspec"] do
+ system "gem build reference_number.gemspec"
+ system "gem install reference_number-#{ReferenceNumber::VERSION}.gem"
+end
@@ -0,0 +1,48 @@
+class ReferenceNumber
+ class InvalidReferenceNumber < ArgumentError
+ end
+
+ attr_accessor :input
+
+ def initialize(input, opts = { :zero_padding => false })
+ @zero_padding = opts[:zero_padding]
+
+ @input = input
+ if(@input.is_a? Fixnum)
+ @input = @input.to_s
+ end
+
+ raise InvalidReferenceNumber, "Input should be between 3 to 19 characters long" if (@input.length < 3 || @input.length > 19)
+ raise InvalidReferenceNumber, "Only spaces and numbers are allowed" if (@input =~ /[^ \d]/) != nil
+
+ @input.gsub!(/ /, "")
+ @input.sub!(/0*/, "") # remove leading zeros
+
+ raise InvalidReferenceNumber, "Input can't be just spaces and zeroes." if(@input.length == 0)
+ end
+
+ def to_s(options = { :zero_padding => false })
+ @input.reverse!
+ base = "731" * 50
+
+ index = 0
+ sum = 0
+
+ @input.each_byte do |b|
+ result = b.chr.to_i * base[index % 3].chr.to_i
+ sum = sum + result
+ index = index + 1
+ end
+
+ # Calculate the last number. If sum = 94 then number is 6.
+ difference = (10 - (sum % 10)) % 10
+ difference_and_input = "#{difference}#{@input}".reverse
+
+ if @zero_padding
+ "%020d" % difference_and_input if @zero_padding
+ else
+ difference_and_input
+ end
+ end
+
+end
@@ -0,0 +1,3 @@
+class ReferenceNumber
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,19 @@
+require File.expand_path("../lib/reference_number/version", __FILE__)
+
+Gem::Specification.new do |s|
+ s.name = "reference_number"
+ s.version = ReferenceNumber::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Antti Akonniemi"]
+ s.email = ["antti@kiskolabs.com"]
+ s.homepage = "http://github.com/akonan/reference_number"
+ s.summary = "A gem for calculating reference number (viitenumere, referensnummer)"
+ s.description = "No need to copy paste your old reference number codes anymore, just use this gem"
+ s.required_rubygems_version = ">= 1.3.6"
+ s.add_development_dependency "bundler"
+ s.add_development_dependency "rake"
+ s.add_development_dependency "rspec", "~> 2.11.0"
+
+ s.files = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md", "lib/generators/**/*.*"]
+ s.require_path = 'lib'
+end
@@ -0,0 +1,29 @@
+require "spec_helper"
+
+describe ReferenceNumber do
+
+ it "gives the right reference number when input is right" do
+ ReferenceNumber.new(100).to_s.should == "1009"
+ ReferenceNumber.new("100").to_s.should == "1009"
+ ReferenceNumber.new("000100").to_s.should == "1009"
+ ReferenceNumber.new("1000").to_s == "10003"
+ ReferenceNumber.new("10000").to_s.should == "100007"
+ ReferenceNumber.new("234").to_s.should == "2341"
+ ReferenceNumber.new("0234").to_s.should == "2341"
+ ReferenceNumber.new("123123123234234234").to_s.should == "1231231232342342349"
+ ReferenceNumber.new("1000000000").to_s.should == "10000000003"
+ end
+
+ it "pads the reference number when asked to" do
+ ReferenceNumber.new("100", :zero_padding => true).to_s.should == "00000000000000001009"
+ end
+
+ it "gives an error, if the input is incorrect" do
+ lambda {ReferenceNumber.new("")}.should raise_error(ReferenceNumber::InvalidReferenceNumber)
+ lambda {ReferenceNumber.new(" ")}.should raise_error(ReferenceNumber::InvalidReferenceNumber)
+ lambda {ReferenceNumber.new("12")}.should raise_error(ReferenceNumber::InvalidReferenceNumber)
+ lambda {ReferenceNumber.new(1)}.should raise_error(ReferenceNumber::InvalidReferenceNumber)
+ lambda {ReferenceNumber.new("0000")}.should raise_error(ReferenceNumber::InvalidReferenceNumber)
+ end
+
+end
@@ -0,0 +1,7 @@
+require "bundler"
+
+Bundler.setup
+
+require "rspec"
+require "reference_number"
+

0 comments on commit dae5076

Please sign in to comment.