Class: Ppp::Card::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ppp/card/base.rb

Overview

This class is abstract.

Subclass and override #to_s to implement a custom Card class.

Direct Known Subclasses

Html, Plain, Xml

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

Instance Method Summary collapse

Constructor Details

#initialize(generator, opts = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • generator (Generator)

    the generator this card will get passcodes from

  • opts (Hash) (defaults to: {})

    options to create the card with

Options Hash (opts):

  • :row_count (Fixnum) — default: 10

    the number of rows in the card

  • :card_title (String) — default: 'PPP Passcard'

    the title of the card

  • :first_card_number (Fixnum) — default: 1

    the number of the first card to create

Raises:

  • (ArgumentError)


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

  options = { :row_count         => 10,
              :card_title        => 'PPP Passcard',
              :first_card_number => 1 }
  options.merge! opts
  @title       = options[ :card_title        ]
  @row_count   = options[ :row_count         ]
  @card_number = options[ :first_card_number ]
end

Instance Attribute Details

#card_numberObject

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

#titleObject (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_lengthFixnum

Returns the number of characters in each passcode in the card.

Returns:

  • (Fixnum)

    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

#codesArray(Array(String))

Returns the passcodes on the current card.

Returns:

  • (Array(Array(String)))

    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

Overloads:

  • #passcode(cell) ⇒ String

    Returns the passcode in the given cell.

    Parameters:

    • cell (String)

      A cell ID. example: 10B

    Returns:

    • (String)

      the passcode in the given cell

  • #passcode(row, column) ⇒ String

    Returns the passcode in the given row and column.

    Parameters:

    • row (Fixnum)

      the row of the passcode to return

    • column (String)

      the column of the passcode to return

    Returns:

    • (String)

      the passcode in the given row and column



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_cardFixnum

Returns the number of passcodes to be printed on the card.

Returns:

  • (Fixnum)

    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_lineFixnum

Returns the number of passcodes to be printed on each line.

Returns:

  • (Fixnum)

    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_sObject



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

Overloads:

  • #verify(cell, given_passcode) ⇒ Boolean

    Returns true if the given passcode matches the passcode in the given cell.

    Parameters:

    • cell (String)

      A cell ID. example: 10B

    • given_passcode (String)

      the passcode to verify

    Returns:

    • (Boolean)

      true if the given passcode matches the passcode in the given cell

  • #verify(row, column, given_passcode) ⇒ Boolean

    Returns true if the given passcode matches the passcode in the given row and column.

    Parameters:

    • row (Fixnum)

      the row of the passcode to return

    • column (String)

      the column of the passcode to return

    • given_passcode (String)

      the passcode to verify

    Returns:

    • (Boolean)

      true if the given passcode matches the passcode in the given row and column



105
106
107
108
# File 'lib/ppp/card/base.rb', line 105

def verify *args
  given_code = args.pop
  given_code == passcode( *args )
end