Class: Egn::Validator
- Inherits:
-
Object
- Object
- Egn::Validator
- Defined in:
- lib/egn/validator.rb
Instance Attribute Summary collapse
-
#egn ⇒ Object
readonly
Returns the value of attribute egn.
Class Method Summary collapse
-
.validate(egn) ⇒ Object
Convenience method.
Instance Method Summary collapse
-
#initialize(egn) ⇒ Validator
constructor
A new instance of Validator.
-
#validate ⇒ Object
Checks if a given EGN is valid.
Constructor Details
#initialize(egn) ⇒ Validator
10 11 12 13 14 |
# File 'lib/egn/validator.rb', line 10 def initialize(egn) @egn = egn @year, @month, @day = explode_date(egn) end |
Instance Attribute Details
#egn ⇒ Object (readonly)
Returns the value of attribute egn.
3 4 5 |
# File 'lib/egn/validator.rb', line 3 def egn @egn end |
Class Method Details
.validate(egn) ⇒ Object
Convenience method
6 7 8 |
# File 'lib/egn/validator.rb', line 6 def self.validate(egn) Validator.new(egn).validate end |
Instance Method Details
#validate ⇒ Object
Checks if a given EGN is valid
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/egn/validator.rb', line 17 def validate return false unless @egn.length == 10 return false unless Date.valid_date?(@year, @month, @day) # Gregorian calendar adoption in 1916 in Bulgaria # 31/03/1916 > +13 days > 14/04/1916 return false if @year == 1916 && @month == 4 && @day <= 13 # Calculate the checksum and check if the given one is correct checksum = Util.egn_checksum(@egn[0, 9]) checksum == @egn[9].to_i end |