class Pontifex::KeyStream

Constants

JA
JB

Attributes

deck[R]

Public Class Methods

new(key=Pontifex::DEFAULT_KEY) click to toggle source
# File lib/pontifex/key_stream.rb, line 16
def initialize(key=Pontifex::DEFAULT_KEY)
  @deck = process_param(key)
end

Public Instance Methods

letter() click to toggle source
# File lib/pontifex/key_stream.rb, line 27
def letter
  count = @deck.first.to_i
  if result = @deck[count].to_c
    result
  else
    sequence!
    letter
  end
end
sequence!() click to toggle source
# File lib/pontifex/key_stream.rb, line 20
def sequence!
  move_down!("ja", 1)
  move_down!("jb", 2)
  triple_cut!
  count_cut!
end
to_key() click to toggle source
# File lib/pontifex/key_stream.rb, line 37
def to_key
  @deck.map { |card| card.str }.join(",")
end

Private Instance Methods

count_cut!() click to toggle source
# File lib/pontifex/key_stream.rb, line 73
def count_cut!
  count = @deck.last.to_i
  top = @deck.shift(count)
  @deck.insert(-2, *top)
end
move_down!(card_str, num) click to toggle source
# File lib/pontifex/key_stream.rb, line 51
def move_down!(card_str, num)
  num.times do
    index = @deck.index { |c| c.str == card_str }
    unless @deck[index] == @deck.last
      @deck[index], @deck[index + 1] = @deck[index + 1], @deck[index]
    else
      @deck.insert(1, @deck.pop)
    end
  end
end
process_param(str) click to toggle source
# File lib/pontifex/key_stream.rb, line 43
def process_param(str)
  #TODO I need to raise an exception here if any 'Ja' jokers,
  #     as opposed to 'ja' jokers, (or 'Jb') are included in
  #     the key -- Or does this exception belong in Card?
  key_ary = str.split(",")
  key_ary.map { |param| Card.new(param) }
end
triple_cut!() click to toggle source
# File lib/pontifex/key_stream.rb, line 62
def triple_cut!
  ti, bi = @deck.index(JA), @deck.index(JB)
  ti, bi = bi, ti if ti > bi

  top = @deck[0...ti]
  middle = @deck[ti..bi]
  bottom = @deck[(bi + 1)..-1]

  @deck.replace(bottom + middle + top)
end