Module: FileGenerator
- Defined in:
- lib/file_generator.rb
Class Method Summary collapse
-
.config ⇒ Object
Called by rake generate:config.
-
.helper(name) ⇒ Object
Called by rake generate:helper name=<template_name>.
-
.page_yaml(template, names) ⇒ Object
Called by rake generate:page template=<name> names=“<file1 file2>”.
Class Method Details
.config ⇒ Object
Called by rake generate:config
Generates a default config.rb file, with usage examples present and commented out.
36 37 38 39 40 41 42 |
# File 'lib/file_generator.rb', line 36 def config if File.exists?("config.rb") puts 'Config file already exists!' else FileUtils.cp(File.dirname(__FILE__) + '/../files/config.rb', 'config.rb') end end |
.helper(name) ⇒ Object
Called by rake generate:helper name=<template_name>
Generates a helper module in /helpers. A helper will be loaded if it shares the name of the template being applied. To load a different helper, you’ll have to include it in default_helper.
23 24 25 26 27 28 29 30 31 |
# File 'lib/file_generator.rb', line 23 def helper(name) helper_name = name =~ /_helper\Z/ ? name : "#{name}_helper" module_name = helper_name.split('_').map{|str| str.capitalize}.join File.open("helpers/#{helper_name}.rb", 'w') do |f| f << "module #{module_name} # Add methods for the #{name} template here end" end end |
.page_yaml(template, names) ⇒ Object
Called by rake generate:page template=<name> names=“<file1 file2>”
Generates empty page yaml files in /pages. Takes the methods called in the template and turns them into a yaml framework.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/file_generator.rb', line 8 def page_yaml(template, names) template = Templette::Template.new(template) names.split(/\s+/).each do |name| filename = "pages/#{name}.yml" unless File.exists?(filename) FileUtils.mkdir_p(File.dirname(filename)) File.open(filename, 'w') {|f| f << template.to_yaml } end end end |