Class: Tidy::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/tidy/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Template

Returns a new instance of Template.



5
6
7
8
9
10
11
12
13
# File 'lib/tidy/template.rb', line 5

def initialize(params)
  @template_id = params[:template_id]
  @force = params[:force]
  @args = params[:args].collect{|arg| arg unless arg.match("--")}

  create_template_binding
  copy_files if @template_binding.valid?

end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/tidy/template.rb', line 4

def args
  @args
end

Instance Method Details

#copy_file(filename) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tidy/template.rb', line 38

def copy_file(filename)
  if File.directory?(filename)
  
    make_dirs(get_destination filename)
    return
  end
  contents = IO.read(filename)
  destination = get_destination(filename)
  make_dirs(File.split(destination)[0])

  result = File.extname(filename) == ".erb" ? contents : ERB.new(contents,0,"%").result(@template_binding.get_binding)
  force = @force unless @force.nil?
  if File.exists?(destination) 
    if(force.nil?) 
      puts "overwrite #{destination}? (yN)"
      force = $stdin.gets.chomp.downcase == "y"
    end
    unless force
      puts "skipping #{destination}"
    else
      File.delete(destination)
      write_result(result,destination)
    end
  else
    write_result(result,destination)
  end
end

#copy_filesObject



30
31
32
33
34
35
36
# File 'lib/tidy/template.rb', line 30

def copy_files
  pattern = File.join(template_path,"**/*")
  files = Dir.glob(pattern)
  files.each do |filename|
    copy_file(filename) unless filename == template_class
  end
end

#create_template_bindingObject



14
15
16
17
18
19
20
# File 'lib/tidy/template.rb', line 14

def create_template_binding
  require template_class
  # the get_template_binding method is placed
  # in the included template class
  @template_binding = get_template_binding(self)
  @template_binding.init!
end

#get_destination(file) ⇒ Object



77
78
79
# File 'lib/tidy/template.rb', line 77

def get_destination(file)
  @template_binding.get_destination file.gsub(template_path,".")
end

#make_dirs(dir) ⇒ Object



73
74
75
# File 'lib/tidy/template.rb', line 73

def make_dirs(dir)
  FileUtils.mkdir_p(dir) unless File.exists? dir
end

#template_classObject



26
27
28
# File 'lib/tidy/template.rb', line 26

def template_class
  File.join(template_path,"#{@template_id}.rb")
end

#template_pathObject



22
23
24
# File 'lib/tidy/template.rb', line 22

def template_path
  File.join(File.expand_path(File.dirname(__FILE__)),'../../templates',@template_id)
end

#write_result(result, destination) ⇒ Object



66
67
68
69
70
71
# File 'lib/tidy/template.rb', line 66

def write_result result,destination
  File.open(destination,'a') do |file|
    file << result
  end
  puts("created " + destination)
end