Class: CSVDiff::Report

Inherits:
Object
  • Object
show all
Includes:
Excel, Html, Text
Defined in:
lib/csv-diff-report/report.rb

Overview

Defines a class for generating diff reports using CSVDiff.

A diff report may contain multiple file diffs, and can be output as either an XLSX spreadsheet document, or an HTML file.

Instance Method Summary collapse

Constructor Details

#initialize {|*out| ... } ⇒ Report

Instantiate a new diff report object. Takes an optional block callback to use for handling the output generated by the diff process. If no callback is supplied, this output will be sent to the console using ColorConsole.

Yields:

  • (*out)

    If supplied, the block passed to this method will be called for each line of text to be output. The argument to the block will be an array of text chunks, each of which may be accompanied by optional foreground and background colours.



28
29
30
31
# File 'lib/csv-diff-report/report.rb', line 28

def initialize(&block)
    @diffs = []
    @echo_handler = block
end

Instance Method Details

#<<(diff) ⇒ Object

Add a CSVDiff object to this report.



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
# File 'lib/csv-diff-report/report.rb', line 47

def <<(diff)
    if diff.is_a?(CSVDiff)
        @diffs << diff
        unless @left
            @left = Pathname.new(diff.left.path)
            @right = Pathname.new(diff.right.path)
        end
        diff.diff_warnings.each{ |warn| echo [warn, :yellow] }
        out = []
        out << ["Found #{diff.diffs.size} differences"]
        diff.summary.each_with_index.map do |pair, i|
            out << [i == 0 ? ": " : ", "]
            k, v = pair
            color = case k
                    when 'Add' then :light_green
                    when 'Delete' then :red
                    when 'Update' then :cyan
                    when 'Move' then :light_magenta
                    when 'Warning' then :yellow
                    end
            out << ["#{v} #{k}s", color]
        end
        echo(*out)
    else
        raise ArgumentError, "Only CSVDiff objects can be added to a CSVDiff::Report"
    end
end

#diff(left, right, options = {}) ⇒ Object

Add a diff to the diff report.

Parameters:

  • options (Hash) (defaults to: {})

    Options to be passed to the diff process.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/csv-diff-report/report.rb', line 79

def diff(left, right, options = {})
    @left = Pathname.new(left)
    @right = Pathname.new(right)
    if @left.file? && @right.file?
        echo "Performing file diff:"
        echo "  From File:    #{@left}"
        echo "  To File:      #{@right}"
        opt_file = load_opt_file(options.fetch(:options_file, @left.dirname))
        diff_file(@left.to_s, @right.to_s, options, opt_file)
    elsif @left.directory? && @right.directory?
        echo "Performing directory diff:"
        echo "  From directory:  #{@left}"
        echo "  To directory:    #{@right}"
        opt_file = load_opt_file(options.fetch(:options_file, @left))
        if fts = options[:file_types]
            file_types = find_matching_file_types(fts, opt_file)
            file_types.each do |file_type|
                hsh = opt_file[:file_types][file_type]
                ft_opts = options.merge(hsh)
                diff_dir(@left, @right, ft_opts, opt_file)
            end
        else
            diff_dir(@left, @right, options, opt_file)
        end
    else
        echo ["From path '#{@left}' not found", :red] unless @left.exist?
        echo ["To path '#{@right}' not found", :red] unless @right.exist?
        raise ArgumentError, "Left and right must both exist and be of the same type (files or directories)"
    end
end

#echo(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/csv-diff-report/report.rb', line 34

def echo(*args)
    if @echo_handler
        @echo_handler.call(*args)
    else
        args.each do |out|
            Console.write(*out)
        end
        Console.puts
    end
end

#output(path, format = :html) ⇒ Object

Saves a diff report to path in format.

Parameters:

  • path (String)

    The path to the output report.

  • format (Symbol) (defaults to: :html)

    The output format for the report; one of :html or :xlsx.



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/csv-diff-report/report.rb', line 116

def output(path, format = :html)
    path = case
    when format.to_s =~ /^xlsx?$/i || File.extname(path) =~ /xlsx?$/i
        xl_output(path)
    when format.to_s =~ /^html$/i || File.extname(path) =~ /html$/i
        html_output(path)
    when format.to_s =~ /^(te?xt|csv)$/i || File.extname(path) =~ /(csv|txt)$/i
        text_output(path)
    else
        raise ArgumentError, "Unrecognised output format: #{format}"
    end
    echo "Diff report saved to '#{path}'"
end