Class: RailsWizard::Config
- Inherits:
-
Object
- Object
- RailsWizard::Config
- Defined in:
- lib/rails_wizard/config.rb
Defined Under Namespace
Classes: MultipleChoice, Prompt, TrueFalse
Constant Summary collapse
- QUESTION_TYPES =
{ 'boolean' => TrueFalse, 'string' => Prompt, 'multiple_choice' => MultipleChoice }
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#questions ⇒ Object
readonly
Returns the value of attribute questions.
Instance Method Summary collapse
- #compile(values = {}) ⇒ Object
-
#initialize(schema, defaults = nil) ⇒ Config
constructor
A new instance of Config.
Constructor Details
#initialize(schema, defaults = nil) ⇒ Config
Returns a new instance of Config.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rails_wizard/config.rb', line 7 def initialize(schema, defaults=nil) @questions = ActiveSupport::OrderedHash.new @defaults = defaults schema.each do |hash| key = hash.keys.first details = hash.values.first 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 |
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
5 6 7 |
# File 'lib/rails_wizard/config.rb', line 5 def defaults @defaults end |
#questions ⇒ Object (readonly)
Returns the value of attribute questions.
5 6 7 |
# File 'lib/rails_wizard/config.rb', line 5 def questions @questions end |
Instance Method Details
#compile(values = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/rails_wizard/config.rb', line 20 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}')" end result.join("\n") end |