Permalink
Browse files

adds question/ending constructors

  • Loading branch information...
1 parent fd33cec commit e9b302b67734c10f9f8206ef262611f715b705e1 @Najaf Najaf committed Feb 27, 2013
Showing with 46 additions and 1 deletion.
  1. +4 −0 lib/dumb-answers/ending.rb
  2. +4 −0 lib/dumb-answers/question.rb
  3. +1 −1 lib/dumb-answers/yaml_parser.rb
  4. +15 −0 spec/ending_spec.rb
  5. +22 −0 spec/question_spec.rb
View
4 lib/dumb-answers/ending.rb
@@ -1,4 +1,8 @@
module DumbAnswers
class Ending
+ attr_accessor :id, :text
+ def initialize(attributes)
+ @id, @text = *attributes.values_at('id', 'text')
+ end
end
end
View
4 lib/dumb-answers/question.rb
@@ -1,5 +1,9 @@
module DumbAnswers
class Question
+ attr_accessor :id, :text, :additional_text, :answers, :transitions
+ def initialize(attributes)
+ @id, @text, @additional_text, @answers, @transitions = *attributes.values_at('id', 'text', 'additional_text', 'answers', 'transitions')
+ end
end
end
View
2 lib/dumb-answers/yaml_parser.rb
@@ -7,7 +7,7 @@ module YamlParser
def self.load(data)
data.reduce({}) do |memo, node|
- memo[node['id']] = TYPES[node['type']].new
+ memo[node['id']] = TYPES[node['type']].new(node)
memo
end
end
View
15 spec/ending_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe Ending do
+ describe '#initialize' do
+ it 'sets id and text from hash' do
+ attributes = {
+ 'id' => 1,
+ 'text' => 2
+ }
+ e = Ending.new(attributes)
+ e.id.must_equal 1
+ e.text.must_equal 2
+ end
+ end
+end
View
22 spec/question_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe Question do
+ describe '#initialize' do
+ it 'sets attributes on question from hash' do
+ attributes = {
+ 'id' => 1,
+ 'text' => 2,
+ 'additional_text' => 3,
+ 'answers' => 4,
+ 'transitions' => 5
+ }
+
+ q = Question.new attributes
+ q.id.must_equal 1
+ q.text.must_equal 2
+ q.additional_text.must_equal 3
+ q.answers.must_equal 4
+ q.transitions.must_equal 5
+ end
+ end
+end

0 comments on commit e9b302b

Please sign in to comment.