读书人

使用 Ruby 解析CSV资料amp;YAML文件

发布时间: 2013-10-10 14:14:51 作者: rapoo

使用 Ruby 解析CSV文件&YAML文件
解析CSV

使用 Ruby 解析CSV资料&YAML文件


使用 Ruby 解析CSV资料&YAML文件

haml (students.csv文件位置在在public/csv_demo/students.csv)


modles

def self.import_from_yaml(file)    text = file.read    text = text.gb2312_to_utf8 if !text.utf8?    hash = YAML.load(text)    ActiveRecord::Base.transaction do      categories = _import_from_yaml_by_hash(hash)      categories.each{|c|c.move_to_root}    end  end  def self._import_from_yaml_by_hash(child_hash)    return [] if child_hash.blank?    child_hash.map do |parent_name,child_name_hash|      categories = self._import_from_yaml_by_hash(child_name_hash)      parent_category = Category.create(:name=>parent_name)      categories.each{|c|c.move_to_child_of(parent_category)}      parent_category    end  end





读书人网 >Ruby Rails

热点推荐