Class: Backup

Inherits:
Object
  • Object
show all
Defined in:
app/models/backup.rb

Defined Under Namespace

Classes: Entry, ResponseOutWriter

Constant Summary collapse

ALLOWED_EXTENSIONS =
[:tgz, :zip]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extension) ⇒ Backup

Returns a new instance of Backup.



4
5
6
7
# File 'app/models/backup.rb', line 4

def initialize(extension)
  raise "Invalid extension '#{extension}'" unless ALLOWED_EXTENSIONS.include? extension.to_sym
  @extension = extension.to_sym
end

Class Method Details

.eachObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/backup.rb', line 58

def each
  Cartoonist::Backup.all.each do |key, proc|
    proc.call.find_each do |value|
      if value.respond_to? :to_backup_entries
        value.to_backup_entries.each do |entry|
          yield entry.with_key(key)
        end
      else
        yield Backup::Entry.from_record(value).with_key(key)
      end
    end
  end
end

Instance Method Details

#content_dispositionObject



9
10
11
# File 'app/models/backup.rb', line 9

def content_disposition
  %{attachment; filename="#{filename}"}
end

#filenameObject



13
14
15
16
# File 'app/models/backup.rb', line 13

def filename
  prefix = "dev-" unless Rails.env.production?
  "#{prefix}cartoonist-backup-#{Time.now.strftime("%Y-%m-%d_%H%M%S")}.#{@extension}"
end

#response_bodyObject



18
19
20
21
22
# File 'app/models/backup.rb', line 18

def response_body
  Enumerator.new do |out|
    send @extension, out
  end
end

#tgz(out) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/backup.rb', line 35

def tgz(out)
  gzip = Zlib::GzipWriter.new Backup::ResponseOutWriter.new(out)

  begin
    Archive::Tar::Minitar::Writer.open gzip do |tar|
      Backup.each do |entry|
        tar.add_file_simple entry.path, :mode => 0644, :size => entry.content.length do |output|
          output.write entry.content
        end
      end
    end
  ensure
    gzip.close
  end
end

#zip(out) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'app/models/backup.rb', line 24

def zip(out)
  buffer = Zip::ZipOutputStream.write_buffer do |zos|
    Backup.each do |entry|
      zos.put_next_entry entry.path
      zos.write entry.content
    end
  end

  out << buffer.string
end