Class: Moodle2CC::Moodle::Backup

Inherits:
Object
  • Object
show all
Includes:
HappyMapper
Defined in:
lib/moodle2cc/moodle/backup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backup_pathObject

Returns the value of attribute backup_path.



7
8
9
# File 'lib/moodle2cc/moodle/backup.rb', line 7

def backup_path
  @backup_path
end

#filesObject

Returns the value of attribute files.



7
8
9
# File 'lib/moodle2cc/moodle/backup.rb', line 7

def files
  @files
end

Class Method Details

.read(backup_path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/moodle2cc/moodle/backup.rb', line 13

def self.read(backup_path)
  xml = nil
  files = nil
  if File.directory?(backup_path)
    xml = File.read(File.join(backup_path, "moodle.xml"))
    files = Dir["#{backup_path}/**/*"].reject { |e| File.directory?(e) }.
        map { |e| e.sub("#{backup_path}/", '') }.select { |e| e =~ /^course_files/ }.
        map { |e| e.sub('course_files/', '') }.sort
  else
    Zip::File.open(backup_path) do |zipfile|
      xml = zipfile.read("moodle.xml")
      files = zipfile.entries.select { |e| e.name =~ /^course_files/ && !e.directory? }.
        map { |e| e.name.sub('course_files/', '') }.sort
    end
  end
  backup = parse(xml)
  backup.backup_path = backup_path
  backup.files = files
  backup
end

Instance Method Details

#copy_files_to(dir) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/moodle2cc/moodle/backup.rb', line 34

def copy_files_to(dir)
  if File.directory?(@backup_path)
    @files.each do |file|
      destination_file = File.join(dir, file)
      FileUtils.mkdir_p(File.dirname(destination_file))
      File.open(destination_file, 'wb') do |f|
        f.write File.read(File.join(@backup_path, "course_files/#{file}"))
      end
    end
  else
    Zip::File.open(@backup_path) do |zipfile|
      @files.each do |file|
        destination_file = File.join(dir, file)
        FileUtils.mkdir_p(File.dirname(destination_file))
        File.open(destination_file, 'wb') do |f|
          f.write zipfile.read("course_files/#{file}")
        end
      end
    end
  end
end