Sha256: 4642f5264dacfd0d998207c6c8a76cd5b9c9276c8b7df0ef6950ae5d3e464aeb

Contents?: true

Size: 1.05 KB

Versions: 20

Compression:

Stored size: 1.05 KB

Contents

namespace :csv do

  task :require do
    require "csv"
    require "yaml"
  end

  # csv_to_smc sample task.
  task :update_samples => [:require] do

    # By default, csv rows are saved as array.
    csv_to_smc("_smc/samples.csv") {|data,rownum| "samples/#{"%04d" % rownum}.html.smc" }

    # You can create hash using label_row/skip_rows parameters.
    # csv_to_smc("_smc/samples.csv", 1, 0) {|data,rownum| "samples/#{"%04d" % rownum}.html.smc" }

  end

end


def csv_to_smc(csv_file, skip_rows = nil, label_row = nil)
  labels = nil
  skip_rows ||= 0
  label_row ||= -1

  rownum = -1
  CSV.foreach(csv_file) do |row|
    rownum += 1
    labels = row.map{|cell| cell.to_s } if label_row == rownum
    next if rownum < skip_rows

    if labels
      data = {}
      labels.each_with_index do |label,i|
        data[label] = row[i].to_s
      end
    else
      data = row.map {|cell| cell.to_s }
    end
    if block_given?
      fname = yield(data,rownum)
    else
      fname = data[0]
    end
    File.open(fname, "w") do |io|
      io << data.to_yaml
    end
  end

end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
smallcage-0.3.2 project/rake/_smc/rakelib/csv.rake
smallcage-0.3.1 project/rake/_smc/rakelib/csv.rake
smallcage-0.3.0 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.9 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.8 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.7 project/rake/_smc/rakelib/csv.rake
bluemark-smallcage-0.1.5 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.6 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.5 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.4 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.3 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.2 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.1 project/rake/_smc/rakelib/csv.rake
smallcage-0.2.0 project/rake/_smc/rakelib/csv.rake
smallcage-0.1.9 project/rake/_smc/rakelib/csv.rake
smallcage-0.1.8 project/rake/_smc/rakelib/csv.rake
smallcage-0.1.7 project/rake/_smc/rakelib/csv.rake
smallcage-0.1.6 project/rake/_smc/rakelib/csv.rake
smallcage-0.1.5 project/rake/_smc/rakelib/csv.rake
smallcage-0.1.4 project/rake/_smc/rakelib/csv.rake