Class: Ppp::Card::Base Abstract
- Inherits:
-
Object
- Object
- Ppp::Card::Base
- Defined in:
- lib/ppp/card/base.rb
Overview
Subclass and override #to_s to implement a custom Card class.
Constant Summary collapse
- @@CHARS_PER_LINE =
34
- @@FIRST_COLUMN =
?A
- @@ROW_COL_PATTERN =
/([[:digit:]]+)([[:alpha:]])/
- @@ERROR_BAD_ROW_COL =
%[Expected a string with exactly one integer followed by one letter, got "%s"]
- @@ERROR_LONG_CODES =
%[Passcodes longer than 16 characters are too long for printing]
- @@ERROR_WRONG_NUM_ARGS =
%[Wrong number of arguments. Expected %s, got %d]
Instance Attribute Summary collapse
-
#card_number ⇒ Object
Returns the value of attribute card_number.
-
#row_count ⇒ Object
readonly
Returns the value of attribute row_count.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#code_length ⇒ Fixnum
The number of characters in each passcode in the card.
-
#codes ⇒ Array(Array(String))
The passcodes on the current card.
-
#initialize(generator, opts = {}) ⇒ Base
constructor
A new instance of Base.
- #passcode(*args) ⇒ Object
-
#passcodes_per_card ⇒ Fixnum
The number of passcodes to be printed on the card.
-
#passcodes_per_line ⇒ Fixnum
The number of passcodes to be printed on each line.
- #to_s ⇒ Object
- #verify(*args) ⇒ Object
Constructor Details
#initialize(generator, opts = {}) ⇒ Base
Returns a new instance of Base.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ppp/card/base.rb', line 22 def initialize generator, opts={} @generator = generator raise ArgumentError.new( @@ERROR_LONG_CODES ) if code_length > 16 = { :row_count => 10, :card_title => 'PPP Passcard', :first_card_number => 1 } .merge! opts @title = [ :card_title ] @row_count = [ :row_count ] @card_number = [ :first_card_number ] end |
Instance Attribute Details
#card_number ⇒ Object
Returns the value of attribute card_number.
7 8 9 |
# File 'lib/ppp/card/base.rb', line 7 def card_number @card_number end |
#row_count ⇒ Object (readonly)
Returns the value of attribute row_count.
7 8 9 |
# File 'lib/ppp/card/base.rb', line 7 def row_count @row_count end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/ppp/card/base.rb', line 7 def title @title end |
Instance Method Details
#code_length ⇒ Fixnum
Returns the number of characters in each passcode in the card.
36 37 38 |
# File 'lib/ppp/card/base.rb', line 36 def code_length @generator.length end |
#codes ⇒ Array(Array(String))
Returns the passcodes on the current card.
60 61 62 63 64 65 66 |
# File 'lib/ppp/card/base.rb', line 60 def codes (1..row_count).collect do |row| offset = card_offset + ((row-1) * passcodes_per_line) @generator.passcodes offset, passcodes_per_line end end |
#passcode(cell) ⇒ String #passcode(row, column) ⇒ String
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ppp/card/base.rb', line 75 def passcode *args case args.size when 1 match = @@ROW_COL_PATTERN.match( args.first ) raise ArgumentError.new( @@ERROR_BAD_ROW_COL % args.first ) unless match row = match[1] col = match[2] when 2 (row, col) = args else raise ArgumentError.new( @@ERROR_WRONG_NUM_ARGS % ['1 or 2', args.size] ) end col_offset = col.ord - @@FIRST_COLUMN.ord row_offset = row.to_i - 1 offset = row_offset * passcodes_per_line + col_offset offset = card_offset + offset @generator.passcode offset end |
#passcodes_per_card ⇒ Fixnum
Returns the number of passcodes to be printed on the card.
55 56 57 |
# File 'lib/ppp/card/base.rb', line 55 def passcodes_per_card passcodes_per_line * row_count end |
#passcodes_per_line ⇒ Fixnum
Returns the number of passcodes to be printed on each line.
50 51 52 |
# File 'lib/ppp/card/base.rb', line 50 def passcodes_per_line @passcodes_per_line ||= ( (@@CHARS_PER_LINE+1) / (code_length + 1) ).to_i end |
#to_s ⇒ Object
110 111 112 |
# File 'lib/ppp/card/base.rb', line 110 def to_s raise "unimplemented" end |
#verify(cell, given_passcode) ⇒ Boolean #verify(row, column, given_passcode) ⇒ Boolean
105 106 107 108 |
# File 'lib/ppp/card/base.rb', line 105 def verify *args given_code = args.pop given_code == passcode( *args ) end |