class RsHighscores::Stats

Constants

ActualStatCount
ExpectedStatCount
Skills

Attributes

raw_stats[RW]
stats[RW]

Public Class Methods

new(raw_stats) click to toggle source
# File lib/rshighscores/stats.rb, line 16
def initialize raw_stats
  @raw_stats = raw_stats

  parse_stats
end

Public Instance Methods

[](skill) click to toggle source
# File lib/rshighscores/stats.rb, line 40
def [] skill
  skill = skill.to_s.capitalize
  raise "non-existant skill lookup" unless self.class::Skills.index(skill)

  stats[self.class::Skills.index(skill)]
end
method_missing(name, *args) click to toggle source
# File lib/rshighscores/stats.rb, line 47
def method_missing name, *args
  return self[name] if Skills.include? name.to_s.capitalize
  raise NoMethodError
end
parse_stats() click to toggle source
# File lib/rshighscores/stats.rb, line 28
def parse_stats
  validate_raw_stats

  @stats = []
  @raw_stats.take(self.class::ActualStatCount).each do |line|
    raise "malformed raw stats" unless line =~ /\d+,\d+,\d+/
    stat = Stat.new line.split(",").map(&:to_i)

    @stats << stat
  end
end
validate_raw_stats() click to toggle source
# File lib/rshighscores/stats.rb, line 22
def validate_raw_stats
  unless @raw_stats.length == self.class::ExpectedStatCount
    raise "incorrect input length: expected #{self.class::ExpectedStatCount}, got #{@raw_stats.length}"
  end
end