Class: Pokerstats::PokerstarsFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pokerstats/pokerstars_file.rb

Constant Summary collapse

POKERSTARS_HEADER_PATTERN =
/PokerStars Game #([0-9]+)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, starting_at = 0, transliterate_from = "ISO-8859-1", transliterate_to = "ASCII//TRANSLIT//IGNORE") ⇒ PokerstarsFile

Returns a new instance of PokerstarsFile.



13
14
15
16
17
18
# File 'lib/pokerstats/pokerstars_file.rb', line 13

def initialize(filename, starting_at = 0, transliterate_from = "ISO-8859-1", transliterate_to = "ASCII//TRANSLIT//IGNORE")
  @filename = File.expand_path(filename)
  @lastline = nil
  @lines = []
  @transliterator = Iconv.new(transliterate_to, transliterate_from)
end

Class Method Details

.open(filename, starting_at = 0, &block) ⇒ Object



9
10
11
# File 'lib/pokerstats/pokerstars_file.rb', line 9

def self.open(filename, starting_at = 0, &block)
  new(filename, starting_at).open(starting_at, &block)
end

Instance Method Details

#closeObject



90
91
92
# File 'lib/pokerstats/pokerstars_file.rb', line 90

def close
  @file.close unless @file.closed?
end

#closed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/pokerstats/pokerstars_file.rb', line 37

def closed?
  @file.closed?
end

#each {|next_handrecord| ... } ⇒ Object

Yields:



66
67
68
69
# File 'lib/pokerstats/pokerstars_file.rb', line 66

def each
  yield next_handrecord
  yield next_handrecord until @lastline.nil?
end

#eof?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/pokerstats/pokerstars_file.rb', line 56

def eof?
  @lastline.nil?
end

#first(starting_at = 0) ⇒ Object



60
61
62
63
64
# File 'lib/pokerstats/pokerstars_file.rb', line 60

def first(starting_at = 0)
  open(starting_at) do
    return next_handrecord
  end
end

#next_handrecordObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pokerstats/pokerstars_file.rb', line 71

def next_handrecord
  starting_at = pos
  until @file.eof?
    @lastline = read_and_chomp_next_line_from_file
    break if @lastline =~ POKERSTARS_HEADER_PATTERN
    @lines << @lastline unless @lastline.empty?
  end
  result, @lines = HandHistory.new(@lines, @filename, starting_at), [@lastline]
  if @file.eof?
    @lastline = nil
    @index_of_last_header = nil
    @lines = []
  else
    @index_of_last_header = @file.pos-@lastline.size-1
    @lines = [@lastline]
  end
  result
end

#open(starting_at = 0) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pokerstats/pokerstars_file.rb', line 25

def open(starting_at = 0)
  open_file_and_verify_first_line(starting_at)
  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
  self
end

#open_file_and_verify_first_line(starting_at = 0) ⇒ Object



20
21
22
23
# File 'lib/pokerstats/pokerstars_file.rb', line 20

def open_file_and_verify_first_line(starting_at = 0)
  @file = File.open(@filename, "r")
  self.pos=starting_at
end

#posObject



41
42
43
44
# File 'lib/pokerstats/pokerstars_file.rb', line 41

def pos
  return @file.pos if @lastline.nil?
  @file.pos - @lastline.size - 1
end

#pos=(index) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/pokerstats/pokerstars_file.rb', line 46

def pos=(index)
  @file.pos=index unless pos == index
  @lastline = read_and_chomp_next_line_from_file
  unless @lastline && @lastline =~ POKERSTARS_HEADER_PATTERN
    close
    raise "hand record must begin with a valid header line"
  end
  @lines = [@lastline]
end