Class: Socrates::Core::StateData
- Inherits:
-
Object
- Object
- Socrates::Core::StateData
- Defined in:
- lib/socrates/core/state_data.rb
Instance Attribute Summary collapse
-
#state_action ⇒ Object
Returns the value of attribute state_action.
-
#state_id ⇒ Object
Returns the value of attribute state_id.
Class Method Summary collapse
Instance Method Summary collapse
- #clear(key = nil) ⇒ Object
- #get(key, clear: false) ⇒ Object
- #has_key?(key) ⇒ Boolean
- #has_temporary_key?(key) ⇒ Boolean
-
#initialize(state_id: nil, state_action: nil, data: {}, temporary_keys: []) ⇒ StateData
constructor
A new instance of StateData.
- #keys ⇒ Object
- #merge(other) ⇒ Object
- #serialize ⇒ Object
- #set(key, value) ⇒ Object
- #set_temporary(key, value) ⇒ Object
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 |
# 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 @data = data @temporary_keys = Set.new(temporary_keys) end |
Instance Attribute Details
#state_action ⇒ Object
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_id ⇒ Object
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 |
Class Method Details
.deserialize(string) ⇒ Object
73 74 75 |
# File 'lib/socrates/core/state_data.rb', line 73 def self.deserialize(string) YAML.load(string) end |
Instance Method Details
#clear(key = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/socrates/core/state_data.rb', line 59 def clear(key = nil) if key @data.delete(key) @temporary_keys.delete(key) else @data.clear @temporary_keys.clear end end |
#get(key, clear: false) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/socrates/core/state_data.rb', line 31 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
22 23 24 |
# File 'lib/socrates/core/state_data.rb', line 22 def has_key?(key) @data.has_key?(key) end |
#has_temporary_key?(key) ⇒ Boolean
26 27 28 29 |
# File 'lib/socrates/core/state_data.rb', line 26 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 |
#keys ⇒ Object
18 19 20 |
# File 'lib/socrates/core/state_data.rb', line 18 def keys @data.keys end |
#merge(other) ⇒ Object
55 56 57 |
# File 'lib/socrates/core/state_data.rb', line 55 def merge(other) @data.merge!(other) end |
#serialize ⇒ Object
69 70 71 |
# File 'lib/socrates/core/state_data.rb', line 69 def serialize YAML.dump(self) end |
#set(key, value) ⇒ Object
42 43 44 |
# File 'lib/socrates/core/state_data.rb', line 42 def set(key, value) @data[key] = value end |
#set_temporary(key, value) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/socrates/core/state_data.rb', line 46 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 |