Class: CommaPile::Report

Inherits:
Hash
  • Object
show all
Defined in:
lib/comma_pile/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Report

Returns a new instance of Report.



13
14
15
16
17
# File 'lib/comma_pile/report.rb', line 13

def initialize(config)
  @config = config
  @results = {}
  super(@results)
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



11
12
13
# File 'lib/comma_pile/report.rb', line 11

def results
  @results
end

Instance Method Details

#generate!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/comma_pile/report.rb', line 43

def generate!
  if output
    CSV.open(output.sub(/\.(\w+)$/, '.raw.\1'), 'w+') do |raw_csv|
      @raw_csv = raw_csv
      CSV.open(output, 'w+') do |parsed_csv|
        @parsed_csv = parsed_csv
        @parsed_csv << field_names.collect {|f| f.to_s }
        process_input
      end
    end
  else
    process_input
  end
end

#summary(entry = nil, parent_keys = []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/comma_pile/report.rb', line 20

def summary(entry = nil, parent_keys = [])
  collector = ''
  (entry || @results).each do |key, value|
    if value.nil? || value.empty?
      cells = [value.counter] + parent_keys + [key] + value.sum.values
      collector += CSV.generate_line(cells.flatten)
    else
      collector += summary(value, parent_keys + [key])
    end
  end
  collector
end

#to_stdout(index = 0, entry = nil) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/comma_pile/report.rb', line 33

def to_stdout(index = 0, entry = nil)
  (entry || @results).each do |key, value|
    puts "#{"\t" * index}#{key}: #{value.counter}"
    if value.respond_to?(:each)
      to_stdout(index + 1, value) if value && !value.empty?
    end
  end
end