Module: Merb::GeneratorHelpers
- Included in:
- GeneratorBase
- Defined in:
- lib/merb-gen/helpers.rb
Instance Method Summary collapse
-
#copy_dirs ⇒ Object
Establishes the directories of the template layout.
-
#copy_files ⇒ Object
Copies all files within each directory of the template layout.
- #interpolate_path(path) ⇒ Object
-
#relative(path, dir = nil) ⇒ Object
Remove “template/” from a file name.
Instance Method Details
#copy_dirs ⇒ Object
Establishes the directories of the template layout
22 23 24 25 26 |
# File 'lib/merb-gen/helpers.rb', line 22 def copy_dirs select_template_directories.map { |x| relative(x) }.each do |path| m.directory interpolate_path(path) end end |
#copy_files ⇒ Object
Copies all files within each directory of the template layout
29 30 31 32 33 34 35 36 37 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 65 66 67 |
# File 'lib/merb-gen/helpers.rb', line 29 def copy_files select_template_directories(true).each do |file| # Remove "template/" from the glob filename dir = relative(file) # Get all the files under the directory that are not directories or in # the list of excluded templates above. files = Dir["#{file.empty? ? "." : file}/*"].reject { |f| File.directory?(f) } next if files.empty? # We want to templatize any files that contain <% %> characters #-- # This is old code, should we need it: # templates, to_copy = files.partition {|file| !(file =~ /\.erb$/) && File.read(file) =~ /<%.*%>/} templates, to_copy = files.partition { |file| File.read(file) =~ /<%.*%>/ } # Make the paths relative to the directory we're inspecting [to_copy, templates].each do |paths| paths.map! { |f| relative(f, dir) } end # Copy the files over to_copy.each do |filename| m.file( file_name(dir, filename), file_name(interpolate_path(dir), filename) ) end # Copy the templates over templates.each do |filename| m.template( file_name(dir, filename), file_name(interpolate_path(dir), interpolate_path(filename)) ) end end end |
#interpolate_path(path) ⇒ Object
17 18 19 |
# File 'lib/merb-gen/helpers.rb', line 17 def interpolate_path(path) path.gsub(/%([^\}]*)%/) {|x| assigns[$1.to_sym]} end |
#relative(path, dir = nil) ⇒ Object
Remove “template/” from a file name
Parameters
path
<String>-
the path to relative-ize
dir
<String>-
a prefix directory to add into the relative-ized path
Returns
- String
-
a relative-ized path
13 14 15 |
# File 'lib/merb-gen/helpers.rb', line 13 def relative(path, dir = nil) path.gsub(/^#{base}\/templates(\/#{dir ? dir + "/" : ""})?/, "") end |