Module: Socrates::Core::State
- Included in:
- SampleStates::AskForBirthDate, SampleStates::AskForName, SampleStates::CalculateAge, SampleStates::EndConversation1, SampleStates::EndConversation2, SampleStates::GetStarted, SampleStates::Help, SampleStates::NoComprende, SampleStates::RaiseError
- Defined in:
- lib/socrates/core/state.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #ask ⇒ Object
- #end_conversation ⇒ Object
- #initialize(data: StateData.new, adapter:, context: nil) ⇒ Object
- #listen(_message) ⇒ Object
- #next_state_action ⇒ Object
- #next_state_id ⇒ Object
- #repeat_action ⇒ Object
- #respond(message: nil, template: nil) ⇒ Object
- #send_message(to:, message:) ⇒ Object
- #transition_to(state_id, action: nil, data: {}) ⇒ Object
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
7 8 9 |
# File 'lib/socrates/core/state.rb', line 7 def context @context end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/socrates/core/state.rb', line 7 def data @data end |
Instance Method Details
#ask ⇒ Object
82 83 84 |
# File 'lib/socrates/core/state.rb', line 82 def ask # stub implementation, to be overwritten. end |
#end_conversation ⇒ Object
76 77 78 79 80 |
# File 'lib/socrates/core/state.rb', line 76 def end_conversation @data.clear transition_to END_OF_CONVERSATION, action: END_OF_CONVERSATION end |
#initialize(data: StateData.new, adapter:, context: nil) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/socrates/core/state.rb', line 9 def initialize(data: StateData.new, adapter:, context: nil) @data = data @adapter = adapter @context = context @next_state_id = nil @next_state_action = nil @logger = Socrates::Config.logger || Socrates::Logger.default end |
#listen(_message) ⇒ Object
86 87 88 |
# File 'lib/socrates/core/state.rb', line 86 def listen() # stub implementation, to be overwritten. end |
#next_state_action ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/socrates/core/state.rb', line 26 def next_state_action if @next_state_action.nil? next_action(@data.state_action) else @next_state_action end end |
#next_state_id ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/socrates/core/state.rb', line 18 def next_state_id if @next_state_id.nil? state_id_from_classname else @next_state_id end end |
#repeat_action ⇒ Object
71 72 73 74 |
# File 'lib/socrates/core/state.rb', line 71 def repeat_action @next_state_id = @data.state_id @next_state_action = @data.state_action end |
#respond(message: nil, template: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/socrates/core/state.rb', line 34 def respond(message: nil, template: nil) if template # TODO: Partials? filename = File.join(Socrates.config.view_path, template) source = File.read(filename) = ERB.new(source, 0, "<>").result(binding) end return if .empty? @logger.info %(#{client_id} send: "#{format_for_logging()}") @adapter.(, context: @context) end |
#send_message(to:, message:) ⇒ Object
48 49 50 51 |
# File 'lib/socrates/core/state.rb', line 48 def (to:, message:) @logger.info %(#{client_id} send direct to #{to}: "#{format_for_logging()}") @adapter.(, to, context: @context) end |
#transition_to(state_id, action: nil, data: {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/socrates/core/state.rb', line 53 def transition_to(state_id, action: nil, data: {}) if action.nil? action = if state_id.nil? nil elsif state_id == state_id_from_classname next_action(@data.state_action) else :ask end end @next_state_id = state_id @next_state_action = action @data.merge(data) end |