Skip to content
Browse files

+ preferences and updated examples

  • Loading branch information...
1 parent 902a8ea commit f7c6cb74c5a9948542738562095e3110a5f862dc @floere committed
View
2 examples/reminder_dialog.rb
@@ -16,7 +16,7 @@
# Run a thread that sleeps i hours.
#
Thread.new do
- sleep i
+ sleep i*3600
James.controller.say "Hi, this is James, you set a reminder #{i} hours ago, so I am reminding you! "*3
end
View
2 examples/stocks_dialog.rb
@@ -38,4 +38,4 @@ def initialize *stocks
end
-James.use StocksDialog.new('AAPL', 'IBM')
+James.use StocksDialog.new('IBM', 'AAPL')
View
2 examples/time_dialog.rb
@@ -21,7 +21,7 @@
hear ['What time is it?', 'What time is it now?']
into do
time = Time.now
- "It is currently #{time.hour} #{time.min}."
+ "It is currently #{time.hour}:#{time.min}."
end
end
View
5 examples/weather_dialogue.rb
@@ -11,11 +11,6 @@
#
# This is a very simple James example.
#
-# Note: Currently it just a stub.
-#
-# Note 2: We need to enable Dialog passing to James.use
-# to make dialogs configurable a la WeatherDialog.for("Paris").
-#
class WeatherDialog
include James::Dialog
View
2 lib/james.rb
@@ -1,5 +1,7 @@
module James; end
+require File.expand_path '../james/preferences', __FILE__
+
require File.expand_path '../james/state_api', __FILE__
require File.expand_path '../james/state_internals', __FILE__
View
6 lib/james/controller.rb
@@ -28,6 +28,7 @@ def self.instance
def initialize dialog = nil
@initial = dialog || CoreDialog.new
@conversation = Conversation.new @initial.current
+ @preferences = James::Preferences.new
end
# Convenience method to add a dialog to the current system.
@@ -53,9 +54,6 @@ def listen options = {}
@input_class = options[:input] || Inputs::Audio
@output_class = options[:output] || Outputs::Audio
- @output_options ||= {}
- @output_options[:voice] = options[:voice] || 'com.apple.speech.synthesis.voice.Alex'
-
app = NSApplication.sharedApplication
app.delegate = self
@@ -84,7 +82,7 @@ def start_input
# Start speaking.
#
def start_output
- @output = @output_class.new @output_options
+ @output = @output_class.new @preferences
end
# Callback method from dialog.
View
6 lib/james/outputs/audio.rb
@@ -7,10 +7,10 @@ class Audio
# Create a new audio output.
#
# Options:
- # * voice # Default is 'com.apple.speech.synthesis.voice.Alex'.
+ # * preferences # A James::Preferences
#
- def initialize options = {}
- @output = NSSpeechSynthesizer.alloc.initWithVoice options[:voice] || 'com.apple.speech.synthesis.voice.Alex'
+ def initialize preferences
+ @output = NSSpeechSynthesizer.alloc.initWithVoice preferences.voice
end
# Say the given text out loud.
View
2 lib/james/outputs/terminal.rb
@@ -8,7 +8,7 @@ class Terminal
#
#
- def initialize options = {}
+ def initialize preferences = nil
end
View
60 lib/james/preferences.rb
@@ -0,0 +1,60 @@
+require 'yaml'
+
+module James
+
+ # This class loads the .james preferences
+ # and handles them.
+ #
+ # The preferences are loaded once, at startup. To reload
+ # the preferences, you have to restart James.
+ #
+ # It loads them in the following order of precedence:
+ # * .james
+ # * ~/.james
+ #
+ # If no dotfile is found, it will
+ #
+ class Preferences
+
+ attr_reader :preferences
+
+ def initialize
+ load
+ end
+
+ # Preference accessors & defaults.
+ #
+
+ # Default is the OSX Alex voice.
+ #
+ def voice
+ preferences['voice'] || 'com.apple.speech.synthesis.voice.Alex'
+ end
+
+ # Loads a set of preferences.
+ #
+ def load
+ @preferences = load_from_file || {}
+ end
+
+ # Load the preferences from a file if
+ # a suitable .james is found.
+ #
+ def load_from_file
+ dotfile = find_file
+ YAML.load_file dotfile if dotfile
+ end
+
+ # Finds dotfiles in order of precedence.
+ # * .james
+ # * ~/.james
+ #
+ # Returns nil if none is found.
+ #
+ def find_file
+ Dir['.james', File.expand_path('~/.james')].first
+ end
+
+ end
+
+end
View
1 test/data/.james
@@ -0,0 +1 @@
+voice: "some.voice"
View
37 test/unit/lib/james/preferences_test.rb
@@ -0,0 +1,37 @@
+# encoding: utf-8
+#
+require File.expand_path '../../../../../lib/james/preferences', __FILE__
+
+require 'minitest/autorun'
+require 'minitest/unit'
+
+describe James::Preferences do
+
+ describe 'without a dotfile in the current dir' do
+ before do
+ @preferences = James::Preferences.new
+ end
+ it 'uses the default voice' do
+ assert_equal 'com.apple.speech.synthesis.voice.Alex', @preferences.voice
+ end
+ end
+
+ describe 'with a dotfile in the current dir' do
+ before do
+ @old_dir = Dir.pwd
+ Dir.chdir 'test/data'
+ @preferences = James::Preferences.new
+ end
+ after do
+ Dir.chdir @old_dir
+ end
+ it 'sets the voice' do
+ assert_equal 'some.voice', @preferences.voice
+ end
+ end
+
+ describe 'with a dotfile in the home dir' do
+
+ end
+
+end

0 comments on commit f7c6cb7

Please sign in to comment.
Something went wrong with that request. Please try again.