Permalink
Browse files

Recipe hands defaults to new configs; config merges when compiling

  • Loading branch information...
bryanstearns committed May 20, 2012
1 parent d98424e commit 848a9d201324d86b84282e973dbf9379170033b3
Showing with 15 additions and 4 deletions.
  1. +4 −2 lib/rails_wizard/config.rb
  2. +1 −1 lib/rails_wizard/recipe.rb
  3. +10 −1 spec/rails_wizard/config_spec.rb
@@ -2,10 +2,11 @@
module RailsWizard
class Config
- attr_reader :questions
+ attr_reader :questions, :defaults
- def initialize(schema)
+ def initialize(schema, defaults=nil)
@questions = ActiveSupport::OrderedHash.new
+ @defaults = defaults
schema.each do |hash|
key = hash.keys.first
details = hash.values.first
@@ -18,6 +19,7 @@ def initialize(schema)
def compile(values = {})
result = []
+ values.merge!(defaults) if defaults
result << "config = #{values.inspect}"
@questions.each_pair do |key, question|
result << "config['#{key}'] = #{question.compile} unless config.key?('#{key}')"
@@ -73,7 +73,7 @@ def self.attributes=(hash)
def self.config
return nil unless attributes[:config]
- RailsWizard::Config.new(attributes[:config])
+ RailsWizard::Config.new(attributes[:config], attributes[:defaults])
end
def attributes
@@ -2,7 +2,8 @@
describe RailsWizard::Config do
describe '#initialize' do
- subject{ RailsWizard::Config.new(YAML.load(@schema)) }
+ let(:defaults) { nil }
+ subject{ RailsWizard::Config.new(YAML.load(@schema), defaults) }
it 'should add a question key for each key of the schema' do
@schema = <<-YAML
- test:
@@ -72,6 +73,14 @@
it 'should handle "unelss_recipe"' do
lines[3].should be_include("!recipe?('awesome')")
end
+
+ describe 'with defaults' do
+ let(:defaults) { { 'multiple_choice' => 'def' }}
+
+ it 'should process defaults' do
+ lines[0].should == 'config = {"multiple_choice"=>"def"}'
+ end
+ end
end
describe RailsWizard::Config::Prompt do

0 comments on commit 848a9d2

Please sign in to comment.