Class: Yop::Template
Overview
A Yop Template, which consists of a base directory and an hash of variables
Instance Method Summary collapse
-
#apply(directory) ⇒ Object
Apply the template on a directory.
-
#initialize(base_directory, vars = {}) ⇒ Template
constructor
Create a new template from a base directory.
Constructor Details
#initialize(base_directory, vars = {}) ⇒ Template
Create a new template from a base directory
36 37 38 39 |
# File 'lib/yop/templates.rb', line 36 def initialize(base_directory, vars = {}) @base = base_directory @vars = vars end |
Instance Method Details
#apply(directory) ⇒ Object
Apply the template on a directory. It creates it if it doesn’t exist, then recursively copies itself in it
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/yop/templates.rb', line 45 def apply(directory) mkdir_p directory # get relative paths sources = [] cd(@base) { sources = Dir["**/*", "**/.*"] } cd directory do sources.each do |path| next if skip? path source = "#{@base}/#{path}" if File.directory? source mkdir_p path elsif File.file? source File.open(path, "w") do |f| content = replace_vars source f.write(content) end else puts "Warning: unsupported file: #{source}" next end mirror_perms source, path end end end |