10
10
11
11
require "digest"
12
12
13
- # An instance of class \PStore can store and retrieve Ruby objects --
14
- # not just strings or raw data, but objects of many kinds.
13
+ # \PStore implements a file based persistence mechanism based on a Hash.
14
+ # User code can store hierarchies of Ruby objects (values)
15
+ # into the data store by name (keys).
16
+ # An object hierarchy may be just a single object.
17
+ # User code may later read values back from the data store
18
+ # or even update data, as needed.
19
+ #
20
+ # The transactional behavior ensures that any changes succeed or fail together.
21
+ # This can be used to ensure that the data store is not left in a transitory state,
22
+ # where some values were updated but others were not.
23
+ #
24
+ # Behind the scenes, Ruby objects are stored to the data store file with Marshal.
25
+ # That carries the usual limitations. Proc objects cannot be marshalled,
26
+ # for example.
27
+ #
15
28
# There are three key terms here (details at the links):
16
29
#
17
30
# - {Store}[rdoc-ref:PStore@The+Store]: a store is an instance of \PStore.
18
31
# - {Roots}[rdoc-ref:PStore@Roots]: the store is hash-like;
19
32
# each root is a key for a stored object.
20
- # - {Transactions}[rdoc-ref:PStore@Transactions]: each transaction is a ollection
33
+ # - {Transactions}[rdoc-ref:PStore@Transactions]: each transaction is a collection
21
34
# of prospective changes to the store;
22
35
# a transaction is defined in the block given with a call
23
36
# to PStore#transaction.
37
50
# end
38
51
#
39
52
# To avoid modifying the example store, some examples first execute
40
- # <tt>temp = store.dup</tt>, then apply changes to +temp+
53
+ # <tt>temp = store.dup</tt>, then apply changes to +temp+.
41
54
#
42
55
# == The Store
43
56
#
59
72
# Each root has a key and a value, just as in a hash:
60
73
#
61
74
# - Key: as in a hash, the key can be (almost) any object;
62
- # see {Hash}[https://docs.ruby-lang.org/en/master/Hash.html].
75
+ # see {Hash Keys }[https://docs.ruby-lang.org/en/master/Hash.html#class-Hash-label-Hash+Keys ].
63
76
# You may find it convenient to keep it simple by using only
64
77
# symbols or strings as keys.
65
- # - Value: the value truly may be any object, and in fact can be a collection
78
+ # - Value: the value may be any object that can be serialized by \Marshal
79
+ # (see {Marshal::dump}[https://docs.ruby-lang.org/en/master/Marshal.html#method-c-dump])
80
+ # and in fact may be a collection
66
81
# (e.g., an array, a hash, a set, a range, etc).
67
- # That collection may in turn contain nested collections, to any depth.
82
+ # That collection may in turn contain nested objects,
83
+ # including collections, to any depth;
84
+ # those objects must also be Marshal-serializable.
68
85
# See {Deep Root Values}[rdoc-ref:PStore@Deep+Root+Values].
69
86
#
70
87
# == Transactions
71
88
#
72
89
# A call to PStore#transaction must have a block.
73
90
#
74
- # A transaction consists of just those \PStore method calls in the block
75
- # that would modify the store; those methods are #[]= and #delete.
76
- # Note that the block may contain any code whatsoever
91
+ # The block may contain any code whatsoever
77
92
# except a nested call to #transaction.
93
+ # The transaction itself consists of the \PStore method calls in the block
94
+ # that would modify the store: calls to #[]= and #delete.
78
95
#
79
96
# An instance method in \PStore may be called only from within a transaction
80
97
# (with the exception the #path may be called from anywhere).
@@ -416,7 +433,7 @@ def []=(name, value)
416
433
# :call-seq:
417
434
# delete(key)
418
435
#
419
- # Removes and returns the root for +key+ if it exists:
436
+ # Removes and returns the value at +key+ if it exists:
420
437
#
421
438
# store = PStore.new('t.store')
422
439
# store.transaction do
@@ -467,8 +484,8 @@ def path
467
484
@filename
468
485
end
469
486
470
- # Exits the current transaction block after committing any changes
471
- # specified in that block.
487
+ # Exits the current transaction block, committing any changes
488
+ # specified in the transaction block.
472
489
# See {Committing or Aborting}[rdoc-ref:PStore@Committing+or+Aborting].
473
490
#
474
491
# Raises an exception if called outside a transaction block.
@@ -478,8 +495,8 @@ def commit
478
495
throw :pstore_abort_transaction
479
496
end
480
497
481
- # Exits the current transaction block, ignoring any changes
482
- # specified in that block.
498
+ # Exits the current transaction block, discarding any changes
499
+ # specified in the transaction block.
483
500
# See {Committing or Aborting}[rdoc-ref:PStore@Committing+or+Aborting].
484
501
#
485
502
# Raises an exception if called outside a transaction block.
@@ -489,11 +506,11 @@ def abort
489
506
throw :pstore_abort_transaction
490
507
end
491
508
492
- # Defines a transaction block for the store.
509
+ # Opens a transaction block for the store.
493
510
# See {Transactions}[rdoc-ref:PStore@Transactions].
494
511
#
495
- # With argument +read_only+ as +false+, the block may contain any Ruby code,
496
- # including calls to \PStore methods other #transaction .
512
+ # With argument +read_only+ as +false+, the block may both read from
513
+ # and write to the store .
497
514
#
498
515
# With argument +read_only+ as +true+, the block may not include calls
499
516
# to #transaction, #[]=, or #delete.
0 commit comments