Class: QuickState

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = nil) ⇒ QuickState

Returns a new instance of QuickState.



4
5
6
7
8
9
10
11
# File 'lib/quick_state.rb', line 4

def initialize(file_path=nil)
  @file_path = file_path || "#{$0}.state"
  if File.exist?(@file_path)
    @data = Marshal.load(File.read(@file_path))
  else
    @data = {}
  end
end

Instance Attribute Details

#autosaveObject

Returns the value of attribute autosave.



2
3
4
# File 'lib/quick_state.rb', line 2

def autosave
  @autosave
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
# File 'lib/quick_state.rb', line 13

def [](key)
  @data[key]
end

#[]=(key, val) ⇒ Object



17
18
19
20
21
# File 'lib/quick_state.rb', line 17

def []=(key,val)
  @data[key] = val
  save if autosave
  val
end

#autosave!Object



27
28
29
30
# File 'lib/quick_state.rb', line 27

def autosave!
  self.autosave = true
  self
end

#saveObject



23
24
25
# File 'lib/quick_state.rb', line 23

def save
  File.open(@file_path, "w") {|f| Marshal.dump(@data, f); f.fsync}
end