Sha256: 01b1ebc27225e5020ee2e27cd5ec3e46ad40a6929eb33bba1acab8eb980dc94d

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Tataru
  # The state of the environment
  class State
    def initialize(objstate = {})
      @current_ids = objstate[:current_ids] || {}
      @replacer_ids = objstate[:replacer_ids] || {}
      @waiting_ids = objstate[:waiting_ids] || {}
    end

    def putstate(id, state, value, replacer: false)
      ids = id_list(replacer)
      ids[id] = {} unless ids.key? id
      ids[id][state] = value
    end

    def getstate(id, state, replacer: false)
      ids = id_list(replacer)
      return unless ids.key? id

      ids[id][state]
    end

    def id_list(replacer = false)
      return @replacer_ids if replacer

      @current_ids
    end

    def replace(id)
      @current_ids[id] = @replacer_ids[id]
      @replacer_ids.delete(id)
    end

    def delete_list
      @replacer_ids.keys.select { |x| @current_ids.key? x }
    end

    def [](id)
      @current_ids[id].clone
    end

    def waiting_on(id, what)
      @waiting_ids[id] = what
    end

    def no_longer_waiting(id)
      @waiting_ids.delete(id)
    end

    def waiting_list
      @waiting_ids.clone
    end

    def to_h
      {
        current_ids: @current_ids,
        replacer_ids: @replacer_ids,
        waiting_ids: @waiting_ids
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tataru-0.1.0 lib/tataru/state.rb