Class: Viitenumero::Viite
- Inherits:
-
Object
- Object
- Viitenumero::Viite
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/viitenumero/viite.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(s) ⇒ Viite
constructor
A new instance of Viite.
- #machine_format ⇒ Object
- #paper_format ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(s) ⇒ Viite
Returns a new instance of Viite.
9 10 11 |
# File 'lib/viitenumero/viite.rb', line 9 def initialize(s) @number = format(s) end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
7 8 9 |
# File 'lib/viitenumero/viite.rb', line 7 def number @number end |
Class Method Details
.generate(base) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/viitenumero/viite.rb', line 25 def self.generate(base) base = (base || '').to_s.gsub(/\s+/, '') raise ArgumentError.new('must be a number') if base.match(/\A\d+\z/).nil? raise ArgumentError.new('must be between 3-19 chars long') if base.length < 3 || base.length > 19 Viite.new(base + checksum(base)) end |
.random(opts = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/viitenumero/viite.rb', line 33 def self.random(opts={}) opts.symbolize_keys! length = opts.fetch(:length, 4) raise ArgumentError if length < 4 || length > 20 base = '' base_length = length - 1 base = rand(10**base_length).to_s while base.length != base_length generate(base.to_s) end |
.valid?(s) ⇒ Boolean
43 44 45 |
# File 'lib/viitenumero/viite.rb', line 43 def self.valid?(s) Viite.new(s).valid? end |
Instance Method Details
#machine_format ⇒ Object
17 18 19 |
# File 'lib/viitenumero/viite.rb', line 17 def machine_format number end |
#paper_format ⇒ Object
13 14 15 |
# File 'lib/viitenumero/viite.rb', line 13 def paper_format number.gsub(/.{5}(?=.)/, '\0 ') end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/viitenumero/viite.rb', line 47 def to_s machine_format end |
#valid? ⇒ Boolean
21 22 23 |
# File 'lib/viitenumero/viite.rb', line 21 def valid? valid_format? and valid_length? and valid_checksum? end |