Class: RsHighscores::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/rshighscores/stats.rb

Direct Known Subclasses

OldSchool::Stats

Constant Summary collapse

Skills =
%w(Overall Attack Defence Strength
Hitpoints Ranged Prayer Magic
Cooking Woodcutting Fletching Fishing
Firemaking Crafting Smithing Mining
Herblore Agility Thieving Slayer
Farming Runecrafting Hunter Construction
Summoning Dungeoneering Divination)
ExpectedStatCount =
44
ActualStatCount =
27

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_stats) ⇒ Stats

Returns a new instance of Stats.



16
17
18
19
20
# File 'lib/rshighscores/stats.rb', line 16

def initialize raw_stats
  @raw_stats = raw_stats

  parse_stats
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Raises:

  • (NoMethodError)


45
46
47
48
# File 'lib/rshighscores/stats.rb', line 45

def method_missing name, *args
  return self[name] if Skills.include? name.to_s.capitalize
  raise NoMethodError
end

Instance Attribute Details

#raw_statsObject

Returns the value of attribute raw_stats.



14
15
16
# File 'lib/rshighscores/stats.rb', line 14

def raw_stats
  @raw_stats
end

#statsObject

Returns the value of attribute stats.



14
15
16
# File 'lib/rshighscores/stats.rb', line 14

def stats
  @stats
end

Instance Method Details

#[](skill) ⇒ Object



38
39
40
41
42
43
# File 'lib/rshighscores/stats.rb', line 38

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

#parse_statsObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rshighscores/stats.rb', line 26

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_statsObject



22
23
24
# File 'lib/rshighscores/stats.rb', line 22

def validate_raw_stats
  raise "incorrect input length" unless @raw_stats.length == self.class::ExpectedStatCount
end