class Card attr :no attr :category attr :rarity attr :name attr :sub_title attr :power attr :ds attr :ps attr :advance attr :job attr :illustrator attr :timing attr :text attr :flavor_text def initialize(line) @no = line[0] @rarity = line[1].nil? ? 0 : line[1].size / 2 @illustrator = line[2] @name = line[3] @category = line[4] @sub_title = line[5] @power = line[6].nil? ? nil : line[6].to_i @advance = line[7].nil? ? [] : line[7].split(/ /) @ds = line[8].nil? ? nil : line[8].to_i @ps = line[9].nil? ? nil : line[9].to_i @timing = line[10] @job = line[11].nil? ? [] : line[11].split(/ /) @text = line[12] @flavor_text = line[13] end end class Cards include Enumerable @data = [] attr_accessor :sort_key def initialize path = File.dirname(__FILE__) file = path + '/hayate_tcg.csv' cache = path + '/cache/hayate_tcg.csv.cache' if File.exists?(cache) && File.mtime(file) <= File.mtime(cache) get_cache(cache) else load_csv(file) set_cache(cache) end end def set_sort_key(key) @sort_key = key end def each i = 0 @data.sort_by do |x| [((@sort_key+"").size == 0 || x.__send__(@sort_key).nil?) ? 0 : x.__send__(@sort_key), i += 1] end.each{|x| yield x} end def [](key) @data.find(){|card| card.no == key} end private def load_csv(file) require 'csv' require 'kconv' data = CSV.open(file, 'r') data.shift @data = data.map do |line| Card.new(line.map{|x| x.nil? ? nil : x.toeuc}) end end def set_cache(file) require 'pstore' db = PStore.new(file) db.transaction do db["cards"] = @data db["last_modified"] = Time.now end end def get_cache(file) require 'pstore' db = PStore.new(file) db.transaction do @data = db["cards"] end end end