Module: James::Dialog

Included in:
CoreDialog
Defined in:
lib/james/dialog_api.rb,
lib/james/dialog_internals.rb

Overview

A dialog is just a container object for defining states and executing methods.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(into) ⇒ Object



8
9
10
# File 'lib/james/dialog_internals.rb', line 8

def self.included into
  into.extend ClassMethods
end

Instance Method Details

#<<(dialog_s) ⇒ Object

Chain the given Dialog(s) to all chainable states in this Dialog.

Note: If you only want one state to chain,

then get it from the otiginating dialog
using dialog.state_for(:name) and
append the dialog there:
dialog.follows preceding_dialog.state_for(:name)


45
46
47
48
49
50
# File 'lib/james/dialog_internals.rb', line 45

def << dialog_s
  self.class.states.each do |(name, definition)|
    state = state_for name # TODO Do not call this everywhere.
    dialog_s.chain_to(state) if state.chainable?
  end
end

#chain_to(state) ⇒ Object

Chain (the states of) this dialog to the given state.

Creates state instances if it is given names.

Note: Be careful not to create circular

state chaining. Except if you really
want that.


29
30
31
32
33
34
# File 'lib/james/dialog_internals.rb', line 29

def chain_to state
  warn "Define a hear => :some_state_name in a dialog to have it be able to chain to another." && return unless respond_to?(:entry_phrases)
  entry_phrases.each do |(phrases, entry_state)|
    state.hear phrases => state_for(entry_state)
  end
end

#state_for(possible_state) ⇒ Object

Returns a state instance for the given state / or state name.

Note: Lazily creates the state instances.



16
17
18
19
# File 'lib/james/dialog_internals.rb', line 16

def state_for possible_state
  return possible_state if possible_state.respond_to?(:expects)
  self.class.state_for possible_state, self
end