Class: Sinew::Output

Inherits:
Object show all
Defined in:
lib/sinew/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sinew) ⇒ Output

Returns a new instance of Output.



13
14
15
16
17
# File 'lib/sinew/output.rb', line 13

def initialize(sinew)
  @sinew = sinew
  @rows = []
  @urls = Set.new
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



11
12
13
# File 'lib/sinew/output.rb', line 11

def columns
  @columns
end

#csvObject (readonly)

Returns the value of attribute csv.



11
12
13
# File 'lib/sinew/output.rb', line 11

def csv
  @csv
end

#rowsObject (readonly)

Returns the value of attribute rows.



11
12
13
# File 'lib/sinew/output.rb', line 11

def rows
  @rows
end

#sinewObject (readonly)

Returns the value of attribute sinew.



11
12
13
# File 'lib/sinew/output.rb', line 11

def sinew
  @sinew
end

#urlsObject (readonly)

Returns the value of attribute urls.



11
12
13
# File 'lib/sinew/output.rb', line 11

def urls
  @urls
end

Instance Method Details

#countObject



66
67
68
# File 'lib/sinew/output.rb', line 66

def count
  rows.length
end

#emit(row) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sinew/output.rb', line 42

def emit(row)
  # implicit header if necessary
  header(row.keys) if !csv

  # don't allow duplicate urls
  return if dup_url?(row)

  rows << row.dup

  # map columns to row, and normalize along the way
  print = {}
  row = columns.map do |i|
    value = normalize(row[i])
    print[i] = value if value.present?
    value
  end

  # print
  sinew.vputs print.ai

  csv << row
  csv.flush
end

#filenameObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sinew/output.rb', line 19

def filename
  @filename ||= begin
    recipe = sinew.options[:recipe]
    ext = File.extname(recipe)
    if ext.empty?
      "#{recipe}.csv"
    else
      recipe.gsub(ext, '.csv')
    end
  end
end

#header(columns) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/sinew/output.rb', line 31

def header(columns)
  sinew.banner("Writing to #{filename}...") if !sinew.quiet?

  columns = columns.flatten
  @columns = columns

  # open csv, write header row
  @csv = CSV.open(filename, 'wb')
  csv << columns
end

#reportObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sinew/output.rb', line 70

def report
  return if count == 0

  sinew.banner("Got #{count} rows.")

  # calculate counts
  counts = Hash.new(0)
  rows.each do |row|
    row.each_pair { |k, v| counts[k] += 1 if v.present? }
  end
  # sort by counts
  cols = columns.sort_by { |i| [ -counts[i], i ] }

  # report
  len = cols.map { |i| i.to_s.length }.max
  fmt = "  %-#{len + 1}s %7d / %-7d %6.1f%%\n"
  cols.each do |col|
    $stderr.printf(fmt, col, counts[col], count, counts[col] * 100.0 / count)
  end
end