Class: Captify::Template

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_dirObject

Returns the value of attribute base_dir.



5
6
7
# File 'lib/captify/template.rb', line 5

def base_dir
  @base_dir
end

#filesObject

Returns the value of attribute files.



5
6
7
# File 'lib/captify/template.rb', line 5

def files
  @files
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/captify/template.rb', line 5

def name
  @name
end

Class Method Details

.load_from_path(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/captify/template.rb', line 21

def self.load_from_path(path)
  return nil unless File.exists? path

  self.new.tap do |t|
    t.name = File.basename path
    t.base_dir = path
    t.files = Dir[ File.join(path, '**', '*') ].
      select{|item| File.file? item}.
      map{|file| file.sub(path+"/", '')}
  end
end

Instance Method Details

#apply_to(target_dir) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/captify/template.rb', line 7

def apply_to(target_dir)
  msg = []
  files.each do |file|
    target_file = File.join(target_dir, file)
    prefix = File.exist?(target_file) ? "[overwrite]" : '[add]'

    FileUtils.mkdir_p File.dirname(target_file)
    FileUtils.cp File.join(base_dir, file), target_file

    msg << "#{prefix} writing '#{target_file}'"
  end
  return msg
end