Class: Egn::Egn
- Inherits:
-
Object
- Object
- Egn::Egn
- Defined in:
- lib/egn/egn.rb
Instance Attribute Summary collapse
-
#birth_date ⇒ Object
readonly
Returns the value of attribute birth_date.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Instance Method Summary collapse
- #day ⇒ Object
-
#initialize(args = nil) ⇒ Egn
constructor
Creates a new EGN object.
- #month ⇒ Object
-
#sex(options = {}) ⇒ Object
(also: #gender)
Formats default: :male | :female number: 1 | 2 char: ‘m’ | ‘f’.
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Is the number valid?.
- #year ⇒ Object
Constructor Details
#initialize(args = nil) ⇒ Egn
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.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 |
Instance Attribute Details
#birth_date ⇒ Object (readonly)
Returns the value of attribute birth_date.
4 5 6 |
# File 'lib/egn/egn.rb', line 4 def birth_date @birth_date end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
4 5 6 |
# File 'lib/egn/egn.rb', line 4 def number @number end |
Instance Method Details
#day ⇒ Object
33 34 35 |
# File 'lib/egn/egn.rb', line 33 def day @birth_date.day end |
#month ⇒ Object
37 38 39 |
# File 'lib/egn/egn.rb', line 37 def month @birth_date.month end |
#sex(options = {}) ⇒ Object Also known as: gender
Formats default: :male | :female number: 1 | 2 char: ‘m’ | ‘f’
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/egn/egn.rb', line 49 def sex( = {}) = { format: :default }.merge() male = number[8, 1].to_i.even? case [:format] when :default male ? :male : :female when :number male ? 1 : 2 when :char male ? 'm' : 'f' end end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/egn/egn.rb', line 67 def to_s "#{@number} | Gender: #{gender} | Birthdate: #{@birth_date} | Valid: #{valid?}" end |
#valid? ⇒ Boolean
Is the number valid?
29 30 31 |
# File 'lib/egn/egn.rb', line 29 def valid? @valid ||= Validator.validate(@number) end |
#year ⇒ Object
41 42 43 |
# File 'lib/egn/egn.rb', line 41 def year @birth_date.year end |