Class: AppScrollsScrolls::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/appscrolls/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

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/appscrolls/config.rb', line 7

def initialize(schema)
  @questions = ActiveSupport::OrderedHash.new
  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

#questionsObject (readonly)

Returns the value of attribute questions.



5
6
7
# File 'lib/appscrolls/config.rb', line 5

def questions
  @questions
end

Instance Method Details

#compile(values = {}) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/appscrolls/config.rb', line 19

def compile(values = {})
  result = []
  result << "config = #{values.inspect}"
  @questions.each_pair do |key, question|
    result << "config['#{key}'] = #{question.compile} unless config.key?('#{key}')"
  end
  result.join("\n")
end