Class: Luhn::CivicNumber
- Inherits:
-
Object
- Object
- Luhn::CivicNumber
- Defined in:
- lib/luhn/civic_number.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #birth_date ⇒ Object
-
#civic_number ⇒ Object
For backwards compability.
- #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 self.value = string end |
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/luhn/civic_number.rb', line 5 def value @value 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 OpenStruct.new({ :year => value[0...2].to_i, :month => value[2...4].to_i, :day => value[4...6].to_i }) end |
#civic_number ⇒ Object
For backwards compability
54 55 56 |
# File 'lib/luhn/civic_number.rb', line 54 def civic_number value end |
#control_digit ⇒ Object
19 20 21 |
# File 'lib/luhn/civic_number.rb', line 19 def control_digit @control_digit ||= Luhn.control_digit(value[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 46 47 |
# File 'lib/luhn/civic_number.rb', line 43 def formatted return value if value.length < 10 value.insert(value.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 valid? ? (value[8...9].to_i.even? ? 'female' : 'male') : 'unknown' end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/luhn/civic_number.rb', line 49 def to_s value end |
#valid? ⇒ Boolean
11 12 13 |
# File 'lib/luhn/civic_number.rb', line 11 def valid? @valid ||= value.length == 10 && valid_date? && Luhn.valid?(value) 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 |