class Rack::State::Store::File

File-based state storage adapter. The state token is the file name and the state object is Marshaled and stored in the file.

Public Class Methods

new(directory = Dir.tmpdir) click to toggle source
directory

Storage directory for state files. It must exist and have proper permissions.

# File lib/rack/state.rb, line 261
def initialize(directory = Dir.tmpdir)
  @directory = directory
end

Public Instance Methods

create(token, object) click to toggle source
# File lib/rack/state.rb, line 265
def create(token, object)
  flags = ::File::WRONLY|::File::CREAT|::File::EXCL

  ::File.open(state_file(token), flags, 0600) do |f|
    f.flock ::File::LOCK_EX
    f.write ::Marshal.dump(object)
  end
rescue Errno::EEXIST
  raise KeyError
end
delete(token) click to toggle source
# File lib/rack/state.rb, line 294
def delete(token)
  ::File.unlink state_file(token) rescue nil
end
read(token) click to toggle source
# File lib/rack/state.rb, line 276
def read(token)
  ::File.open(state_file(token)) do |f|
    f.flock ::File::LOCK_SH
    ::Marshal.load(f) if f.size > 0
  end rescue nil
end
update(token, object) click to toggle source
# File lib/rack/state.rb, line 283
def update(token, object)
  flags = ::File::WRONLY|::File::TRUNC

  ::File.open(state_file(token), flags) do |f|
    f.flock ::File::LOCK_EX
    f.write ::Marshal.dump(object)
  end
rescue Errno::ENOENT
  raise KeyError
end