Class: Temp::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/temp/project.rb

Overview

A Project object represents a project and provides a method for copying the files from a template to create a project.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, template) ⇒ Project

Initialize all the required information for creating the project and make sure the project doesn’t already exist



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/temp/project.rb', line 14

def initialize(path, template)
  @path = File.expand_path(path)
  @template = template
  if File.file? @path
    raise Temp::Exceptions::ProjectExistsError
  elsif File.directory? @path
    @name = @template.filename
    @path = File.join(@path, @name)
    raise Temp::Exceptions::ProjectExistsError if File.exist? @path
  else
    @name = File.basename(@path)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/temp/project.rb', line 10

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/temp/project.rb', line 10

def path
  @path
end

#templateObject (readonly)

Returns the value of attribute template.



10
11
12
# File 'lib/temp/project.rb', line 10

def template
  @template
end

Instance Method Details

#createObject

Create the project



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/temp/project.rb', line 29

def create
  if File.file? @template.path
    FileUtils.cp(@template.path, @path)
  else
    FileUtils.mkdir_p(path)
    (@template.files - @template.tempfile.ignore_files).each do |file|
      t_file = File.join(@template.path, file)
      p_file = File.join(@path, file)
      if File.file? t_file
        if @template.tempfile.erb_files.include? file
          renderer = ERB.new(File.read(t_file))
          File.open(p_file, 'w') { |f|
            f.write renderer.result(@template.tempfile.get_binding) }
        else
          FileUtils.cp(t_file, p_file)
        end
      else
        Dir.mkdir(p_file)
      end
    end
  end
end