Sha256: 859cdfd0f4db23308cfddd0225fc526e783f3d2bd7ec42d16fec244d80055781

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

require "hashie"
require "json"
require "set"
require "yaml"

module Socrates
  module Core
    class StateData
      attr_accessor :state_id, :state_action, :timestamp

      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

      def elapsed_time
        Time.current - @timestamp
      end

      def reset_elapsed_time
        @timestamp = Time.current
      end

      def keys
        @data.keys
      end

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

      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

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

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

        value
      end

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

      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

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

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

      def serialize
        YAML.dump(self)
      end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
socrates-0.1.7 lib/socrates/core/state_data.rb
socrates-0.1.6 lib/socrates/core/state_data.rb
socrates-0.1.4 lib/socrates/core/state_data.rb
socrates-0.1.3 lib/socrates/core/state_data.rb
socrates-0.1.2 lib/socrates/core/state_data.rb