Class: Socrates::Core::StateData

Inherits:
Object
  • Object
show all
Defined in:
lib/socrates/core/state_data.rb

Constant Summary collapse

END_OF_CONVERSATION =
:__end__

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_id: nil, state_action: nil, data: {}) ⇒ StateData

Returns a new instance of StateData.



14
15
16
17
18
19
# File 'lib/socrates/core/state_data.rb', line 14

def initialize(state_id: nil, state_action: nil, data: {})
  @state_id       = state_id
  @state_action   = state_action
  @data           = data
  @temporary_keys = []
end

Instance Attribute Details

#last_interaction_timestampObject

Returns the value of attribute last_interaction_timestamp.



12
13
14
# File 'lib/socrates/core/state_data.rb', line 12

def last_interaction_timestamp
  @last_interaction_timestamp
end

#state_actionObject

Returns the value of attribute state_action.



12
13
14
# File 'lib/socrates/core/state_data.rb', line 12

def state_action
  @state_action
end

#state_idObject

Returns the value of attribute state_id.



12
13
14
# File 'lib/socrates/core/state_data.rb', line 12

def state_id
  @state_id
end

Class Method Details

.deserialize(string) ⇒ Object



99
100
101
# File 'lib/socrates/core/state_data.rb', line 99

def self.deserialize(string)
  YAML.load(string)
end

Instance Method Details

#clear(key = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/socrates/core/state_data.rb', line 85

def clear(key = nil)
  if key
    @data.delete(key)
    @temporary_keys.delete(key)
  else
    @data.clear
    @temporary_keys.clear
  end
end

#each_key(&block) ⇒ Object



41
42
43
# File 'lib/socrates/core/state_data.rb', line 41

def each_key(&block)
  @data.each_key(&block)
end

#elapsed_timeObject



33
34
35
# File 'lib/socrates/core/state_data.rb', line 33

def elapsed_time
  Time.current - @last_interaction_timestamp
end

#expired?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/socrates/core/state_data.rb', line 25

def expired?
  return false if last_interaction_timestamp.nil? ||
                  Socrates.config.expired_timeout.nil? ||
                  Socrates.config.expired_timeout.zero?

  elapsed_time > Socrates.config.expired_timeout
end

#finished?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/socrates/core/state_data.rb', line 21

def finished?
  @state_id.nil? || @state_id == END_OF_CONVERSATION
end

#get(key, clear: false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/socrates/core/state_data.rb', line 57

def get(key, clear: false)
  value = @data[key]

  if @temporary_keys.include?(key) || clear
    @temporary_keys.delete(key)
    @data.delete(key)
  end

  value
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/socrates/core/state_data.rb', line 49

def has_key?(key)
  @data.has_key?(key)
end

#has_temporary_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/socrates/core/state_data.rb', line 53

def has_temporary_key?(key)
  @temporary_keys.include?(key)
end

#keysObject



45
46
47
# File 'lib/socrates/core/state_data.rb', line 45

def keys
  @data.keys
end

#merge(other) ⇒ Object



81
82
83
# File 'lib/socrates/core/state_data.rb', line 81

def merge(other)
  @data.merge!(other)
end

#reset_elapsed_timeObject



37
38
39
# File 'lib/socrates/core/state_data.rb', line 37

def reset_elapsed_time
  @last_interaction_timestamp = Time.current
end

#serializeObject



95
96
97
# File 'lib/socrates/core/state_data.rb', line 95

def serialize
  YAML.dump(self)
end

#set(key, value) ⇒ Object



68
69
70
# File 'lib/socrates/core/state_data.rb', line 68

def set(key, value)
  @data[key] = value
end

#set_temporary(key, value) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/socrates/core/state_data.rb', line 72

def set_temporary(key, value)
  if @data.has_key?(key) && !@temporary_keys.include?(key)
    raise ArgumentError, "Cannot overrite key '#{key}' with a temporary value."
  end

  @data[key] = value
  @temporary_keys << key
end