Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby32/share/ruby
File: pstore.rb
# frozen_string_literal: true
[0] Fix | Delete
# = PStore -- Transactional File Storage for Ruby Objects
[1] Fix | Delete
#
[2] Fix | Delete
# pstore.rb -
[3] Fix | Delete
# originally by matz
[4] Fix | Delete
# documentation by Kev Jackson and James Edward Gray II
[5] Fix | Delete
# improved by Hongli Lai
[6] Fix | Delete
#
[7] Fix | Delete
# See PStore for documentation.
[8] Fix | Delete
[9] Fix | Delete
require "digest"
[10] Fix | Delete
[11] Fix | Delete
# \PStore implements a file based persistence mechanism based on a Hash.
[12] Fix | Delete
# User code can store hierarchies of Ruby objects (values)
[13] Fix | Delete
# into the data store by name (keys).
[14] Fix | Delete
# An object hierarchy may be just a single object.
[15] Fix | Delete
# User code may later read values back from the data store
[16] Fix | Delete
# or even update data, as needed.
[17] Fix | Delete
#
[18] Fix | Delete
# The transactional behavior ensures that any changes succeed or fail together.
[19] Fix | Delete
# This can be used to ensure that the data store is not left in a transitory state,
[20] Fix | Delete
# where some values were updated but others were not.
[21] Fix | Delete
#
[22] Fix | Delete
# Behind the scenes, Ruby objects are stored to the data store file with Marshal.
[23] Fix | Delete
# That carries the usual limitations. Proc objects cannot be marshalled,
[24] Fix | Delete
# for example.
[25] Fix | Delete
#
[26] Fix | Delete
# There are three important concepts here (details at the links):
[27] Fix | Delete
#
[28] Fix | Delete
# - {Store}[rdoc-ref:PStore@The+Store]: a store is an instance of \PStore.
[29] Fix | Delete
# - {Entries}[rdoc-ref:PStore@Entries]: the store is hash-like;
[30] Fix | Delete
# each entry is the key for a stored object.
[31] Fix | Delete
# - {Transactions}[rdoc-ref:PStore@Transactions]: each transaction is a collection
[32] Fix | Delete
# of prospective changes to the store;
[33] Fix | Delete
# a transaction is defined in the block given with a call
[34] Fix | Delete
# to PStore#transaction.
[35] Fix | Delete
#
[36] Fix | Delete
# == About the Examples
[37] Fix | Delete
#
[38] Fix | Delete
# Examples on this page need a store that has known properties.
[39] Fix | Delete
# They can get a new (and populated) store by calling thus:
[40] Fix | Delete
#
[41] Fix | Delete
# example_store do |store|
[42] Fix | Delete
# # Example code using store goes here.
[43] Fix | Delete
# end
[44] Fix | Delete
#
[45] Fix | Delete
# All we really need to know about +example_store+
[46] Fix | Delete
# is that it yields a fresh store with a known population of entries;
[47] Fix | Delete
# its implementation:
[48] Fix | Delete
#
[49] Fix | Delete
# require 'pstore'
[50] Fix | Delete
# require 'tempfile'
[51] Fix | Delete
# # Yield a pristine store for use in examples.
[52] Fix | Delete
# def example_store
[53] Fix | Delete
# # Create the store in a temporary file.
[54] Fix | Delete
# Tempfile.create do |file|
[55] Fix | Delete
# store = PStore.new(file)
[56] Fix | Delete
# # Populate the store.
[57] Fix | Delete
# store.transaction do
[58] Fix | Delete
# store[:foo] = 0
[59] Fix | Delete
# store[:bar] = 1
[60] Fix | Delete
# store[:baz] = 2
[61] Fix | Delete
# end
[62] Fix | Delete
# yield store
[63] Fix | Delete
# end
[64] Fix | Delete
# end
[65] Fix | Delete
#
[66] Fix | Delete
# == The Store
[67] Fix | Delete
#
[68] Fix | Delete
# The contents of the store are maintained in a file whose path is specified
[69] Fix | Delete
# when the store is created (see PStore.new).
[70] Fix | Delete
# The objects are stored and retrieved using
[71] Fix | Delete
# module Marshal, which means that certain objects cannot be added to the store;
[72] Fix | Delete
# see {Marshal::dump}[rdoc-ref:Marshal.dump].
[73] Fix | Delete
#
[74] Fix | Delete
# == Entries
[75] Fix | Delete
#
[76] Fix | Delete
# A store may have any number of entries.
[77] Fix | Delete
# Each entry has a key and a value, just as in a hash:
[78] Fix | Delete
#
[79] Fix | Delete
# - Key: as in a hash, the key can be (almost) any object;
[80] Fix | Delete
# see {Hash Keys}[rdoc-ref:Hash@Hash+Keys].
[81] Fix | Delete
# You may find it convenient to keep it simple by using only
[82] Fix | Delete
# symbols or strings as keys.
[83] Fix | Delete
# - Value: the value may be any object that can be marshalled by \Marshal
[84] Fix | Delete
# (see {Marshal::dump}[rdoc-ref:Marshal.dump])
[85] Fix | Delete
# and in fact may be a collection
[86] Fix | Delete
# (e.g., an array, a hash, a set, a range, etc).
[87] Fix | Delete
# That collection may in turn contain nested objects,
[88] Fix | Delete
# including collections, to any depth;
[89] Fix | Delete
# those objects must also be \Marshal-able.
[90] Fix | Delete
# See {Hierarchical Values}[rdoc-ref:PStore@Hierarchical+Values].
[91] Fix | Delete
#
[92] Fix | Delete
# == Transactions
[93] Fix | Delete
#
[94] Fix | Delete
# === The Transaction Block
[95] Fix | Delete
#
[96] Fix | Delete
# The block given with a call to method #transaction#
[97] Fix | Delete
# contains a _transaction_,
[98] Fix | Delete
# which consists of calls to \PStore methods that
[99] Fix | Delete
# read from or write to the store
[100] Fix | Delete
# (that is, all \PStore methods except #transaction itself,
[101] Fix | Delete
# #path, and Pstore.new):
[102] Fix | Delete
#
[103] Fix | Delete
# example_store do |store|
[104] Fix | Delete
# store.transaction do
[105] Fix | Delete
# store.keys # => [:foo, :bar, :baz]
[106] Fix | Delete
# store[:bat] = 3
[107] Fix | Delete
# store.keys # => [:foo, :bar, :baz, :bat]
[108] Fix | Delete
# end
[109] Fix | Delete
# end
[110] Fix | Delete
#
[111] Fix | Delete
# Execution of the transaction is deferred until the block exits,
[112] Fix | Delete
# and is executed _atomically_ (all-or-nothing):
[113] Fix | Delete
# either all transaction calls are executed, or none are.
[114] Fix | Delete
# This maintains the integrity of the store.
[115] Fix | Delete
#
[116] Fix | Delete
# Other code in the block (including even calls to #path and PStore.new)
[117] Fix | Delete
# is executed immediately, not deferred.
[118] Fix | Delete
#
[119] Fix | Delete
# The transaction block:
[120] Fix | Delete
#
[121] Fix | Delete
# - May not contain a nested call to #transaction.
[122] Fix | Delete
# - Is the only context where methods that read from or write to
[123] Fix | Delete
# the store are allowed.
[124] Fix | Delete
#
[125] Fix | Delete
# As seen above, changes in a transaction are made automatically
[126] Fix | Delete
# when the block exits.
[127] Fix | Delete
# The block may be exited early by calling method #commit or #abort.
[128] Fix | Delete
#
[129] Fix | Delete
# - Method #commit triggers the update to the store and exits the block:
[130] Fix | Delete
#
[131] Fix | Delete
# example_store do |store|
[132] Fix | Delete
# store.transaction do
[133] Fix | Delete
# store.keys # => [:foo, :bar, :baz]
[134] Fix | Delete
# store[:bat] = 3
[135] Fix | Delete
# store.commit
[136] Fix | Delete
# fail 'Cannot get here'
[137] Fix | Delete
# end
[138] Fix | Delete
# store.transaction do
[139] Fix | Delete
# # Update was completed.
[140] Fix | Delete
# store.keys # => [:foo, :bar, :baz, :bat]
[141] Fix | Delete
# end
[142] Fix | Delete
# end
[143] Fix | Delete
#
[144] Fix | Delete
# - Method #abort discards the update to the store and exits the block:
[145] Fix | Delete
#
[146] Fix | Delete
# example_store do |store|
[147] Fix | Delete
# store.transaction do
[148] Fix | Delete
# store.keys # => [:foo, :bar, :baz]
[149] Fix | Delete
# store[:bat] = 3
[150] Fix | Delete
# store.abort
[151] Fix | Delete
# fail 'Cannot get here'
[152] Fix | Delete
# end
[153] Fix | Delete
# store.transaction do
[154] Fix | Delete
# # Update was not completed.
[155] Fix | Delete
# store.keys # => [:foo, :bar, :baz]
[156] Fix | Delete
# end
[157] Fix | Delete
# end
[158] Fix | Delete
#
[159] Fix | Delete
# === Read-Only Transactions
[160] Fix | Delete
#
[161] Fix | Delete
# By default, a transaction allows both reading from and writing to
[162] Fix | Delete
# the store:
[163] Fix | Delete
#
[164] Fix | Delete
# store.transaction do
[165] Fix | Delete
# # Read-write transaction.
[166] Fix | Delete
# # Any code except a call to #transaction is allowed here.
[167] Fix | Delete
# end
[168] Fix | Delete
#
[169] Fix | Delete
# If argument +read_only+ is passed as +true+,
[170] Fix | Delete
# only reading is allowed:
[171] Fix | Delete
#
[172] Fix | Delete
# store.transaction(true) do
[173] Fix | Delete
# # Read-only transaction:
[174] Fix | Delete
# # Calls to #transaction, #[]=, and #delete are not allowed here.
[175] Fix | Delete
# end
[176] Fix | Delete
#
[177] Fix | Delete
# == Hierarchical Values
[178] Fix | Delete
#
[179] Fix | Delete
# The value for an entry may be a simple object (as seen above).
[180] Fix | Delete
# It may also be a hierarchy of objects nested to any depth:
[181] Fix | Delete
#
[182] Fix | Delete
# deep_store = PStore.new('deep.store')
[183] Fix | Delete
# deep_store.transaction do
[184] Fix | Delete
# array_of_hashes = [{}, {}, {}]
[185] Fix | Delete
# deep_store[:array_of_hashes] = array_of_hashes
[186] Fix | Delete
# deep_store[:array_of_hashes] # => [{}, {}, {}]
[187] Fix | Delete
# hash_of_arrays = {foo: [], bar: [], baz: []}
[188] Fix | Delete
# deep_store[:hash_of_arrays] = hash_of_arrays
[189] Fix | Delete
# deep_store[:hash_of_arrays] # => {:foo=>[], :bar=>[], :baz=>[]}
[190] Fix | Delete
# deep_store[:hash_of_arrays][:foo].push(:bat)
[191] Fix | Delete
# deep_store[:hash_of_arrays] # => {:foo=>[:bat], :bar=>[], :baz=>[]}
[192] Fix | Delete
# end
[193] Fix | Delete
#
[194] Fix | Delete
# And recall that you can use
[195] Fix | Delete
# {dig methods}[rdoc-ref:dig_methods.rdoc]
[196] Fix | Delete
# in a returned hierarchy of objects.
[197] Fix | Delete
#
[198] Fix | Delete
# == Working with the Store
[199] Fix | Delete
#
[200] Fix | Delete
# === Creating a Store
[201] Fix | Delete
#
[202] Fix | Delete
# Use method PStore.new to create a store.
[203] Fix | Delete
# The new store creates or opens its containing file:
[204] Fix | Delete
#
[205] Fix | Delete
# store = PStore.new('t.store')
[206] Fix | Delete
#
[207] Fix | Delete
# === Modifying the Store
[208] Fix | Delete
#
[209] Fix | Delete
# Use method #[]= to update or create an entry:
[210] Fix | Delete
#
[211] Fix | Delete
# example_store do |store|
[212] Fix | Delete
# store.transaction do
[213] Fix | Delete
# store[:foo] = 1 # Update.
[214] Fix | Delete
# store[:bam] = 1 # Create.
[215] Fix | Delete
# end
[216] Fix | Delete
# end
[217] Fix | Delete
#
[218] Fix | Delete
# Use method #delete to remove an entry:
[219] Fix | Delete
#
[220] Fix | Delete
# example_store do |store|
[221] Fix | Delete
# store.transaction do
[222] Fix | Delete
# store.delete(:foo)
[223] Fix | Delete
# store[:foo] # => nil
[224] Fix | Delete
# end
[225] Fix | Delete
# end
[226] Fix | Delete
#
[227] Fix | Delete
# === Retrieving Values
[228] Fix | Delete
#
[229] Fix | Delete
# Use method #fetch (allows default) or #[] (defaults to +nil+)
[230] Fix | Delete
# to retrieve an entry:
[231] Fix | Delete
#
[232] Fix | Delete
# example_store do |store|
[233] Fix | Delete
# store.transaction do
[234] Fix | Delete
# store[:foo] # => 0
[235] Fix | Delete
# store[:nope] # => nil
[236] Fix | Delete
# store.fetch(:baz) # => 2
[237] Fix | Delete
# store.fetch(:nope, nil) # => nil
[238] Fix | Delete
# store.fetch(:nope) # Raises exception.
[239] Fix | Delete
# end
[240] Fix | Delete
# end
[241] Fix | Delete
#
[242] Fix | Delete
# === Querying the Store
[243] Fix | Delete
#
[244] Fix | Delete
# Use method #key? to determine whether a given key exists:
[245] Fix | Delete
#
[246] Fix | Delete
# example_store do |store|
[247] Fix | Delete
# store.transaction do
[248] Fix | Delete
# store.key?(:foo) # => true
[249] Fix | Delete
# end
[250] Fix | Delete
# end
[251] Fix | Delete
#
[252] Fix | Delete
# Use method #keys to retrieve keys:
[253] Fix | Delete
#
[254] Fix | Delete
# example_store do |store|
[255] Fix | Delete
# store.transaction do
[256] Fix | Delete
# store.keys # => [:foo, :bar, :baz]
[257] Fix | Delete
# end
[258] Fix | Delete
# end
[259] Fix | Delete
#
[260] Fix | Delete
# Use method #path to retrieve the path to the store's underlying file;
[261] Fix | Delete
# this method may be called from outside a transaction block:
[262] Fix | Delete
#
[263] Fix | Delete
# store = PStore.new('t.store')
[264] Fix | Delete
# store.path # => "t.store"
[265] Fix | Delete
#
[266] Fix | Delete
# == Transaction Safety
[267] Fix | Delete
#
[268] Fix | Delete
# For transaction safety, see:
[269] Fix | Delete
#
[270] Fix | Delete
# - Optional argument +thread_safe+ at method PStore.new.
[271] Fix | Delete
# - Attribute #ultra_safe.
[272] Fix | Delete
#
[273] Fix | Delete
# Needless to say, if you're storing valuable data with \PStore, then you should
[274] Fix | Delete
# backup the \PStore file from time to time.
[275] Fix | Delete
#
[276] Fix | Delete
# == An Example Store
[277] Fix | Delete
#
[278] Fix | Delete
# require "pstore"
[279] Fix | Delete
#
[280] Fix | Delete
# # A mock wiki object.
[281] Fix | Delete
# class WikiPage
[282] Fix | Delete
#
[283] Fix | Delete
# attr_reader :page_name
[284] Fix | Delete
#
[285] Fix | Delete
# def initialize(page_name, author, contents)
[286] Fix | Delete
# @page_name = page_name
[287] Fix | Delete
# @revisions = Array.new
[288] Fix | Delete
# add_revision(author, contents)
[289] Fix | Delete
# end
[290] Fix | Delete
#
[291] Fix | Delete
# def add_revision(author, contents)
[292] Fix | Delete
# @revisions << {created: Time.now,
[293] Fix | Delete
# author: author,
[294] Fix | Delete
# contents: contents}
[295] Fix | Delete
# end
[296] Fix | Delete
#
[297] Fix | Delete
# def wiki_page_references
[298] Fix | Delete
# [@page_name] + @revisions.last[:contents].scan(/\b(?:[A-Z]+[a-z]+){2,}/)
[299] Fix | Delete
# end
[300] Fix | Delete
#
[301] Fix | Delete
# end
[302] Fix | Delete
#
[303] Fix | Delete
# # Create a new wiki page.
[304] Fix | Delete
# home_page = WikiPage.new("HomePage", "James Edward Gray II",
[305] Fix | Delete
# "A page about the JoysOfDocumentation..." )
[306] Fix | Delete
#
[307] Fix | Delete
# wiki = PStore.new("wiki_pages.pstore")
[308] Fix | Delete
# # Update page data and the index together, or not at all.
[309] Fix | Delete
# wiki.transaction do
[310] Fix | Delete
# # Store page.
[311] Fix | Delete
# wiki[home_page.page_name] = home_page
[312] Fix | Delete
# # Create page index.
[313] Fix | Delete
# wiki[:wiki_index] ||= Array.new
[314] Fix | Delete
# # Update wiki index.
[315] Fix | Delete
# wiki[:wiki_index].push(*home_page.wiki_page_references)
[316] Fix | Delete
# end
[317] Fix | Delete
#
[318] Fix | Delete
# # Read wiki data, setting argument read_only to true.
[319] Fix | Delete
# wiki.transaction(true) do
[320] Fix | Delete
# wiki.keys.each do |key|
[321] Fix | Delete
# puts key
[322] Fix | Delete
# puts wiki[key]
[323] Fix | Delete
# end
[324] Fix | Delete
# end
[325] Fix | Delete
#
[326] Fix | Delete
class PStore
[327] Fix | Delete
VERSION = "0.1.2"
[328] Fix | Delete
[329] Fix | Delete
RDWR_ACCESS = {mode: IO::RDWR | IO::CREAT | IO::BINARY, encoding: Encoding::ASCII_8BIT}.freeze
[330] Fix | Delete
RD_ACCESS = {mode: IO::RDONLY | IO::BINARY, encoding: Encoding::ASCII_8BIT}.freeze
[331] Fix | Delete
WR_ACCESS = {mode: IO::WRONLY | IO::CREAT | IO::TRUNC | IO::BINARY, encoding: Encoding::ASCII_8BIT}.freeze
[332] Fix | Delete
[333] Fix | Delete
# The error type thrown by all PStore methods.
[334] Fix | Delete
class Error < StandardError
[335] Fix | Delete
end
[336] Fix | Delete
[337] Fix | Delete
# Whether \PStore should do its best to prevent file corruptions,
[338] Fix | Delete
# even when an unlikely error (such as memory-error or filesystem error) occurs:
[339] Fix | Delete
#
[340] Fix | Delete
# - +true+: changes are posted by creating a temporary file,
[341] Fix | Delete
# writing the updated data to it, then renaming the file to the given #path.
[342] Fix | Delete
# File integrity is maintained.
[343] Fix | Delete
# Note: has effect only if the filesystem has atomic file rename
[344] Fix | Delete
# (as do POSIX platforms Linux, MacOS, FreeBSD and others).
[345] Fix | Delete
#
[346] Fix | Delete
# - +false+ (the default): changes are posted by rewinding the open file
[347] Fix | Delete
# and writing the updated data.
[348] Fix | Delete
# File integrity is maintained if the filesystem raises
[349] Fix | Delete
# no unexpected I/O error;
[350] Fix | Delete
# if such an error occurs during a write to the store,
[351] Fix | Delete
# the file may become corrupted.
[352] Fix | Delete
#
[353] Fix | Delete
attr_accessor :ultra_safe
[354] Fix | Delete
[355] Fix | Delete
# Returns a new \PStore object.
[356] Fix | Delete
#
[357] Fix | Delete
# Argument +file+ is the path to the file in which objects are to be stored;
[358] Fix | Delete
# if the file exists, it should be one that was written by \PStore.
[359] Fix | Delete
#
[360] Fix | Delete
# path = 't.store'
[361] Fix | Delete
# store = PStore.new(path)
[362] Fix | Delete
#
[363] Fix | Delete
# A \PStore object is
[364] Fix | Delete
# {reentrant}[https://en.wikipedia.org/wiki/Reentrancy_(computing)].
[365] Fix | Delete
# If argument +thread_safe+ is given as +true+,
[366] Fix | Delete
# the object is also thread-safe (at the cost of a small performance penalty):
[367] Fix | Delete
#
[368] Fix | Delete
# store = PStore.new(path, true)
[369] Fix | Delete
#
[370] Fix | Delete
def initialize(file, thread_safe = false)
[371] Fix | Delete
dir = File::dirname(file)
[372] Fix | Delete
unless File::directory? dir
[373] Fix | Delete
raise PStore::Error, format("directory %s does not exist", dir)
[374] Fix | Delete
end
[375] Fix | Delete
if File::exist? file and not File::readable? file
[376] Fix | Delete
raise PStore::Error, format("file %s not readable", file)
[377] Fix | Delete
end
[378] Fix | Delete
@filename = file
[379] Fix | Delete
@abort = false
[380] Fix | Delete
@ultra_safe = false
[381] Fix | Delete
@thread_safe = thread_safe
[382] Fix | Delete
@lock = Thread::Mutex.new
[383] Fix | Delete
end
[384] Fix | Delete
[385] Fix | Delete
# Raises PStore::Error if the calling code is not in a PStore#transaction.
[386] Fix | Delete
def in_transaction
[387] Fix | Delete
raise PStore::Error, "not in transaction" unless @lock.locked?
[388] Fix | Delete
end
[389] Fix | Delete
#
[390] Fix | Delete
# Raises PStore::Error if the calling code is not in a PStore#transaction or
[391] Fix | Delete
# if the code is in a read-only PStore#transaction.
[392] Fix | Delete
#
[393] Fix | Delete
def in_transaction_wr
[394] Fix | Delete
in_transaction
[395] Fix | Delete
raise PStore::Error, "in read-only transaction" if @rdonly
[396] Fix | Delete
end
[397] Fix | Delete
private :in_transaction, :in_transaction_wr
[398] Fix | Delete
[399] Fix | Delete
# Returns the value for the given +key+ if the key exists.
[400] Fix | Delete
# +nil+ otherwise;
[401] Fix | Delete
# if not +nil+, the returned value is an object or a hierarchy of objects:
[402] Fix | Delete
#
[403] Fix | Delete
# example_store do |store|
[404] Fix | Delete
# store.transaction do
[405] Fix | Delete
# store[:foo] # => 0
[406] Fix | Delete
# store[:nope] # => nil
[407] Fix | Delete
# end
[408] Fix | Delete
# end
[409] Fix | Delete
#
[410] Fix | Delete
# Returns +nil+ if there is no such key.
[411] Fix | Delete
#
[412] Fix | Delete
# See also {Hierarchical Values}[rdoc-ref:PStore@Hierarchical+Values].
[413] Fix | Delete
#
[414] Fix | Delete
# Raises an exception if called outside a transaction block.
[415] Fix | Delete
def [](key)
[416] Fix | Delete
in_transaction
[417] Fix | Delete
@table[key]
[418] Fix | Delete
end
[419] Fix | Delete
[420] Fix | Delete
# Like #[], except that it accepts a default value for the store.
[421] Fix | Delete
# If the +key+ does not exist:
[422] Fix | Delete
#
[423] Fix | Delete
# - Raises an exception if +default+ is +PStore::Error+.
[424] Fix | Delete
# - Returns the value of +default+ otherwise:
[425] Fix | Delete
#
[426] Fix | Delete
# example_store do |store|
[427] Fix | Delete
# store.transaction do
[428] Fix | Delete
# store.fetch(:nope, nil) # => nil
[429] Fix | Delete
# store.fetch(:nope) # Raises an exception.
[430] Fix | Delete
# end
[431] Fix | Delete
# end
[432] Fix | Delete
#
[433] Fix | Delete
# Raises an exception if called outside a transaction block.
[434] Fix | Delete
def fetch(key, default=PStore::Error)
[435] Fix | Delete
in_transaction
[436] Fix | Delete
unless @table.key? key
[437] Fix | Delete
if default == PStore::Error
[438] Fix | Delete
raise PStore::Error, format("undefined key `%s'", key)
[439] Fix | Delete
else
[440] Fix | Delete
return default
[441] Fix | Delete
end
[442] Fix | Delete
end
[443] Fix | Delete
@table[key]
[444] Fix | Delete
end
[445] Fix | Delete
[446] Fix | Delete
# Creates or replaces the value for the given +key+:
[447] Fix | Delete
#
[448] Fix | Delete
# example_store do |store|
[449] Fix | Delete
# temp.transaction do
[450] Fix | Delete
# temp[:bat] = 3
[451] Fix | Delete
# end
[452] Fix | Delete
# end
[453] Fix | Delete
#
[454] Fix | Delete
# See also {Hierarchical Values}[rdoc-ref:PStore@Hierarchical+Values].
[455] Fix | Delete
#
[456] Fix | Delete
# Raises an exception if called outside a transaction block.
[457] Fix | Delete
def []=(key, value)
[458] Fix | Delete
in_transaction_wr
[459] Fix | Delete
@table[key] = value
[460] Fix | Delete
end
[461] Fix | Delete
[462] Fix | Delete
# Removes and returns the value at +key+ if it exists:
[463] Fix | Delete
#
[464] Fix | Delete
# example_store do |store|
[465] Fix | Delete
# store.transaction do
[466] Fix | Delete
# store[:bat] = 3
[467] Fix | Delete
# store.delete(:bat)
[468] Fix | Delete
# end
[469] Fix | Delete
# end
[470] Fix | Delete
#
[471] Fix | Delete
# Returns +nil+ if there is no such key.
[472] Fix | Delete
#
[473] Fix | Delete
# Raises an exception if called outside a transaction block.
[474] Fix | Delete
def delete(key)
[475] Fix | Delete
in_transaction_wr
[476] Fix | Delete
@table.delete key
[477] Fix | Delete
end
[478] Fix | Delete
[479] Fix | Delete
# Returns an array of the existing keys:
[480] Fix | Delete
#
[481] Fix | Delete
# example_store do |store|
[482] Fix | Delete
# store.transaction do
[483] Fix | Delete
# store.keys # => [:foo, :bar, :baz]
[484] Fix | Delete
# end
[485] Fix | Delete
# end
[486] Fix | Delete
#
[487] Fix | Delete
# Raises an exception if called outside a transaction block.
[488] Fix | Delete
#
[489] Fix | Delete
# PStore#roots is an alias for PStore#keys.
[490] Fix | Delete
def keys
[491] Fix | Delete
in_transaction
[492] Fix | Delete
@table.keys
[493] Fix | Delete
end
[494] Fix | Delete
alias roots keys
[495] Fix | Delete
[496] Fix | Delete
# Returns +true+ if +key+ exists, +false+ otherwise:
[497] Fix | Delete
#
[498] Fix | Delete
# example_store do |store|
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function