Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Getting recipes into ideal schema configuration, next up comes the ac…

…tual code.
  • Loading branch information...
commit b3540f41d31c3ca58ae60c87c1fb588c4030efb2 1 parent 2a1060e
Michael Bleigh authored
View
1  lib/railswizard-recipes.rb → lib/rails_wizard.rb
@@ -1,5 +1,6 @@
require 'rails_wizard/recipes'
require 'rails_wizard/recipe'
+require 'rails_wizard/config'
Dir[File.dirname(__FILE__) + '/../recipes/*.rb'].each do |path|
key = File.basename(path, '.rb')
View
44 lib/rails_wizard/config.rb
@@ -0,0 +1,44 @@
+module RailsWizard
+ class Config
+ attr_reader :questions
+
+ def initialize(schema)
+ @questions = {}
+ schema.each_pair do |key, details|
+ kind = details['type']
+ raise ArgumentError, "Unrecognized question type, must be one of #{QUESTION_TYPES.keys.join(', ')}" unless QUESTION_TYPES.keys.include?(kind)
+ @questions[key] = QUESTION_TYPES[kind].new(details)
+ end
+ end
+
+ def compile(values = {})
+
+ end
+
+ class Question
+ def initialize(schema)
+ end
+ end
+
+ class Prompt
+ def initialize(details)
+
+ end
+ end
+
+ class TrueFalse < Question
+ def compile
+ "wizard_yes?(#{prompt.inspect})"
+ end
+ end
+
+ class MultipleChoice < Question
+ end
+
+ QUESTION_TYPES = {
+ 'boolean' => TrueFalse,
+ 'string' => Prompt,
+ 'multiple_choice' => MultipleChoice
+ }
+ end
+end
View
40 lib/rails_wizard/recipe.rb
@@ -1,8 +1,16 @@
require 'active_support/inflector'
require 'yaml'
+require 'rails_wizard/config'
+
module RailsWizard
class Recipe
+ ATTRIBUTES = %w(key args category name description template config)
+ DEFAULT_ATTRIBUTES = {
+ :category => 'other',
+ :args => []
+ }
+
def self.generate(key, template_or_file, attributes = {})
if template_or_file.respond_to?(:read)
file = template_or_file.read
@@ -25,16 +33,14 @@ def self.generate(key, template_or_file, attributes = {})
recipe_class
end
- ATTRIBUTES = %w(key category name description template)
-
ATTRIBUTES.each do |setter|
class_eval <<-RUBY
def self.#{setter}
- (@attributes ||= {})[:#{setter}]
+ attributes[:#{setter}]
end
def self.#{setter}=(val)
- (@attributes ||= {})[:#{setter}] = val
+ attributes[:#{setter}] = val
end
def #{setter}
@@ -44,21 +50,39 @@ def #{setter}
end
# The attributes hash containing any set values for
- # the properties specified in ATTRIBUTES.
def self.attributes
- @attributes || {}
+ @attributes ||= DEFAULT_ATTRIBUTES.dup
end
def self.attributes=(hash)
- @attributes = hash
+ attributes.merge! hash
+ end
+
+ def self.config
+ return nil unless attributes[:config]
+ RailsWizard::Config.new(attributes[:config])
end
def attributes
self.class.attributes
end
- def compile
+ def self.compile
"# >#{"[ #{name} ]".center(75,'-')}<\n\n# #{description}\nsay_recipe '#{name}'\n\n#{template}\n"
end
+ def compile; self.class.compile end
+
+ def self.to_mongo(value)
+ case value
+ when Class
+ value.key
+ when String
+ value
+ end
+ end
+
+ def self.from_mongo(key)
+ RailsWizard::Recipes[key]
+ end
end
end
View
8 lib/rails_wizard/recipes.rb
@@ -11,6 +11,10 @@ def self.add(recipe)
recipe
end
+ def self.[](key)
+ @@list[key.to_s]
+ end
+
def self.list
@@list.keys.sort
end
@@ -19,6 +23,10 @@ def self.list_classes
@@list.values.sort_by{|c| c.key}
end
+ def self.categories
+ @@categories.keys.sort
+ end
+
def self.for(category)
(@@categories[category.to_s] || []).sort
end
View
6 railswizard-recipes.gemspec → rails_wizard.gemspec
@@ -18,9 +18,9 @@ Gem::Specification.new do |s|
s.add_dependency "activesupport", "~> 3.0.0"
s.add_development_dependency "rspec", "~> 2.5.0"
- s.files = `git ls-files`.split("\n")
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.files = Dir["lib/**/*.rb", "recipes/*.rb", "README.markdown", "version.rb"]
+ s.test_files = Dir["spec/**/*"]
+ s.executables = Dir["bin/*"]
s.require_paths = ["lib"]
end
View
6 recipes/activerecord.rb
@@ -2,6 +2,10 @@
__END__
-category: orm
name: ActiveRecord
description: "Use the default ActiveRecord database store."
+author: mbleigh
+
+exclusive: orm
+category: persistence
+tags: [sql, defaults, orm]
View
17 recipes/capybara.rb
@@ -0,0 +1,17 @@
+gem 'steak', '>= 1.0.0.rc.1'
+gem 'capybara'
+
+after_bundler do
+ generate 'steak:install'
+end
+
+__END__
+
+name: Capybara
+description: "Use the Capybara acceptance testing libraries with RSpec."
+author: mbleigh
+
+requires: [rspec]
+exclusive: acceptance_testing
+category: testing
+tags: [acceptance]
View
6 recipes/cucumber.rb
@@ -7,6 +7,10 @@
__END__
-category: integration_testing
name: Cucumber
description: "Use Cucumber for integration testing with Capybara."
+author: mbleigh
+
+exclusive: acceptance_testing
+category: testing
+tags: [acceptance]
View
26 recipes/devise.rb
@@ -3,21 +3,21 @@
after_bundler do
generate 'devise:install'
- case template['orm']
- when 'mongo_mapper'
- gem 'mm-devise'
- gsub_file 'config/intializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongo_mapper_active_model'
- when 'mongoid'
- gsub_file 'config/intializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongoid'
- when 'active_record'
- # Nothing to do
-
- generate 'devise user'
- end
+ if recipes.include? 'mongo_mapper'
+ gem 'mm-devise'
+ gsub_file 'config/intializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongo_mapper_active_model'
+ elsif recipes.include? 'mongoid'
+ gsub_file 'config/intializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongoid'
+ end
+
+ generate 'devise user'
end
__END__
-category: authentication
name: Devise
-description: "Utilize Devise for authentication, automatically configured for your selected ORM."
+description: Utilize Devise for authentication, automatically configured for your selected ORM.
+author: mbleigh
+
+category: authentication
+exclusive: authentication
View
5 recipes/haml.rb
@@ -3,6 +3,9 @@
__END__
-category: templating
name: HAML
description: "Utilize HAML for templating."
+author: mbleigh
+
+category: templating
+exclusive: templating
View
26 recipes/heroku.rb
@@ -0,0 +1,26 @@
+after_bundler do
+ heroku_name = nil
+ while heroku_name.blank? || !system("heroku create #{heroku_name}")
+ heroku_name = wizard_ask("What do you want to call your app? ")
+ end
+
+ git :push => "heroku master"
+end
+
+__END__
+
+name: Heroku
+description: Create a Heroku application and instantly deploy.
+author: mbleigh
+
+exclusive: deployment
+category: deployment
+tags: [provider]
+
+config:
+ - app_name:
+ prompt: "Application Name:"
+ type: string
+ - staging:
+ prompt: "Create staging app? (appname-staging.heroku.com)"
+ type: boolean
View
28 recipes/hoptoad.rb
@@ -0,0 +1,28 @@
+if recipes.include? 'heroku'
+ before_config do
+ config['heroku'] = wizard_yes?("Use Heroku addon for Hoptoad?")
+ end
+end
+
+gem 'hoptoad_notifier'
+
+if config['heroku']
+ generate "hoptoad --heroku"
+else
+ generate "hoptoad --api-key #{config['api_key']}"
+end
+
+__END__
+
+name: Hoptoad
+description: Add Hoptoad exception reporting to your application.
+
+category: services
+exclusive: exception_notification
+tags: [exception_notification]
+
+config:
+ - api_key:
+ prompt: "Enter Hoptoad API Key:"
+ type: string
+ unless: heroku
View
10 recipes/jquery.rb
@@ -1,6 +1,6 @@
inside "public/javascripts" do
get "https://github.com/rails/jquery-ujs/raw/master/src/rails.js", "rails.js"
- get "http://code.jquery.com/jquery-1.5.min.js", "jquery.js"
+ get "http://code.jquery.com/jquery-1.5.1.min.js", "jquery.js"
end
application do
@@ -12,6 +12,12 @@
__END__
-category: javascript
name: jQuery
description: "Adds the latest jQuery and Rails UJS helpers for jQuery."
+author: mbleigh
+
+exclusive: javascript_framework
+category: javascript
+tags: [framework]
+
+args: ["-J"]
View
5 recipes/less.rb
@@ -3,6 +3,9 @@
__END__
-category: css
name: Less CSS
description: "Utilize Less CSS for CSS generation utilizing the \"more\" plugin for Rails."
+author: mbleigh
+
+exclusive: css_replacement
+category: css
View
8 recipes/mongo_mapper.rb
@@ -7,6 +7,12 @@
__END__
-category: orm
name: MongoMapper
description: "Use MongoDB with MongoMapper as your primary datastore."
+author: mbleigh
+
+exclusive: orm
+category: persistence
+tags: [mongodb, orm]
+
+args: ["-O"]
View
9 recipes/mongoid.rb
@@ -6,6 +6,13 @@
__END__
-category: orm
name: Mongoid
description: "Utilize MongoDB with Mongoid as the ORM."
+author: mbleigh
+
+category: persistence
+exclusive: orm
+tags: [orm, mongodb]
+
+args: ["-O"]
+
View
8 recipes/mootools.rb
@@ -12,6 +12,12 @@
__END__
-category: javascript
name: MooTools
description: "Adds MooTools and MooTools-compatible UJS helpers."
+author: mbleigh
+
+exclusive: javascript_framework
+category: javascript
+tags: [framework]
+
+args: ["-J"]
View
5 recipes/omniauth.rb
@@ -7,6 +7,9 @@
__END__
-category: authentication
name: OmniAuth
description: "A basic setup of OmniAuth with a SessionsController to handle the request and callback phases."
+author: mbleigh
+
+exclusive: authentication
+category: authentication
View
6 recipes/prototype.rb
@@ -2,6 +2,10 @@
__END__
-category: javascript
name: Prototype
description: "Use the default Javascript libraries and helpers."
+author: mbleigh
+
+exclusive: javascript_framework
+category: javascript
+tags: [framework, defaults]
View
8 recipes/rspec.rb
@@ -10,6 +10,12 @@
__END__
-category: unit_testing
name: RSpec
description: "Use RSpec for unit testing for this Rails app."
+author: mbleigh
+
+exclusive: unit_testing
+category: testing
+
+args: ["-T"]
+
View
5 recipes/sass.rb
@@ -4,6 +4,9 @@
__END__
-category: css
name: SASS
description: "Utilize SASS (through the HAML gem) for really awesome stylesheets!"
+author: mbleigh
+
+category: css
+exclusive: css_replacement
View
5 recipes/slim.rb
@@ -3,6 +3,9 @@
__END__
-category: templating
name: Slim
description: "Use Slim as the default templating engine."
+author: mbleigh
+
+category: templating
+exclusive: templating
View
12 recipes/steak.rb
@@ -1,12 +0,0 @@
-gem 'steak', '>= 1.0.0.rc.1'
-gem 'capybara'
-
-after_bundler do
- generate 'steak:install'
-end
-
-__END__
-
-category: integration_testing
-name: Steak
-description: "Use Steak and Capybara for integration testing."
View
6 recipes/test_unit.rb
@@ -2,6 +2,10 @@
__END__
-category: unit_testing
name: Test::Unit
description: "Utilize the default Rails test facilities."
+author: mbleigh
+
+exclusive: unit_testing
+category: testing
+tags: [defaults]
View
32 spec/rails_wizard/config_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe RailsWizard::Config do
+ describe '#initialize' do
+ subject{ RailsWizard::Config.new(@schema) }
+ it 'should add a question key for each key of the schema' do
+ @schema = {
+ 'test' => {'type' => 'string'},
+ 'foo' => {'type' => 'boolean'}
+ }
+ subject.questions.should be_key('test')
+ end
+
+ it 'should instantiate the correct question type for each question' do
+ @schema = {
+ 'string' => {'type' => 'string'},
+ 'boolean' => {'type' => 'boolean'},
+ 'multiple_choice' => {'type' => 'multiple_choice'}
+ }
+ subject.questions['string'].should be_kind_of(RailsWizard::Config::Prompt)
+ subject.questions['boolean'].should be_kind_of(RailsWizard::Config::TrueFalse)
+ subject.questions['multiple_choice'].should be_kind_of(RailsWizard::Config::MultipleChoice)
+ end
+
+ it 'should error on invalid question type' do
+ @schema = {
+ 'invalid' => {'type' => 'invalid'}
+ }
+ lambda{ subject }.should raise_error(ArgumentError)
+ end
+ end
+end
View
15 spec/rails_wizard/recipe_spec.rb
@@ -5,7 +5,7 @@
subject{ RailsWizard::Recipe.generate('recipe_example', "# this is a test", :category => 'example', :name => "RailsWizard Example") }
context 'string setter methods' do
- (RailsWizard::Recipe::ATTRIBUTES + ['category']).each do |setter|
+ (RailsWizard::Recipe::ATTRIBUTES - ['config']).each do |setter|
it "should be able to set #{setter} with an argument" do
subject.send(setter + '=', "test")
subject.send(setter).should == 'test'
@@ -19,11 +19,6 @@
end
describe '.attributes' do
- it 'should be a hash of the set attributes' do
- hash = subject::ATTRIBUTES.inject({}){|hash,att| subject.send(att + '=', att); hash[att.to_sym] = att; hash}
- subject.attributes.should == hash
- end
-
it 'should be accessible from the instance' do
subject.new.attributes.should == subject.attributes
end
@@ -71,6 +66,14 @@
end
end
end
+
+ it 'should set default attributes' do
+ recipe = RailsWizard::Recipe.generate('abc','# test')
+
+ RailsWizard::Recipe::DEFAULT_ATTRIBUTES.each_pair do |k,v|
+ recipe.send(k).should == v
+ end
+ end
end
__END__
View
6 spec/rails_wizard/recipes/sanity_spec.rb
@@ -16,6 +16,12 @@
end
end
+ it "should have a Config or nil config" do
+ if recipe.config
+ recipe.config.should be_kind_of(RailsWizard::Config)
+ end
+ end
+
it "should be in the list" do
RailsWizard::Recipes.list_classes.should be_include(recipe)
RailsWizard::Recipes.list.should be_include(recipe.key)
View
2  spec/spec_helper.rb
@@ -2,7 +2,7 @@
Bundler.setup
require 'rspec'
-require 'railswizard-recipes'
+require 'rails_wizard'
RSpec.configure do |config|
Please sign in to comment.
Something went wrong with that request. Please try again.