Sha256: 0083c562140c834977a9a4c2533d66fd3e5b495f648dbf54af35a557e722efe0
Contents?: true
Size: 1.41 KB
Versions: 1
Compression:
Stored size: 1.41 KB
Contents
# Generates a random valid EGN module Egn module Generator # The generated EGN will be completely random if no opitons are given. # options is a hash that may have the following keys: :year, :month and :date def self.generate(options={}) date = Util.time_rand options = { year: date.year, month: date.month, day: date.day }.merge(options) validate!(options) century = options[:year] - (options[:year] % 100) sex = Random.rand(1..2) if century == 1800 options[:month] += 20 elsif century == 2000 options[:month] += 40 end region = Random.rand(0..999) if sex == 1 && (region %2 != 0) region -= 1 elsif sex == 2 && (region % 2 == 0) region += 1 end final_year = options[:year] - century egn = final_year.to_s.rjust(2, '0') + options[:month].to_s.rjust(2, '0') + options[:day].to_s.rjust(2,'0') + region.to_s.rjust(3,'0') return egn + Util.egn_checksum(egn).to_s end # Check if the options contain a date that is valid and be turned into an EGN def self.validate!(options) raise ArgumentError, "Year out of bounds" unless (1800..2099).include?(options[:year]) raise ArgumentError, "Month out of bounds" unless (1..12).include?(options[:month]) raise ArgumentError, "Day out of bounds" unless (1..31).include?(options[:day]) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
egn-0.4.0 | lib/egn/generator.rb |