Class: Luhn::CivicNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/luhn/civic_number.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/luhn/civic_number.rb', line 5

def value
  @value
end

Class Method Details

.generateObject



58
59
60
61
# File 'lib/luhn/civic_number.rb', line 58

def self.generate
  date = Time.local(Time.now.year - rand(100) - 1, rand(12) + 1, rand(31) + 1)
  Luhn.generate(10, :prefix => date.strftime("%y%m%d"))
end

Instance Method Details

#birth_dateObject



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_numberObject

For backwards compability



54
55
56
# File 'lib/luhn/civic_number.rb', line 54

def civic_number
  value
end

#control_digitObject



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

Returns:

  • (Boolean)


27
28
29
# File 'lib/luhn/civic_number.rb', line 27

def female?
  sex == 'female'
end

#formattedObject



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

Returns:

  • (Boolean)


31
32
33
# File 'lib/luhn/civic_number.rb', line 31

def male?
  sex == 'male'
end

#sexObject



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_sObject



49
50
51
# File 'lib/luhn/civic_number.rb', line 49

def to_s
  value
end

#valid?Boolean

Returns:

  • (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

Returns:

  • (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