Class: James::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/james/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dialog = nil) ⇒ Controller

This puts together the initial dialog and the user ones that are hooked into it.

The initial dialog needs an state defined as initially. This is where it will start.

Example:

initially :awake
state :awake do
  # ...
end

If you don’t give it an initial dialog, James will simply use the built-in CoreDialog.



28
29
30
31
# File 'lib/james/controller.rb', line 28

def initialize dialog = nil
  @initial      = dialog || CoreDialog.new
  @conversation = Conversation.new @initial.current
end

Instance Attribute Details

#conversationObject (readonly)

Returns the value of attribute conversation.



5
6
7
# File 'lib/james/controller.rb', line 5

def conversation
  @conversation
end

#initialObject (readonly)

Returns the value of attribute initial.



5
6
7
# File 'lib/james/controller.rb', line 5

def initial
  @initial
end

#listeningObject (readonly)

Returns the value of attribute listening.



5
6
7
# File 'lib/james/controller.rb', line 5

def listening
  @listening
end

Class Method Details

.instanceObject

Singleton reader.



9
10
11
# File 'lib/james/controller.rb', line 9

def self.instance
  @controller ||= new
end

Instance Method Details

#<<(dialog) ⇒ Object

Convenience method to add a dialog to the current system.

Will add the dialog to the initial dialog passed into the controller.



38
39
40
# File 'lib/james/controller.rb', line 38

def << dialog
  @initial << dialog
end

#applicationDidFinishLaunching(notification) ⇒ Object

MacRuby callback functions.



70
71
72
73
# File 'lib/james/controller.rb', line 70

def applicationDidFinishLaunching notification
  start_output
  start_input
end

#expectsObject



100
101
102
# File 'lib/james/controller.rb', line 100

def expects
  conversation.expects
end

#hear(text) ⇒ Object



95
96
97
98
99
# File 'lib/james/controller.rb', line 95

def hear text
  conversation.hear text do |response|
    say response
  end
end

#listen(options = {}) ⇒ Object

Start listening using the provided options.

Options:

* input  # Inputs::Terminal or Inputs::Audio (default).
* output # Outputs::Terminal or Outputs::Audio (default).


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/james/controller.rb', line 48

def listen options = {}
  return if listening

  @listening = true

  @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

  app.run
end

#say(text) ⇒ Object

Callback method from dialog.



92
93
94
# File 'lib/james/controller.rb', line 92

def say text
  @output.say text
end

#start_inputObject

Start recognizing words.



80
81
82
83
# File 'lib/james/controller.rb', line 80

def start_input
  @input = @input_class.new self
  @input.listen
end

#start_outputObject

Start speaking.



86
87
88
# File 'lib/james/controller.rb', line 86

def start_output
  @output = @output_class.new @output_options
end

#windowWillClose(notification) ⇒ Object



74
75
76
# File 'lib/james/controller.rb', line 74

def windowWillClose notification
  exit
end