Module: James::Dialog::ClassMethods

Defined in:
lib/james/dialog_internals.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#statesObject (readonly)

Returns the value of attribute states.



88
89
90
# File 'lib/james/dialog_internals.rb', line 88

def states
  @states
end

Instance Method Details

#hear(definition) ⇒ Object

Defines the entry phrases into this dialog.

Example:

hear 'Hello, James!' => :start


65
66
67
68
69
# File 'lib/james/dialog_internals.rb', line 65

def hear definition
  define_method :entry_phrases do
    definition
  end
end

#initially(state_name) ⇒ Object



54
55
56
57
58
# File 'lib/james/dialog_internals.rb', line 54

def initially state_name
  define_method :current do
    Markers::Current.new state_for(state_name)
  end
end

#state(name, &block) ⇒ Object

Defines a state with transitions.

Example:

state :name do
  # state properties (hear, into, exit) go here.
end


78
79
80
81
82
83
84
# File 'lib/james/dialog_internals.rb', line 78

def state name, &block
  @states       ||= {}
  @states[name] ||= block if block_given?
  define_method name do
    state_for name
  end unless instance_methods.include? name
end

#state_for(name, instance) ⇒ Object

Return a state for this name (and dialog instance).



92
93
94
95
96
97
98
99
# File 'lib/james/dialog_internals.rb', line 92

def state_for name, instance
  # Lazily wrap in State instance.
  #
  if states[name].respond_to?(:call)
    states[name] = State.new(name, instance, &states[name])
  end
  states[name]
end