lib/aamva/generator.rb in aamva-0.1.0 vs lib/aamva/generator.rb in aamva-0.2.0

- old
+ new

@@ -1,39 +1,71 @@ -require 'faker' +# frozen_string_literal: true -module Aamva +module AAMVA class Generator - def self.dcg - DCG_MAPPING.keys.sample + attr_reader :standard + + def initialize(standard) + @standard = standard end - # Customer ID Number + def data + @data ||= Data.new( + header: header, + subfile_designators: subfile_designators, + subfiles: subfiles + ) + end - def self.daq - chars = ('A'..'Z').to_a + (('0'..'9').to_a) + private - chars.sample(25).join("") + def header + @header ||= Header.new( + issuer_identification_number: issuer_identification_number, + jurisdiction_version_number: jurisdiction_version_number, + number_of_entries: subfiles.size + ) end - # Physical Description – Eye Color + def subfile_designators + @subfile_designators ||= subfiles.map do |_, subfile| + length = Calculator.subfile_length( + type: subfile.type, + data_elements: subfile.data_elements, + data_element_separator: @standard["data_element_separator"], + segment_terminator: @standard["segment_terminator"] + ) - def self.day - DAY_MAPPING.keys.sample - end + offset = Calculator.subfile_offset - def self.dac - Faker::Name.first_name[0..MAX_DAC_LENGTH] + SubfileDesignator.new( + type: subfile.type, + length: length, + offset: offset + ) + end end - # Date of Birth + def subfiles + @subfiles ||= begin + data_elements = Hash[@standard.required_data_elements.map do |type| + [type.upcase, send(type)] + end] - def self.dbb - Faker::Date.birthday.strftime("%Y%m%d") + { + "DL" => Subfile.new( + type: "DL", + data_elements: data_elements + ) + } + end end - # Document Issue Date - - def self.dbd - Faker::Date.backward.strftime("%Y%m%d") + def method_missing(name, *args) + if factory = @standard.factory(name) + Factory.build(factory[:type], factory[:options]) + else + super + end end end end