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.



20
21
22
23
24
# File 'lib/sinew/output.rb', line 20

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

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



18
19
20
# File 'lib/sinew/output.rb', line 18

def columns
  @columns
end

#csvObject (readonly)

Returns the value of attribute csv.



18
19
20
# File 'lib/sinew/output.rb', line 18

def csv
  @csv
end

#rowsObject (readonly)

Returns the value of attribute rows.



18
19
20
# File 'lib/sinew/output.rb', line 18

def rows
  @rows
end

#sinewObject (readonly)

Returns the value of attribute sinew.



18
19
20
# File 'lib/sinew/output.rb', line 18

def sinew
  @sinew
end

#urlsObject (readonly)

Returns the value of attribute urls.



18
19
20
# File 'lib/sinew/output.rb', line 18

def urls
  @urls
end

Instance Method Details

#countObject



72
73
74
# File 'lib/sinew/output.rb', line 72

def count
  rows.length
end

#emit(row) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sinew/output.rb', line 49

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



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sinew/output.rb', line 26

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/sinew/output.rb', line 38

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sinew/output.rb', line 76

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