Class: ET::Lesson

Inherits:
Object
  • Object
show all
Defined in:
lib/et/lesson.rb

Direct Known Subclasses

Challenge, Exercise

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cwd) ⇒ Lesson

Returns a new instance of Lesson.



9
10
11
# File 'lib/et/lesson.rb', line 9

def initialize(cwd)
  @cwd = cwd
end

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



7
8
9
# File 'lib/et/lesson.rb', line 7

def cwd
  @cwd
end

Instance Method Details

#archive!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/et/lesson.rb', line 13

def archive!
  if exists?
    filepath = random_archive_path

    File.open(filepath, "wb") do |file|
      Zlib::GzipWriter.wrap(file) do |gz|
        Gem::Package::TarWriter.new(gz) do |tar|
          ET::SubmissionFileList.new(dir).each do |file|
            relative_path = file
            absolute_path = File.join(dir, file)

            if FileTest.directory?(absolute_path)
              tar.mkdir(relative_path, 0755)
            else
              file_contents = File.read(absolute_path)
              tar.add_file_simple("./" + relative_path, 0555, file_contents.bytesize) do |io|
                io.write(file_contents)
              end
            end
          end
        end
      end
    end
    filepath
  else
    nil
  end
end

#dirObject



42
43
44
# File 'lib/et/lesson.rb', line 42

def dir
  @dir ||= find_lesson_dir(cwd)
end

#exists?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/et/lesson.rb', line 50

def exists?
  !dir.nil?
end

#slugObject



46
47
48
# File 'lib/et/lesson.rb', line 46

def slug
  File.basename(dir)
end