module Identification # Represents an identity document. # This class carries base methods that will appear in all document types. # # @abstract Subclass and implement a {#parse} method (setting the fields and @validity) # and alter {#initialize} to implement a custom Document class. See other classes for examples. class Document attr_accessor :last_name, :date_of_birth, :gender # Creates an instance of a documents. # Will automatically parse if the drivers number is given, and will automatically generate if all necessary fields are set. # # @param [Hash] details of the paramaters # @option opts [String] :last_name surname of the individual (only set for generating) # @option opts [String] :date_of_birth date of birth of the individual (only set for generating) # @option opts [String] :gender gender of the individual (only set for generating) def initialize(params = {}) @last_name = params[:last_name] @date_of_birth = params[:date_of_birth] @gender = params[:gender] end # Returns true if the drivers license is valid. # Requires the driver number to be parsed before it can be called. # # @return [Boolean] whether or not the passport is valid # @raise [RuntimeError] if no mrz has been parsed yet. def valid? if defined? @validity return @validity else fail 'No document number has been parsed.' end end # Returns the age of the individual. # Requires a date of birth to be set before it can be called. # # @return [Boolean] age of the individual # @raise [RuntimeError] if no date of birth is set def age if !@date_of_birth.nil? now = Time.now.utc.to_date dob = @date_of_birth return now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1) else fail 'No date of birth has been set.' end end # Returns true if the individual is over 18 (18+) # Requires a date of birth to be set before it can be called. # # @return [Boolean] whether or not the individual is over 18 # @raise [RuntimeError] if no date of birth is set def over_18? if !@date_of_birth.nil? return true if age >= 18 else fail 'No date of birth has been set.' end false end # Returns true if the individual is over 21 (21+) # Requires a date of birth to be set before it can be called. # # @return [Boolean] whether or not the individual is over 21 # @raise [RuntimeError] if no date of birth is set def over_21? if !@date_of_birth.nil? return true if age >= 21 else fail 'No date of birth has been set.' end false end end end