lib/egn/generator.rb in egn-1.3.2 vs lib/egn/generator.rb in egn-1.3.3

- old
+ new

@@ -1,22 +1,22 @@ # Generates a random valid EGN module Egn class Generator attr_reader :options - # Convinience method - def self.generate(options={}) + # Convenience method + def self.generate(options = {}) Generator.new(options).generate end - def initialize(options={}) + def initialize(options = {}) validate!(options) set_defaults!(options) process! end - # The generated EGN will be completely random if no opitons are given. + # 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 def generate egn = format(options[:year]) + format(options[:month]) + format(options[:day]) + @@ -31,23 +31,22 @@ @options = {} until Date.valid_date?(@options[:year].to_i, @options[:month].to_i, @options[:day].to_i) @options = defaults.merge(options) end - end - # Little helper - def format(val, pre=2) + # Little helper that prefixes strings with 0s + def format(val, pre = 2) val.to_s.rjust(pre, '0') end # Check if the options contain a date that is valid and be turned into an EGN 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, '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 # Random defaults def defaults @@ -98,8 +97,7 @@ end def genders [:male, :female] end - end end