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
32
# File 'lib/james/controller.rb', line 28

def initialize dialog = nil
  @initial      = dialog || CoreDialog.new
  @conversation = Conversation.new @initial.current
  @preferences  = James::Preferences.new
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.



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

def << dialog
  @initial << dialog
end

#applicationDidFinishLaunching(notification) ⇒ Object

MacRuby callback functions.



68
69
70
71
# File 'lib/james/controller.rb', line 68

def applicationDidFinishLaunching notification
  start_output
  start_input
end

#expectsObject



98
99
100
# File 'lib/james/controller.rb', line 98

def expects
  conversation.expects
end

#hear(text) ⇒ Object



93
94
95
96
97
# File 'lib/james/controller.rb', line 93

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).


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

def listen options = {}
  return if listening

  @listening = true

  @input_class    = options[:input]  || Inputs::Audio
  @output_class   = options[:output] || Outputs::Audio

  app = NSApplication.sharedApplication
  app.delegate = self

  app.run
end

#say(text) ⇒ Object

Callback method from dialog.



90
91
92
# File 'lib/james/controller.rb', line 90

def say text
  @output.say text
end

#start_inputObject

Start recognizing words.



78
79
80
81
# File 'lib/james/controller.rb', line 78

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

#start_outputObject

Start speaking.



84
85
86
# File 'lib/james/controller.rb', line 84

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

#windowWillClose(notification) ⇒ Object



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

def windowWillClose notification
  exit
end