lib/egn/validator.rb in egn-1.2.1 vs lib/egn/validator.rb in egn-1.2.2

- old
+ new

@@ -7,24 +7,29 @@ Validator.new(egn).validate end def initialize(egn) @egn = egn + + @year, @month, @day = explode_date(egn) end # Checks if a given EGN is valid def validate - return false unless egn.length == 10 + return false unless @egn.length == 10 + return false unless Date.valid_date?(@year, @month, @day) - # Extract the correct year and month - year, month, day = egn.scan(/.{1,2}/).map(&:to_i) - year, month = Util.determine_date(year, month) + # Calculate the checksum and check if the given one is correct + checksum = Util.egn_checksum(@egn[0,9]) + checksum == @egn[9].to_i + end - return false unless Date.valid_date? year, month, day + private - # Calculate the checksum and check if the given one is correct - checksum = Util.egn_checksum egn[0,9] - checksum == egn[9].to_i + def explode_date(egn) + year, month, day = egn.scan(/.{1,2}/).map(&:to_i) + year, month = Util.determine_date(year, month) + [year, month, day] end end end