Class: Luhn::CivicNumber
- Inherits:
-
Object
- Object
- Luhn::CivicNumber
- Defined in:
- lib/luhn/civic_number.rb
Instance Attribute Summary collapse
-
#civic_number ⇒ Object
readonly
Returns the value of attribute civic_number.
Class Method Summary collapse
Instance Method Summary collapse
- #birth_date ⇒ Object
- #control_digit ⇒ Object
- #female? ⇒ Boolean
- #formatted ⇒ Object
-
#initialize(string) ⇒ CivicNumber
constructor
A new instance of CivicNumber.
- #male? ⇒ Boolean
- #sex ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #valid_date? ⇒ Boolean
Constructor Details
#initialize(string) ⇒ CivicNumber
Returns a new instance of CivicNumber.
7 8 9 |
# File 'lib/luhn/civic_number.rb', line 7 def initialize(string) @civic_number = cleanup_string(string.to_s) end |
Instance Attribute Details
#civic_number ⇒ Object (readonly)
Returns the value of attribute civic_number.
5 6 7 |
# File 'lib/luhn/civic_number.rb', line 5 def civic_number @civic_number end |
Class Method Details
Instance Method Details
#birth_date ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/luhn/civic_number.rb', line 35 def birth_date @date ||= OpenStruct.new({ :year => civic_number[0...2].to_i, :month => civic_number[2...4].to_i, :day => civic_number[4...6].to_i }) end |
#control_digit ⇒ Object
19 20 21 |
# File 'lib/luhn/civic_number.rb', line 19 def control_digit @control_digit ||= Luhn.control_digit(civic_number[0...9]) end |
#female? ⇒ Boolean
27 28 29 |
# File 'lib/luhn/civic_number.rb', line 27 def female? sex == 'female' end |
#formatted ⇒ Object
43 44 45 |
# File 'lib/luhn/civic_number.rb', line 43 def formatted to_s.insert(civic_number.length - 4, "-") end |
#male? ⇒ Boolean
31 32 33 |
# File 'lib/luhn/civic_number.rb', line 31 def male? sex == 'male' end |
#sex ⇒ Object
23 24 25 |
# File 'lib/luhn/civic_number.rb', line 23 def sex @sex ||= valid? ? (civic_number[8...9].to_i.even? ? 'female' : 'male') : 'unknown' end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/luhn/civic_number.rb', line 47 def to_s civic_number end |
#valid? ⇒ Boolean
11 12 13 |
# File 'lib/luhn/civic_number.rb', line 11 def valid? @valid ||= civic_number.length == 10 && valid_date? && Luhn.valid?(civic_number) end |
#valid_date? ⇒ Boolean
15 16 17 |
# File 'lib/luhn/civic_number.rb', line 15 def valid_date? (1..12).include?(birth_date.month) && (1..31).include?(birth_date.day) end |