lib/egn/generator.rb in egn-1.3.1 vs lib/egn/generator.rb in egn-1.3.2
- old
+ new
@@ -13,14 +13,16 @@
set_defaults!(options)
process!
end
# 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
+ # options is a hash that may have the following keys: :year, :month, :day, :gender
def generate
- # YY MM DD REST
- egn = format(options[:year]) + format(options[:month]) + format(options[:day]) + format(options[:region], 3)
+ egn = format(options[:year]) +
+ format(options[:month]) +
+ format(options[:day]) +
+ format(options[:region], 3)
egn + Util.egn_checksum(egn).to_s
end
private
@@ -42,41 +44,41 @@
# 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, "Sex should be one of #{sexes}" if options[:sex] && !sexes.include?(options[:sex])
+ raise ArgumentError, "Gender should be one of #{genders}" if options[:gender] && !genders.include?(options[:gender])
end
# Random defaults
def defaults
date = -> { Util.time_rand }.call
{
year: date.year,
month: date.month,
day: date.day,
- sex: sexes.sample,
+ gender: genders.sample,
region: Random.rand(0..999)
}
end
def process!
- # Get random century, region and sex
+ # Get random century, region and gender
century = determine_century(options[:year])
options[:month] += month_delta(century)
- options[:region] += region_delta(options[:sex], options[:region])
+ options[:region] += region_delta(options[:gender], options[:region])
options[:year] = options[:year] - century
end
- # Recalculate region based on sex
- def region_delta(sex, region)
- if sex == :male && region.odd?
+ # Recalculate region based on gender
+ def region_delta(gender, region)
+ if gender == :male && region.odd?
-1
- elsif sex == :female && region.even?
+ elsif gender == :female && region.even?
1
else
0
end
end
@@ -93,10 +95,10 @@
def determine_century(year)
year - (year % 100)
end
- def sexes
+ def genders
[:male, :female]
end
end
end