Class: Socrates::Core::StateData

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of StateData.



11
12
13
14
15
16
17
# File 'lib/socrates/core/state_data.rb', line 11

def initialize(state_id: nil, state_action: nil, data: {}, temporary_keys: [])
  @state_id       = state_id
  @state_action   = state_action
  @timestamp      = Time.current
  @data           = data
  @temporary_keys = Set.new(temporary_keys)
end

Instance Attribute Details

#state_actionObject

Returns the value of attribute state_action.



9
10
11
# File 'lib/socrates/core/state_data.rb', line 9

def state_action
  @state_action
end

#state_idObject

Returns the value of attribute state_id.



9
10
11
# File 'lib/socrates/core/state_data.rb', line 9

def state_id
  @state_id
end

#timestampObject

Returns the value of attribute timestamp.



9
10
11
# File 'lib/socrates/core/state_data.rb', line 9

def timestamp
  @timestamp
end

Class Method Details

.deserialize(string) ⇒ Object



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

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

Instance Method Details

#clear(key = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/socrates/core/state_data.rb', line 68

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

#elapsed_timeObject



19
20
21
# File 'lib/socrates/core/state_data.rb', line 19

def elapsed_time
  Time.current - @timestamp
end

#get(key, clear: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/socrates/core/state_data.rb', line 40

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)


31
32
33
# File 'lib/socrates/core/state_data.rb', line 31

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

#has_temporary_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_temporary_key?(key)
  # The !! turns nils into false, which shouldn"t be necessary, but seems to be after the set is loaded from yaml.
  @temporary_keys.include?(key) == true
end

#keysObject



27
28
29
# File 'lib/socrates/core/state_data.rb', line 27

def keys
  @data.keys
end

#merge(other) ⇒ Object



64
65
66
# File 'lib/socrates/core/state_data.rb', line 64

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

#reset_elapsed_timeObject



23
24
25
# File 'lib/socrates/core/state_data.rb', line 23

def reset_elapsed_time
  @timestamp = Time.current
end

#serializeObject



78
79
80
# File 'lib/socrates/core/state_data.rb', line 78

def serialize
  YAML.dump(self)
end

#set(key, value) ⇒ Object



51
52
53
# File 'lib/socrates/core/state_data.rb', line 51

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

#set_temporary(key, value) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/socrates/core/state_data.rb', line 55

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