Permalink
Browse files

Added the ability to add actions on the command line.

e.g

	sinatra-gen get:/ post:/:id put:/much/longer/action/*
  • Loading branch information...
quirkey committed Dec 23, 2008
1 parent c2ed8dd commit 895396c115c48495aae7f629b74235a3573e64d2
@@ -5,14 +5,15 @@ class SinatraAppGenerator < RubiGen::Base
default_options :author => nil
- attr_accessor :app_name, :vendor, :tiny, :git, :git_init, :test_framework, :view_framework, :install_scripts, :cap
+ attr_accessor :app_name, :vendor, :tiny, :git, :git_init, :test_framework, :view_framework, :install_scripts, :cap, :actions
def initialize(runtime_args, runtime_options = {})
super
usage if args.empty?
@destination_root = File.expand_path(args.shift)
self.app_name = base_name
extract_options
+ parse_actions(args)
end
def manifest
@@ -102,6 +103,9 @@ def klass_name
app_name.classify
end
+ def parse_actions(*action_args)
+ @actions = action_args.flatten.collect { |a| a.split(':', 2) }
+ end
# Installation skeleton. Intermediate directories are automatically
# created so don't sweat their absence here.
@@ -12,6 +12,15 @@ set :public, 'public'
set :views, 'views'
<%- end -%>
+<%- unless actions.empty? -%>
+<%- actions.each do |meth, path| -%>
+<%= meth %> '<%= path %>' do
+
+end
+
+<%- end -%>
+<%- else -%>
get '/' do
<%= view_framework -%> :index
-end
+end
+<%- end -%>
@@ -132,6 +132,28 @@ def test_generate_app_with_scripts_option
assert_generated_file 'script/generate'
end
+ def test_generate_app_with_actions_and_no_options
+ run_generator('sinatra_app', [APP_ROOT, 'get:/', 'post:/users/:id', 'put:/users/*'], sources)
+ assert_basic_paths_and_files
+ assert_generated_file 'app.rb' do |app_contents|
+ assert_match(/get '\/' do/, app_contents)
+ assert_match(/post '\/users\/\:id' do/, app_contents)
+ assert_match(/put '\/users\/\*' do/, app_contents)
+ end
+ end
+
+ def test_generate_app_with_actions_and_options
+ run_generator('sinatra_app', [APP_ROOT, 'get:/', 'post:/users/:id', '--tiny', 'put:/users/*'], sources)
+ assert_generated_file 'config.ru'
+ assert_generated_file 'app.rb'
+ assert_generated_file 'Rakefile'
+ assert_generated_file 'app.rb' do |app_contents|
+ assert_match(/get '\/' do/, app_contents)
+ assert_match(/post '\/users\/\:id' do/, app_contents)
+ assert_match(/put '\/users\/\*' do/, app_contents)
+ end
+ end
+
private
def assert_basic_paths_and_files
assert_directory_exists 'lib'

0 comments on commit 895396c

Please sign in to comment.