Class: James::Markers::Marker

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

Overview

A marker is a point in conversation where we once were and might go back.

TODO: Rename to ?.

Direct Known Subclasses

Current, Memory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current) ⇒ Marker

Pass in an current state.



16
17
18
# File 'lib/james/markers/marker.rb', line 16

def initialize current
  @current = current
end

Instance Attribute Details

#currentObject

Returns the value of attribute current.



12
13
14
# File 'lib/james/markers/marker.rb', line 12

def current
  @current
end

Instance Method Details

#chainable?Boolean

Does the current state allow penetration into another dialog?

Returns:

  • (Boolean)


82
83
84
# File 'lib/james/markers/marker.rb', line 82

def chainable?
  current.chainable?
end

#check {|"Whoops. That led nowhere. Perhaps you didn't define the target state?"| ... } ⇒ Object

Yields:

  • ("Whoops. That led nowhere. Perhaps you didn't define the target state?")


59
60
61
# File 'lib/james/markers/marker.rb', line 59

def check
  yield("Whoops. That led nowhere. Perhaps you didn't define the target state?") unless self.current
end

#enter {|result| ... } ⇒ Object

We hear a phrase.

Also used to start the whole process.

Yields:

  • (result)


30
31
32
33
34
# File 'lib/james/markers/marker.rb', line 30

def enter
  result = current.__into__
  yield result if result && block_given?
  result
end

#exit {|result| ... } ⇒ Object

Yields:

  • (result)


38
39
40
41
42
# File 'lib/james/markers/marker.rb', line 38

def exit
  result = current.__exit__
  yield result if result && block_given?
  result
end

#hears?(phrase) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/james/markers/marker.rb', line 76

def hears? phrase
  expects.include? phrase
end

#process(phrase, &block) ⇒ Object

Returns falsy if it stays the same.



65
66
67
68
69
70
71
72
# File 'lib/james/markers/marker.rb', line 65

def process phrase, &block
  exit_text = exit &block
  last_context = current.context
  transition phrase, &block
  check &block
  into_text = enter &block
  last_context != current.context
end

#resetObject

Resets the current state back to the initial.



22
23
24
# File 'lib/james/markers/marker.rb', line 22

def reset
  # Never moves, thus never reset.
end

#to_sObject



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

def to_s
  "#{self.class.name}(#{initial}, current: #{current})"
end

#transition(phrase) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/james/markers/marker.rb', line 46

def transition phrase
  state_or_lambda = current.next_for phrase
  if state_or_lambda.respond_to?(:call)
    result = current.__transition__ &state_or_lambda # Don't transition.
    yield result if result && block_given?
    result
  else
    self.current = state_or_lambda
  end
end