Class: James::Conversation
- Inherits:
-
Object
- Object
- James::Conversation
- Defined in:
- lib/james/conversation.rb
Overview
A conversation has a number of markers (position in dialog), whose dialogs are visited in order of preference.
Why? Conversations have multiple points where they can be. (Politics, then this joke, then back again, finally “Oh, bye I have to go!”)
Instance Attribute Summary collapse
-
#markers ⇒ Object
Returns the value of attribute markers.
Instance Method Summary collapse
-
#enter ⇒ Object
Enter enters the first visitor.
-
#expects ⇒ Object
Simply returns the sum of what phrases all dialogs do expect, front-to-back.
-
#hear(phrase, &block) ⇒ Object
Hear tries all visitors in order until one hears a phrase he knows.
-
#initialize(initial) ⇒ Conversation
constructor
A Conversation keeps a stack of markers with an initial one.
Constructor Details
#initialize(initial) ⇒ Conversation
A Conversation keeps a stack of markers with an initial one.
17 18 19 |
# File 'lib/james/conversation.rb', line 17 def initialize initial @markers = [initial] end |
Instance Attribute Details
#markers ⇒ Object
Returns the value of attribute markers.
12 13 14 |
# File 'lib/james/conversation.rb', line 12 def markers @markers end |
Instance Method Details
#enter ⇒ Object
Enter enters the first visitor.
44 45 46 |
# File 'lib/james/conversation.rb', line 44 def enter markers.first.enter end |
#expects ⇒ Object
Simply returns the sum of what phrases all dialogs do expect, front-to-back.
Stops as soon as a marker is not on a chainable state anymore.
52 53 54 55 56 57 58 |
# File 'lib/james/conversation.rb', line 52 def expects markers.inject([]) do |expects, marker| total = marker.expects + expects break total unless marker.chainable? total end end |
#hear(phrase, &block) ⇒ Object
Hear tries all visitors in order until one hears a phrase he knows.
If a dialog boundary has been crossed: A new visitor is added with the target state of that heard phrase at the position.
After that, all remaining visitors are removed from the current stack (since we are obviously not in one of the later dialogs anymore).
33 34 35 36 37 38 39 40 |
# File 'lib/james/conversation.rb', line 33 def hear phrase, &block self.markers = markers.inject([]) do |remaining, marker| markers = marker.hear phrase, &block remaining = remaining + markers break remaining if remaining.last && remaining.last.current? remaining end end |