Skip to content

Commit 9d8221a

Browse files
pstore: fixes types, treat it as a collection
this adds hash-like typing to signal its key-value store semantics. it seems like a breaking change, but in practice this has been broken for a while. Other wrong sigs were fixed, such as transaction, which requires a block.
1 parent 17f5871 commit 9d8221a

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

stdlib/pstore/0/pstore.rbs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@
299299
# end
300300
# end
301301
#
302-
class PStore
302+
class PStore[unchecked out K, unchecked out V]
303303
# <!--
304304
# rdoc-file=lib/pstore.rb
305305
# - [](key)
@@ -320,7 +320,7 @@ class PStore
320320
#
321321
# Raises an exception if called outside a transaction block.
322322
#
323-
def []: (untyped name) -> untyped
323+
def []: (K name) -> V?
324324

325325
# <!--
326326
# rdoc-file=lib/pstore.rb
@@ -338,7 +338,7 @@ class PStore
338338
#
339339
# Raises an exception if called outside a transaction block.
340340
#
341-
def []=: (untyped name, untyped value) -> untyped
341+
def []=: (K name, V value) -> V
342342

343343
# <!--
344344
# rdoc-file=lib/pstore.rb
@@ -349,7 +349,7 @@ class PStore
349349
#
350350
# Raises an exception if called outside a transaction block.
351351
#
352-
def abort: () -> untyped
352+
def abort: () -> void
353353

354354
# <!--
355355
# rdoc-file=lib/pstore.rb
@@ -360,7 +360,7 @@ class PStore
360360
#
361361
# Raises an exception if called outside a transaction block.
362362
#
363-
def commit: () -> nil
363+
def commit: () -> void
364364

365365
# <!--
366366
# rdoc-file=lib/pstore.rb
@@ -379,7 +379,7 @@ class PStore
379379
#
380380
# Raises an exception if called outside a transaction block.
381381
#
382-
def delete: (untyped name) -> untyped
382+
def delete: (K name) -> V?
383383

384384
# <!--
385385
# rdoc-file=lib/pstore.rb
@@ -400,7 +400,7 @@ class PStore
400400
#
401401
# Raises an exception if called outside a transaction block.
402402
#
403-
def fetch: (untyped name, ?untyped default) -> untyped
403+
def fetch: (K name, ?V default) -> V
404404

405405
# <!--
406406
# rdoc-file=lib/pstore.rb
@@ -410,21 +410,25 @@ class PStore
410410
#
411411
# store.path # => "flat.store"
412412
#
413-
def path: () -> untyped
413+
def path: () -> String
414414

415415
# <!--
416416
# rdoc-file=lib/pstore.rb
417417
# - root?(key)
418418
# -->
419419
#
420-
def root?: (untyped name) -> bool
420+
def key?: (K name) -> bool
421+
422+
alias root? key?
421423

422424
# <!--
423425
# rdoc-file=lib/pstore.rb
424426
# - roots()
425427
# -->
426428
#
427-
def roots: () -> Array[untyped]
429+
def keys: () -> Array[K]
430+
431+
alias roots keys
428432

429433
# <!--
430434
# rdoc-file=lib/pstore.rb
@@ -441,7 +445,7 @@ class PStore
441445
#
442446
# Raises an exception if called within a transaction block.
443447
#
444-
def transaction: (?untyped read_only) -> untyped
448+
def transaction: [U] (?bool read_only) { (self) -> U } -> U
445449

446450
# <!-- rdoc-file=lib/pstore.rb -->
447451
# Whether PStore should do its best to prevent file corruptions, even when an
@@ -458,7 +462,7 @@ class PStore
458462
# raises no unexpected I/O error; if such an error occurs during a write to
459463
# the store, the file may become corrupted.
460464
#
461-
def ultra_safe: () -> untyped
465+
def ultra_safe: () -> bool
462466

463467
# <!-- rdoc-file=lib/pstore.rb -->
464468
# Whether PStore should do its best to prevent file corruptions, even when an
@@ -475,7 +479,7 @@ class PStore
475479
# raises no unexpected I/O error; if such an error occurs during a write to
476480
# the store, the file may become corrupted.
477481
#
478-
def ultra_safe=: (untyped) -> untyped
482+
def ultra_safe=: (bool) -> bool
479483

480484
private
481485

@@ -486,22 +490,22 @@ class PStore
486490
# - empty_marshal_checksum()
487491
# -->
488492
#
489-
def empty_marshal_checksum: () -> untyped
493+
def empty_marshal_checksum: () -> String
490494

491495
# <!--
492496
# rdoc-file=lib/pstore.rb
493497
# - empty_marshal_data()
494498
# -->
495499
#
496-
def empty_marshal_data: () -> untyped
500+
def empty_marshal_data: () -> String
497501

498502
# <!--
499503
# rdoc-file=lib/pstore.rb
500504
# - in_transaction()
501505
# -->
502506
# Raises PStore::Error if the calling code is not in a PStore#transaction.
503507
#
504-
def in_transaction: () -> untyped
508+
def in_transaction: () -> void
505509

506510
# <!--
507511
# rdoc-file=lib/pstore.rb
@@ -510,7 +514,7 @@ class PStore
510514
# Raises PStore::Error if the calling code is not in a PStore#transaction or if
511515
# the code is in a read-only PStore#transaction.
512516
#
513-
def in_transaction_wr: () -> untyped
517+
def in_transaction_wr: () -> void
514518

515519
# <!--
516520
# rdoc-file=lib/pstore.rb
@@ -531,7 +535,7 @@ class PStore
531535
#
532536
# store = PStore.new(path, true)
533537
#
534-
def initialize: (untyped file, ?boolish thread_safe) -> untyped
538+
def initialize: (path file, ?boolish thread_safe) -> void
535539

536540
def load: (untyped content) -> untyped
537541

@@ -543,7 +547,7 @@ class PStore
543547
# be returned. If `read_only` is false, a 3-tuple will be returned: the
544548
# unmarshalled Hash, a checksum of the data, and the size of the data.
545549
#
546-
def load_data: (untyped file, untyped read_only) -> untyped
550+
def load_data: (path file, true read_only) -> Hash[untyped, untyped]
547551

548552
# <!--
549553
# rdoc-file=lib/pstore.rb
@@ -564,7 +568,7 @@ class PStore
564568
#
565569
# All exceptions are propagated.
566570
#
567-
def open_and_lock_file: (untyped filename, untyped read_only) -> untyped
571+
def open_and_lock_file: (string filename, bool read_only) -> File?
568572

569573
# <!--
570574
# rdoc-file=lib/pstore.rb
@@ -578,26 +582,26 @@ class PStore
578582
# - save_data_with_atomic_file_rename_strategy(data, file)
579583
# -->
580584
#
581-
def save_data_with_atomic_file_rename_strategy: (untyped data, untyped file) -> untyped
585+
def save_data_with_atomic_file_rename_strategy: (string data, File file) -> void
582586

583587
# <!--
584588
# rdoc-file=lib/pstore.rb
585589
# - save_data_with_fast_strategy(data, file)
586590
# -->
587591
#
588-
def save_data_with_fast_strategy: (untyped data, untyped file) -> untyped
589-
end
592+
def save_data_with_fast_strategy: (string data, File file) -> void
590593

591-
PStore::EMPTY_MARSHAL_CHECKSUM: String
594+
EMPTY_MARSHAL_CHECKSUM: String
592595

593-
PStore::EMPTY_MARSHAL_DATA: String
596+
EMPTY_MARSHAL_DATA: String
594597

595-
PStore::EMPTY_STRING: String
598+
EMPTY_STRING: String
596599

597-
PStore::RDWR_ACCESS: Hash[untyped, untyped]
600+
RDWR_ACCESS: { mode: Integer, encoding: Encoding }
598601

599-
PStore::RD_ACCESS: Hash[untyped, untyped]
602+
RD_ACCESS: { mode: Integer, encoding: Encoding }
600603

601-
PStore::VERSION: String
604+
VERSION: String
602605

603-
PStore::WR_ACCESS: Hash[untyped, untyped]
606+
WR_ACCESS: { mode: Integer, encoding: Encoding }
607+
end

test/stdlib/PStore_test.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,32 @@ class PStoreSingletonTest < Test::Unit::TestCase
77
testing "singleton(::PStore)"
88

99
def test_initialize
10-
assert_send_type "(untyped file, ?bool thread_safe) -> PStore",
10+
assert_send_type "(path file, ?bool thread_safe) -> PStore",
1111
PStore, :new, "file_name", false
12-
assert_send_type "(untyped file, ?Symbol) -> PStore",
12+
assert_send_type "(path file, ?Symbol) -> PStore",
1313
PStore, :new, "file_name", :false
14+
assert_send_type "(path file, ?bool thread_safe) -> PStore[Integer]",
15+
PStore, :new, "file_name", false
16+
assert_send_type "(path file, ?Symbol) -> PStore[Integer, Integer]",
17+
PStore, :new, "file_name", :false
18+
end
19+
20+
end
21+
22+
class PStoreInstanceTest < Test::Unit::TestCase
23+
include TestHelper
24+
library "pstore"
25+
testing "::PStore[Symbol, Integer]"
26+
27+
def test_accessors
28+
store = PStore.new("file_name", false)
29+
store.transaction do |st|
30+
st[:foo] = 1
31+
32+
assert_send_type "(Symbol) -> Integer",
33+
st, :[], :foo
34+
assert_send_type "(Symbol) -> nil",
35+
st, :[], :bar
36+
end
1437
end
1538
end

0 commit comments

Comments
 (0)