CSVラッパー作りました

CSVのラッパー作りました。
 

require 'csv'

class CsvAccess
  def initialize(file_name=File.dirname(__FILE__) + "./hoge.csv")
    @file_name = file_name
    init
  end

  def get_table
    @table.clone
  end

  def set_table(set_datas)
    table = get_table()
    (0..@first_row_datas.length-1).each do |i|
      table[@first_row_datas[i]] = set_datas[i]
    end
    table
  end

  def select_all
    row_datas = CSV.readlines(@file_name)
    res_array = []
    (1..row_datas.length-1).each do |i|
      res_array << set_table(row_datas[i]) unless row_datas[i][0] == nil
    end
    res_array
  end

  def select(key, col_name)
    row_datas = CSV.readlines(@file_name)
    res_array = []
    col_num =  @first_row_datas.rindex(col_name)
    return res_array unless col_num
    (1..row_datas.length-1).each do |i|
      res_array << set_table(row_datas[i]) if row_datas[i][col_num] == key
    end
    res_array
  end

  def insert(data)
    regist_datas = create_csv_array(data)
    row_datas = CSV.readlines(@file_name)
    row_datas << regist_datas
    CSV.open(@file_name, 'w') do |writer|
      (0..row_datas.length-1).each do |i|
        writer << row_datas[i]
      end
    end
  end

  def update(data, key, col_name, default = 'none')
    col_num =  @first_row_datas.rindex(col_name)
    return unless col_num
    regist_datas = create_csv_array(data)
    row_datas = CSV.readlines(@file_name)
    CSV.open(@file_name, 'w') do |writer|
      (0..row_datas.length-1).each do |i|
        if row_datas[i][col_num] == key
          (0..row_datas[i].length-1).each do |j|
            regist_datas[j] = row_datas[i][j] unless regist_datas[j] and regist_datas[j] != default
          end
          writer << regist_datas
        else
          writer << row_datas[i]
        end
      end
    end
  end

  def delete(data)
    delete_datas = create_csv_array(data)
    row_datas = CSV.readlines(@file_name)
    CSV.open(@file_name, 'w') do |writer|
      (0..row_datas.length-1).each do |i|
        writer << row_datas[i] unless row_datas[i] == delete_datas
      end
    end
  end

  def delete_key(key, col_name)
    col_num =  @first_row_datas.rindex(col_name)
    return unless col_num
    row_datas = CSV.readlines(@file_name)
    CSV.open(@file_name, 'w') do |writer|
      (0..row_datas.length-1).each do |i|
        writer << row_datas[i] unless row_datas[i][col_num] == key
      end
    end
  end

  private
  def create_csv_array(table)
    res_datas = []
    (0..@first_row_datas.length-1).each do |i|
      res_datas << table[@first_row_datas[i]]
    end
    res_datas
  end

  def init
    @first_row_datas = CSV.readlines(@file_name)[0]
    @row_count = @first_row_datas.length
    (0..@first_row_datas.length-1).each do |i|
      @first_row_datas[i] = @first_row_datas[i].gsub(/\n\z/, "")
    end
    @table = create_table
  end

  def create_table
    table = {}
    code = ""
    @first_row_datas.each do |i|
      code += "def #{i};  self[\"#{i}\"];  end; def #{i}=(val);  self[\"#{i}\"] = val; end;"
    end
    table.instance_eval(code)
    table
  end
end

こんな感じのCSVファイルだったら。。。

id,name,hoge
1,fugafuga,hogehoge

 
こんな感じで使えます。
 
登録

csv = CsvAccess.new(File.dirname(__FILE__) + "./hoge.csv")
table = csv.get_table
table.id = 2
table.name = "ふがふが!"
table.hoge = "ほげほげ!"
csv.insert(table)

 
照会

csv = CsvAccess.new(File.dirname(__FILE__) + "./hoge.csv")
select_result = csv.select("fugafuga", "name")
=> [{"id"=>1, "name"=>"fugafuga", "hoge"=>"hogehoge"}]
select_result[0].id
=> 1

とまぁこんな感じです。
CSVの一行目が列名になってて、
.idみたいな感じでアクセスしたかったので作りました。
ちなみに列名がHashのメソッドかぶってるとひどいことになりそうですね。。。