Sha256: c9c7b8967acaf60b0ad4eb57c19408e670aca6a31f42ba6e482e7ed0244b2a74

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# The main data class
module Egn
  class Egn
    attr_reader :number, :birth_date

    # Creates a new EGN object. Has different effects depending on the argument.
    # When no arguments are given, it generates a new random EGN
    # When a String is given, it is assumed that it is an EGN and is parsed
    # When a hash is given, a new EGN is generated with the provided options
    def initialize(args=nil)

      if args.nil?
        @number = Generator.generate
      else
        case args
        when Hash
          @number = Generator.generate(args)
        when String
          @number = args
          raise ArgumentError, "Invalid EGN" unless self.valid?
        else
          raise ArgumentError, "Egn#new should be called either with an EGN or with an options hash"
        end
      end

      parse!
    end

    # Is the number valid?
    def valid?
      @valid ||= Validator.validate(@number)
    end

    def day
      @birth_date.day
    end

    def month
      @birth_date.month
    end

    def year
      @birth_date.year
    end

    def to_s
      @number
    end

    private

    # Extract the birth_date, sex and region
    def parse!
      info = Parser.new(@number)

      @birth_date = info.date
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
egn-1.0.0 lib/egn/egn.rb
egn-0.4.0 lib/egn/egn.rb