0
- desc 'Generate a new controller, with name=<name>'
0
- task :controller do |task|
0
- controller_name = name.camel_case
0
- app_name = ( ENV['app'] || Dir.pwd.split('/').last ).camel_case
0
- raise "Cannot generate Default yet" if controller_name == 'Default'
0
- filename = File.expand_path "controllers/#{name}.rb"
0
- if File.exist?(filename)
0
- $stderr.puts "#{filename} already exists"
0
+ # We're declaring these tasks first so we can add descriptions.
0
+ # The real work is done in the rule, below.
0
+ desc 'Generate new controller, with name=<name>'
0
+ desc 'Generate new view, with name=<name>'
0
+ desc 'Generate new resource, with name=<name>'
0
+ # Rake rules are awesome. In the main block, t.name is the task name that matched
0
+ # the regex. t.source is the string returned by the lambda argument to rule.
0
+ rule( /controller|view|resource/ => lambda { |task| basetask(task).camel_case << "s" } ) do |t|
0
+ content = class_template( app_name, t.source, constant_name )
0
+ name = basetask(t.name) << "s"
0
+ File.write( filename( name ), content )
0
+ desc 'Generate new helper, with name=<name>'
0
+ task :helper do |task|
0
+ content = module_template( app_name, "Helpers", constant_name) do
0
+ "include Waves::Helpers::Default"
0
+ File.write( filename( "helpers" ), content )
0
- class #{controller_name} < Default
0
+desc "Generate resource, controller, view, and helper with name=<name>"
0
+task :generate => %w{ generate:resource generate:controller generate:view generate:helper }
0
+ ( ENV['app'] || Dir.pwd.split('/').last ).camel_case
0
- File.write( filename, controller )
0
+ str = ENV['name'].camel_case
0
+ raise "Cannot generate Default yet" if str == 'Default'
0
+ path = File.expand_path "#{kind}/#{ENV['name'].snake_case}.rb"
0
+ $stderr.puts " Problem encountered:\n #{path} already exists"
0
- desc 'Generate new view, with name=<name>'
0
- view_name = name.camel_case
0
- app_name = ( ENV['app'] || Dir.pwd.split('/').last ).camel_case
0
- raise "Cannot generate Default yet" if view_name == 'Default'
0
- filename = File.expand_path "views/#{name}.rb"
0
- if File.exist?(filename)
0
- $stderr.puts "#{filename} already exists"
0
+# Rake only pretends to namespace tasks, so to get what we think of as
0
+# the task name, you must split and grab.
0
+def class_template(app_name, place, class_name)
0
- class #{view_name} < Default
0
+ class #{class_name} < Default
0
- File.write( filename, view )
0
- desc 'Generate a new helper, with name=<name>'
0
- task :helper do |task|
0
- helper_name = name.camel_case
0
- app_name = ( ENV['app'] || Dir.pwd.split('/').last ).camel_case
0
- raise "Cannot generate Default yet" if helper_name == 'Default'
0
- filename = File.expand_path "helpers/#{name}.rb"
0
- if File.exist?(filename)
0
- $stderr.puts "#{filename} already exists"
0
+# This method expects its block to return something usable as a string.
0
+def module_template(app_name, place, module_name, &block)
0
- include Waves::Helpers::Default
0
+ #{block.call if block}
0
- File.write( filename, helper )
0
\ No newline at end of file
Comments
No one has commented yet.