Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adds tree loading functionality

  • Loading branch information...
commit 5141b2da9e41fa8136c4a9aed585f419dc2acc4a 1 parent afecd3e
@Najaf Najaf authored
View
1  lib/dumb-answers.rb
@@ -5,4 +5,5 @@ module DumbAnswers
autoload :Question, 'dumb-answers/question'
autoload :Ending, 'dumb-answers/ending'
autoload :State, 'dumb-answers/state'
+ autoload :Tree, 'dumb-answers/tree'
end
View
31 lib/dumb-answers/tree.rb
@@ -0,0 +1,31 @@
+require 'yaml'
+module DumbAnswers
+ module Tree
+ class << self
+ attr_accessor :data_directory
+
+ def trees
+ @trees ||= {}
+ end
+
+ def get(name)
+ if trees[name].nil?
+ trees[name] = YamlParser.load(
+ YAML.load_file(File.join(@data_directory, name.to_s + '.yml'))
+ )
+ end
+ trees[name]
+ end
+
+ def begin(name, options)
+ continue(name, options.merge(history: []))
+ end
+
+ def continue(name, options)
+ State.new(get(name), options[:from] || options[:current]).tap do |s|
+ s.history = options[:history]
+ end
+ end
+ end
+ end
+end
View
45 spec/tree_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+describe Tree do
+ describe '.get' do
+ before do
+ Tree.data_directory = File.join(File.dirname(__FILE__), 'data')
+ end
+
+ it 'returns node array for given keys' do
+ Tree.get('parser-test').length.must_equal 2
+ Tree.get('state-test').length.must_equal 4
+ end
+ end
+
+ describe '.begin' do
+ before do
+ Tree.data_directory = File.join(File.dirname(__FILE__), 'data')
+ end
+
+ it 'returns a state object with an empty history' do
+ Tree.begin('state-test', from: 'q1').history.must_equal []
+ end
+
+ it 'returns a state object with set current node' do
+ Tree.begin('state-test', from: 'q2').current.id.must_equal 'q2'
+ end
+ end
+
+ describe '.continue' do
+ before do
+ Tree.data_directory = File.join(File.dirname(__FILE__), 'data')
+ @state = Tree.continue(
+ 'state-test', from: 'q2', history: [['q1', 'a']]
+ )
+ end
+
+ it 'returns a state object with the passed history' do
+ @state.history.must_equal [['q1', 'a']]
+ end
+
+ it 'returns a state object with set current node' do
+ Tree.continue('state-test', from: 'q2').current.id.must_equal 'q2'
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.