Class: Multisync::RsyncStat

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

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ RsyncStat

Returns a new instance of RsyncStat.



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

def initialize output
  @output = output
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



42
43
44
45
46
# File 'lib/multisync/rsync_stat.rb', line 42

def method_missing name
  key = name.to_sym
  return @stats[key] if @stats.keys.include? key
  super
end

Instance Method Details

#definitionsObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/multisync/rsync_stat.rb', line 48

def definitions
  [
    { key: :files, match: 'Number of files', coerce: ->(x) { x.gsub(',',"'") }, default: '0' },
    { key: :created, match: 'Number of created files', coerce: ->(x) { x.gsub(',',"'") }, default: '0' },
    { key: :deleted, match: 'Number of deleted files', coerce: ->(x) { x.gsub(',',"'") }, default: '0' },
    { key: :transferred, match: 'Number of regular files transferred', coerce: ->(x) { x.gsub(',',"'") }, default: '0' },
    { key: :file_size, match: 'Total file size', coerce: ->(x) { Filesize.new(x.gsub(',','').to_i).pretty }, default: '0 B' },
    { key: :transferred_size, match: 'Total transferred file size', coerce: ->(x) { Filesize.new(x.gsub(',','').to_i).pretty }, default: '0 B' },
  ]
end

#parseObject

Build an internal hash with normalized stats



11
12
13
14
15
16
17
# File 'lib/multisync/rsync_stat.rb', line 11

def parse
  @stats = definitions.each_with_object({}) do |definition, stats|
    value = scan[definition[:match]]
    stats[definition[:key]] = value ? (definition[:coerce] ? definition[:coerce].call(value) : value) : definition[:default]
  end
  self
end

#scanObject

Scan output and return a hash

{
  "Number of files" => "35,648",
  "Number of created files" => "0",
  "Number of deleted files" => "0",
  "Number of regular files transferred"=>"0",
  ...
}


27
28
29
# File 'lib/multisync/rsync_stat.rb', line 27

def scan
  @scan ||= @output.scan(/(#{definitions.map{|d| d[:match] }.join('|')}):\s+([,0-9]+)/).each_with_object({}) {|(k,v), o| o[k] = v }
end

#to_aObject



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

def to_a
  [
    @stats[:files],
    @stats[:created],
    @stats[:deleted],
    @stats[:transferred],
    @stats[:file_size],
    @stats[:transferred_size],
  ]
end