Class: Pangrid::CSV

Inherits:
Plugin show all
Defined in:
lib/pangrid/plugins/csv.rb

Constant Summary collapse

DESCRIPTION =
"CSV reader (see source comments for format)"

Constants inherited from Plugin

Plugin::FAILED, Plugin::MISSING_DEPS, Plugin::REGISTRY

Instance Method Summary collapse

Methods inherited from Plugin

class_to_name, get, inherited, list_all, load_all, load_plugin

Methods included from PluginUtils

#check

Instance Method Details

#read(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pangrid/plugins/csv.rb', line 32

def read(data)
  s = ::CSV.parse(data)
  s.reject! {|row| row.compact.empty?}
  while s[0] && s[0][0] !~ /^Width:/i do
    s.shift
  end
  check("No header row found. Header needs a 'Width: ' cell in the first column.") { !s.empty? }
  xw = XWord.new
  h = s.shift.map {|c| c.split(/:\s*/)}
  header = OpenStruct.new
  h.each do |k, v|
    header[k.downcase.strip] = v
  end

  header.clues = header.clues.to_i
  xw.width = header.width.to_i
  xw.height = header.height.to_i
  xw.solution = []
  xw.height.times do
    row = s.shift
    check("Row does not have #{xw.width} cells: \n" +
          row.join(',')) { row.length == xw.width }
    xw.solution << row.map do |c|
      cell = Cell.new
      if c == header.black
        cell.solution = :black
      elsif c == header.empty or c == nil
        cell.solution = :null
      else
        cell.solution = c.gsub /[^[:alpha:]]/, ''
      end
      cell
    end
  end

  xw.clues = []
  s.each do |row|
    if row[header.clues]
      xw.clues << row[header.clues]
    end
  end
  unpack_clues(xw)
  xw
end