class Egn::Generator

Attributes

options[R]

Public Class Methods

generate(options = {}) click to toggle source

Convenience method

# File lib/egn/generator.rb, line 7
def self.generate(options = {})
  Generator.new(options).generate
end
new(options = {}) click to toggle source
# File lib/egn/generator.rb, line 11
def initialize(options = {})
  validate!(options)
  set_defaults!(options)
  process!
end

Public Instance Methods

generate() click to toggle source

The generated EGN will be completely random if no options are given. options is a hash that may have the following keys: :year, :month, :day, :gender

# File lib/egn/generator.rb, line 19
def generate
  egn = format(options[:year]) +
        format(options[:month]) +
        format(options[:day]) +
        format(options[:region], 3)

  egn + Util.egn_checksum(egn).to_s
end

Private Instance Methods

defaults() click to toggle source

Random defaults

# File lib/egn/generator.rb, line 52
def defaults
  date = -> { Util.time_rand }.call
  {
    year:    date.year,
    month:   date.month,
    day:     date.day,
    gender:  genders.sample,
    region:  Random.rand(0..999)
  }
end
determine_century(year) click to toggle source
# File lib/egn/generator.rb, line 95
def determine_century(year)
  year - (year % 100)
end
format(val, pre = 2) click to toggle source

Little helper that prefixes strings with 0s

# File lib/egn/generator.rb, line 39
def format(val, pre = 2)
  val.to_s.rjust(pre, '0')
end
genders() click to toggle source
# File lib/egn/generator.rb, line 99
def genders
  [:male, :female]
end
month_delta(century) click to toggle source
# File lib/egn/generator.rb, line 85
def month_delta(century)
  if century == 1800
    20
  elsif century == 2000
    40
  else
    0
  end
end
process!() click to toggle source
# File lib/egn/generator.rb, line 63
def process!
  # Get random century, region and gender
  century = determine_century(options[:year])

  options[:month] += month_delta(century)

  options[:region] += region_delta(options[:gender], options[:region])

  options[:year] = options[:year] - century
end
region_delta(gender, region) click to toggle source

Recalculate region based on gender

# File lib/egn/generator.rb, line 75
def region_delta(gender, region)
  if gender == :male && region.odd?
    -1
  elsif gender == :female && region.even?
    1
  else
    0
  end
end
set_defaults!(options) click to toggle source
# File lib/egn/generator.rb, line 30
def set_defaults!(options)
  @options = {}

  until Date.valid_date?(@options[:year].to_i, @options[:month].to_i, @options[:day].to_i)
    @options = defaults.merge(options)
  end
end
validate!(options) click to toggle source

Check if the options contain a date that is valid and be turned into an EGN

# File lib/egn/generator.rb, line 44
def validate!(options)
  raise ArgumentError, 'Year out of bounds' if options[:year] && !(1800..2099).include?(options[:year])
  raise ArgumentError, 'Month out of bounds' if options[:month] && !(1..12).include?(options[:month])
  raise ArgumentError, 'Day out of bounds' if options[:day] && !(1..31).include?(options[:day])
  raise ArgumentError, "Gender should be one of #{genders}" if options[:gender] && !genders.include?(options[:gender])
end