Class: State

Inherits:
Object
  • Object
show all
Defined in:
lib/rct/state.rb

Overview


This is the global state where mostly everything is kept in rct.

There are two kinds of state, permanent and temporary. Values set in temporary state can be removed with the reset() method. Values in permanent state remain available for the lifetime of this State object (unless individually removed with delete() method).

When retrieving a value, content in the temporary state overrides content in permanent state if the key is present in both. This provides a way to temporarily override a global default.

Instance Method Summary collapse

Constructor Details

#initializeState


Constructor…



39
40
41
42
# File 'lib/rct/state.rb', line 39

def initialize
  @h = Hash.new()
  @tmp = Hash.new()
end

Instance Method Details

#delete(key) ⇒ Object


Delete the given ‘key’ from both permanent and temporary state.



73
74
75
76
# File 'lib/rct/state.rb', line 73

def delete(key)
  @h.delete(key)
  @tmp.delete(key)
end

#get(key) ⇒ Object


Get the value of ‘key’ (if available, or returns nil).



61
62
63
64
65
66
67
# File 'lib/rct/state.rb', line 61

def get(key)
  if (@tmp[key] != nil)
    return @tmp[key]
  else
    return @h[key]
  end
end

#resetObject


Reset temporary state. Returns the old temporary state hash object.



83
84
85
86
87
# File 'lib/rct/state.rb', line 83

def reset
  tmp = @tmp
  @tmp = Hash.new
  return tmp
end

#set(key, value, temp = false) ⇒ Object


Set (or change) ‘key’ in state to contain ‘value’. If ‘temp’ is true, this key is stored in temporary state only.



49
50
51
52
53
54
55
# File 'lib/rct/state.rb', line 49

def set(key, value, temp=false)
  if (temp)
    @tmp[key] = value
  else
    @h[key] = value
  end
end