Class: Multisync::Summary

Inherits:
Object
  • Object
show all
Defined in:
lib/multisync/summary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks) ⇒ Summary

Returns a new instance of Summary.



6
7
8
# File 'lib/multisync/summary.rb', line 6

def initialize tasks
  @tasks = tasks
end

Instance Attribute Details

#tasksObject (readonly)

All tasks to include in the summary



4
5
6
# File 'lib/multisync/summary.rb', line 4

def tasks
  @tasks
end

Instance Method Details

#dataObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/multisync/summary.rb', line 22

def data
  tasks.map do |task|
    result = task.result
    desc = [task.source_description, "--> #{task.destination_description}"]

    case result[:action]
    when :run
      if result[:status] && result[:status].success?
        # successfull run
        stats = Multisync::RsyncStat.new(result[:stdout])
        [*desc, *stats.formatted_values.map{|e| {value: e.color(:green), alignment: :right} } ]
      else
        # failed or interrupted run
        [*desc, { value: (result[:stderr] || 'n/a').strip.color(:red), colspan: 6 } ]
      end

    when :skip
      # skiped sync
      [*desc, { value: result[:skip_message].color(:yellow), colspan: 6 } ]

    else
      # not executed
      [*desc, { value: 'not executed'.faint, colspan: 6 } ]
    end
  end.push ["Total".faint, "", *Multisync::RsyncStat.formatted_totals.map{|e| {value: e.faint, alignment: :right} } ]
end

#headingsObject



18
19
20
# File 'lib/multisync/summary.rb', line 18

def headings
  %w( Source Destination Files + -    ).zip(%i( left left right right right right right right )).map{|v,a| {value: v, alignment: a} }
end

#tableObject



14
15
16
# File 'lib/multisync/summary.rb', line 14

def table
  Terminal::Table.new(headings: headings, rows: data, style: table_style)
end

#table_styleObject



49
50
51
# File 'lib/multisync/summary.rb', line 49

def table_style
  { border_top: false,  border_bottom: false, border_x: '', border_y: '', border_i: '', padding_left: 0, padding_right: 3 }
end

#to_sObject



10
11
12
# File 'lib/multisync/summary.rb', line 10

def to_s
  table.to_s
end