Class: Records
- Inherits:
-
Array
- Object
- Array
- Records
- Defined in:
- lib/records.rb
Overview
A little wrapper to handle hiscore records
Constant Summary collapse
- FILENAME =
File.join(ENV['HOME'] || ENV['USERPROFILE'], ".pentix")
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(*args) ⇒ Records
constructor
A new instance of Records.
- #last_name ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(*args) ⇒ Records
Returns a new instance of Records.
25 26 27 28 29 30 31 32 33 |
# File 'lib/records.rb', line 25 def initialize(*args) super *args File.read(FILENAME).split("\n").each do |line| if match = line.match(/^\s*(.*?)\s+(\d+)\s*$/) self << [@last_name = match[1].strip, match[2].to_i] end end if File.exists?(FILENAME) end |
Class Method Details
.add(name, score) ⇒ Object
15 16 17 18 19 |
# File 'lib/records.rb', line 15 def self.add(name, score) list = self.new list << [name.strip, score] list.save end |
.last_name ⇒ Object
21 22 23 |
# File 'lib/records.rb', line 21 def self.last_name self.new.last_name end |
.new ⇒ Object
7 8 9 |
# File 'lib/records.rb', line 7 def self.new @@instance ||= super # no need to bother HDD all the time end |
.top(size) ⇒ Object
11 12 13 |
# File 'lib/records.rb', line 11 def self.top(size) new.sort{ |a, b| b[1] <=> a[1] }.uniq.slice(0, size) end |
Instance Method Details
#last_name ⇒ Object
45 46 47 |
# File 'lib/records.rb', line 45 def last_name @last_name || '' end |
#save ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/records.rb', line 35 def save File.open(FILENAME, "w") do |file| each do |entry| if entry[1] > 0 file.write "#{entry[0].ljust(40, ' ')} #{entry[1]}\n" end end end end |