class Egn::Egn
Attributes
birth_date[R]
number[R]
Public Class Methods
new(args = nil)
click to toggle source
Creates a new EGN object. Has different effects depending on the arguments: when no arguments are given, it generates a new random EGN; when a String is given, it is parsed as an EGN; when a hash is given, a new EGN is generated with the provided options.
# File lib/egn/egn.rb, line 10 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
Public Instance Methods
day()
click to toggle source
# File lib/egn/egn.rb, line 33 def day @birth_date.day end
month()
click to toggle source
# File lib/egn/egn.rb, line 37 def month @birth_date.month end
sex(options = {})
click to toggle source
Formats default: :male | :female number: 1 | 2 char: 'm' | 'f'
# File lib/egn/egn.rb, line 49 def sex(options = {}) options = { format: :default }.merge(options) male = number[8, 1].to_i.even? case options[:format] when :default male ? :male : :female when :number male ? 1 : 2 when :char male ? 'm' : 'f' end end
Also aliased as: gender
to_s()
click to toggle source
# File lib/egn/egn.rb, line 67 def to_s "#{@number} | Gender: #{gender} | Birthdate: #{@birth_date} | Valid: #{valid?}" end
valid?()
click to toggle source
Is the number valid?
# File lib/egn/egn.rb, line 29 def valid? @valid ||= Validator.validate(@number) end
year()
click to toggle source
# File lib/egn/egn.rb, line 41 def year @birth_date.year end
Private Instance Methods
parse!()
click to toggle source
Extract the birth_date
, sex and region
# File lib/egn/egn.rb, line 74 def parse! info = Parser.new(@number) @birth_date = info.date end