weak 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +12 -0
- data/CHANGELOG.md +11 -0
- data/lib/weak/map.rb +163 -17
- data/lib/weak/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86165785c0d1874c5c54d7a72c3b703f5a21832cf0df8ffa917b4793dc40c502
|
4
|
+
data.tar.gz: a02c81a859c4f887181a2e33a124d6584a987d40cde92770fd1d6953a836f10d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60d9a3259f88cd37a001b60fdf5247a856d1c637a54d0085712b347ab9b4c0dda330e8cabe69ead1bdf8193fdb70d8a0f7b419ea407afdcbea0b8e659331473c
|
7
|
+
data.tar.gz: b2dbc7c76ee9fd24d1a06df292e4683f5f5eb1a1b7a2d9db6ab653e691e818d3d6b3975589e020c47daede1b50ab48a4c417830af1c7b4cac12a9e2b61dc47f1
|
data/.yardopts
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [Unreleased]
|
4
|
+
|
5
|
+
## [0.2.0] - 2025-04-09
|
6
|
+
|
7
|
+
- Add `Weak::Map#delete_if`
|
8
|
+
- Add `Weak::Map#keep_if`
|
9
|
+
- Add `Weak::Map#reject!`
|
10
|
+
- Add `Weak::Map#replace`
|
11
|
+
- Add `Weak::Map#select!`
|
12
|
+
- Add `Weak::Map#values_at`
|
13
|
+
|
3
14
|
## [0.1.0] - 2025-03-14
|
4
15
|
|
5
16
|
- Initial version of `Weak::Set` to store an unordered collection of objects.
|
data/lib/weak/map.rb
CHANGED
@@ -270,15 +270,14 @@ module Weak
|
|
270
270
|
INSPECT_KEY = :__inspect_key__
|
271
271
|
private_constant :INSPECT_KEY
|
272
272
|
|
273
|
-
# @param maps [Array
|
274
|
-
# merged into the new {Weak::Map}
|
273
|
+
# @param maps [Array<Weak::Map, Hash, #to_hash>] a list of maps which should
|
274
|
+
# be merged into the new {Weak::Map}
|
275
275
|
# @return [Weak::Map] a new {Weak::Map} object populated with the given
|
276
276
|
# objects, if any. With no argument, returns a new empty {Weak::Map}.
|
277
277
|
# @example
|
278
278
|
# hash = {foo: 0, bar: 1, baz: 2}
|
279
279
|
# Weak::Map[hash]
|
280
280
|
# # => #<Weak::Map {:foo=>0, :bar=>1, :baz=>2}>
|
281
|
-
|
282
281
|
def self.[](*maps)
|
283
282
|
Weak::Map.new.merge!(*maps)
|
284
283
|
end
|
@@ -432,6 +431,26 @@ module Weak
|
|
432
431
|
proc
|
433
432
|
end
|
434
433
|
|
434
|
+
# Deletes every key-value pair from `self` for which the given block
|
435
|
+
# evaluates to a truthy value.
|
436
|
+
#
|
437
|
+
# If no block is given, an `Enumerator` is returned instead.
|
438
|
+
#
|
439
|
+
# @yield [key, value] calls the given block once for each key in the map
|
440
|
+
# @yieldparam key [Object] a key
|
441
|
+
# @yieldparam value [Object] the corresponding value
|
442
|
+
# @return [Enumerator, self] `self` if a block was given or an `Enumerator`
|
443
|
+
# if no block was given.
|
444
|
+
# @see reject!
|
445
|
+
def delete_if(&block)
|
446
|
+
return enum_for(__method__) { size } unless block_given?
|
447
|
+
|
448
|
+
each do |key, value|
|
449
|
+
delete(key) if yield(key, value)
|
450
|
+
end
|
451
|
+
self
|
452
|
+
end
|
453
|
+
|
435
454
|
# @return [Boolean] `true` if `self` contains no elements
|
436
455
|
def empty?
|
437
456
|
size == 0
|
@@ -475,17 +494,37 @@ module Weak
|
|
475
494
|
end
|
476
495
|
alias_method :to_s, :inspect
|
477
496
|
|
497
|
+
# Deletes every key-value pair from `self` for which the given block
|
498
|
+
# evaluates to a falsey value.
|
499
|
+
#
|
500
|
+
# If no block is given, an `Enumerator` is returned instead.
|
501
|
+
#
|
502
|
+
# @yield [key, value] calls the given block once for each key in the map
|
503
|
+
# @yieldparam key [String] a hash key
|
504
|
+
# @yieldparam value [Object] the corresponding hash value
|
505
|
+
# @return [Enumerator, self] `self` if a block was given, an `Enumerator`
|
506
|
+
# if no block was given.
|
507
|
+
# @see select!
|
508
|
+
def keep_if(&block)
|
509
|
+
return enum_for(__method__) { size } unless block_given?
|
510
|
+
|
511
|
+
each do |key, value|
|
512
|
+
delete(key) unless yield(key, value)
|
513
|
+
end
|
514
|
+
self
|
515
|
+
end
|
516
|
+
|
478
517
|
# Returns the new {Weak::Map} formed by merging each of `other_maps` into a
|
479
518
|
# copy of `self`.
|
480
519
|
#
|
481
|
-
# Each argument in
|
482
|
-
#
|
520
|
+
# Each argument in `other_maps` must be either a {Weak::Map}, a Hash object
|
521
|
+
# or must be transformable to a Hash by calling `each_hash` on it.
|
483
522
|
#
|
484
523
|
# With arguments and no block:
|
485
524
|
#
|
486
525
|
# - Returns a new {Weak::Map}, after the given maps are merged into a copy
|
487
526
|
# of `self`.
|
488
|
-
# - The given
|
527
|
+
# - The given maps are merged left to right.
|
489
528
|
# - Each duplicate-key entry’s value overwrites the previous value.
|
490
529
|
#
|
491
530
|
# Example:
|
@@ -502,7 +541,7 @@ module Weak
|
|
502
541
|
# With arguments and a block:
|
503
542
|
#
|
504
543
|
# - Returns `self`, after the given maps are merged.
|
505
|
-
# - The given
|
544
|
+
# - The given maps are merged left to right.
|
506
545
|
# - For each duplicate key:
|
507
546
|
# - Calls the block with the key and the old and new values.
|
508
547
|
# - The block’s return value becomes the new value for the entry.
|
@@ -526,8 +565,8 @@ module Weak
|
|
526
565
|
# - Returns a copy of `self`.
|
527
566
|
# - The block, if given, is ignored.
|
528
567
|
#
|
529
|
-
# @param other_maps [Array
|
530
|
-
# merged into a copy of `self`
|
568
|
+
# @param other_maps [Array<Weak::Map, Hash, #to_hash>] a list of maps which
|
569
|
+
# should be merged into a copy of `self`
|
531
570
|
# @yield [key, old_value, new_value] If `self` already contains a value for
|
532
571
|
# a key, we yield the key, the old value from `self` and the new value
|
533
572
|
# from the given map and use the value returned from the block as the new
|
@@ -551,15 +590,87 @@ module Weak
|
|
551
590
|
end
|
552
591
|
end
|
553
592
|
|
593
|
+
# Deletes every key-value pair from `self` for which the given block
|
594
|
+
# evaluates to a truethy value.
|
595
|
+
#
|
596
|
+
# Equivalent to {#delete_if}, but returns `nil` if no changes were made.
|
597
|
+
#
|
598
|
+
# If no block is given, an `Enumerator` is returned instead.
|
599
|
+
#
|
600
|
+
# @yield [key, value] calls the given block once for each key in the map
|
601
|
+
# @yieldparam key [Object] a key
|
602
|
+
# @return [Enumerator, self, nil] `self` if a block was given and some
|
603
|
+
# element(s) were deleted, `nil` if a block was given but no keys were
|
604
|
+
# deleted, or an `Enumerator` if no block was given.
|
605
|
+
# #see #delete_if
|
606
|
+
def reject!(&block)
|
607
|
+
return enum_for(__method__) { size } unless block_given?
|
608
|
+
|
609
|
+
deleted_anything = false
|
610
|
+
each do |key, value|
|
611
|
+
next unless yield(key, value)
|
612
|
+
|
613
|
+
delete(key)
|
614
|
+
deleted_anything = true
|
615
|
+
end
|
616
|
+
|
617
|
+
self if deleted_anything
|
618
|
+
end
|
619
|
+
|
620
|
+
# Replaces the contents of `self` with the contents of the given Hash-like
|
621
|
+
# object and returns `self`.
|
622
|
+
#
|
623
|
+
# If the given `map` defines a {#default} value or {#default_proc}, this
|
624
|
+
# will also replace the respective seting in `self`.
|
625
|
+
#
|
626
|
+
# @param map (see #_implicit)
|
627
|
+
# @return [self]
|
628
|
+
# @example
|
629
|
+
# map = Weak::Map[a: 1, b: 3, c: 1] #=> #<Weak::Map {a: 1, b: 3, c: 1}>
|
630
|
+
# map.replace({x: :y}) #=> #<Weak::Map {x: :y}>
|
631
|
+
# map #=> #<Weak::Map {x: :y}>
|
632
|
+
def replace(map)
|
633
|
+
initialize_copy(_implicit(map))
|
634
|
+
end
|
635
|
+
|
636
|
+
# Deletes every key-value pair from `self` for which the given block
|
637
|
+
# evaluates to a falsey value.
|
638
|
+
#
|
639
|
+
# Equivalent to {#keep_if}, but returns `nil` if no changes were made.
|
640
|
+
#
|
641
|
+
# If no block is given, an `Enumerator` is returned instead.
|
642
|
+
#
|
643
|
+
# @yield [key, value] calls the given block once for each key in the map
|
644
|
+
# @yieldparam key [Object] a key
|
645
|
+
# @yieldparam value [Object] the corresponding value
|
646
|
+
# @return [Enumerator, self, nil] `self` if a block was given and some
|
647
|
+
# element(s) were deleted, `nil` if a block was given but nothing was
|
648
|
+
# deleted, or an `Enumerator` if no block was given.
|
649
|
+
# @see keep_if
|
650
|
+
def select!(&block)
|
651
|
+
return enum_for(__method__) { size } unless block_given?
|
652
|
+
|
653
|
+
deleted_anything = false
|
654
|
+
each do |key, value|
|
655
|
+
next if yield(key, value)
|
656
|
+
|
657
|
+
delete(key)
|
658
|
+
deleted_anything = true
|
659
|
+
end
|
660
|
+
|
661
|
+
self if deleted_anything
|
662
|
+
end
|
663
|
+
alias_method :filter!, :select!
|
664
|
+
|
554
665
|
# Merges each of `other_maps` into `self`; returns `self`.
|
555
666
|
#
|
556
|
-
# Each argument in
|
557
|
-
#
|
667
|
+
# Each argument in `other_maps` must be either a {Weak::Map}, a Hash object
|
668
|
+
# or must be transformable to a Hash by calling `each_hash` on it.
|
558
669
|
#
|
559
670
|
# With arguments and no block:
|
560
671
|
#
|
561
672
|
# - Returns self, after the given maps are merged into it.
|
562
|
-
# - The given
|
673
|
+
# - The given maps are merged left to right.
|
563
674
|
# - Each duplicate-key entry’s value overwrites the previous value.
|
564
675
|
#
|
565
676
|
# Example:
|
@@ -576,7 +687,7 @@ module Weak
|
|
576
687
|
# With arguments and a block:
|
577
688
|
#
|
578
689
|
# - Returns `self`, after the given maps are merged.
|
579
|
-
# - The given
|
690
|
+
# - The given maps are merged left to right.
|
580
691
|
# - For each duplicate key:
|
581
692
|
# - Calls the block with the key and the old and new values.
|
582
693
|
# - The block’s return value becomes the new value for the entry.
|
@@ -600,8 +711,8 @@ module Weak
|
|
600
711
|
# - Returns `self`.
|
601
712
|
# - The block, if given, is ignored.
|
602
713
|
#
|
603
|
-
# @param other_maps [Array
|
604
|
-
#
|
714
|
+
# @param other_maps [Array<Weak::Map, Hash, #to_hash>] a list of maps which
|
715
|
+
# should be merged into `self`
|
605
716
|
# @yield [key, old_value, new_value] If `self` already contains a value for
|
606
717
|
# a key, we yield the key, the old value from `self` and the new value
|
607
718
|
# from the given map and use the value returned from the block as the new
|
@@ -618,7 +729,7 @@ module Weak
|
|
618
729
|
missing = Object.new
|
619
730
|
|
620
731
|
other_maps.each do |map|
|
621
|
-
map.each_pair do |key, value|
|
732
|
+
_implicit(map).each_pair do |key, value|
|
622
733
|
old_value = fetch(key, missing)
|
623
734
|
value = yield(key, old_value, value) unless missing == old_value
|
624
735
|
self[key] = value
|
@@ -626,7 +737,7 @@ module Weak
|
|
626
737
|
end
|
627
738
|
else
|
628
739
|
other_maps.each do |map|
|
629
|
-
map.each_pair do |key, value|
|
740
|
+
_implicit(map).each_pair do |key, value|
|
630
741
|
self[key] = value
|
631
742
|
end
|
632
743
|
end
|
@@ -675,6 +786,25 @@ module Weak
|
|
675
786
|
hash
|
676
787
|
end
|
677
788
|
|
789
|
+
# Returns a new `Array` containing values for the given keys:
|
790
|
+
#
|
791
|
+
# map = Weak::Map[foo: 0, bar: 1, baz: 2]
|
792
|
+
# map.values_at(:baz, :foo)
|
793
|
+
# # => [2, 0]
|
794
|
+
#
|
795
|
+
# The default values are returned for any keys that are not found:
|
796
|
+
#
|
797
|
+
# map.values_at(:hello, :foo)
|
798
|
+
# # => [nil, 0]
|
799
|
+
#
|
800
|
+
# @param keys [Array<Object>] a list of keys
|
801
|
+
# @return [Array] an `Array` containing the values for the given keys if
|
802
|
+
# present or the default value if not. The order of the given `keys` is
|
803
|
+
# preserved.
|
804
|
+
def values_at(*keys)
|
805
|
+
keys.map { |key| self[key] }
|
806
|
+
end
|
807
|
+
|
678
808
|
private
|
679
809
|
|
680
810
|
# Callback method which is called on the new object during `dup` or `clone`
|
@@ -710,5 +840,21 @@ module Weak
|
|
710
840
|
)
|
711
841
|
end
|
712
842
|
end
|
843
|
+
|
844
|
+
# @param map [Weak::Map, Hash, #to_hash] a {Weak::Map} or a Hash object or
|
845
|
+
# an object which can be converted to a `Hash` by calling `to_hash` on it
|
846
|
+
# @return [Weak::Map, Hash] either a `Weak::Map` or a `Hash` object
|
847
|
+
# converted from the given `map`
|
848
|
+
# @raise [TypeError] if the given `map` is not a {Weak::Map} and not a
|
849
|
+
# `Hash`, and could not ge converted to a Hash
|
850
|
+
def _implicit(map)
|
851
|
+
if Weak::Map === map || ::Hash === map
|
852
|
+
map
|
853
|
+
elsif (hash = ::Hash.try_convert(map))
|
854
|
+
hash
|
855
|
+
else
|
856
|
+
raise TypeError, "no implicit conversion of #{map.class} into #{self.class}"
|
857
|
+
end
|
858
|
+
end
|
713
859
|
end
|
714
860
|
end
|
data/lib/weak/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weak
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Holger Just
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-04-09 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
description: |
|
13
13
|
The Weak library provides a Weak::Set class to store an unordered list of
|
@@ -18,6 +18,7 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- ".yardopts"
|
21
22
|
- CHANGELOG.md
|
22
23
|
- CODE_OF_CONDUCT.md
|
23
24
|
- LICENSE.txt
|
@@ -44,7 +45,7 @@ metadata:
|
|
44
45
|
homepage_uri: https://github.com/meineerde/weak
|
45
46
|
source_code_uri: https://github.com/meineerde/weak
|
46
47
|
changelog_uri: https://github.com/meineerde/weak/blob/main/CHANGELOG.md
|
47
|
-
documentation_uri: https://www.rubydoc.info/gems/weak/0.
|
48
|
+
documentation_uri: https://www.rubydoc.info/gems/weak/0.2.0
|
48
49
|
rdoc_options: []
|
49
50
|
require_paths:
|
50
51
|
- lib
|
@@ -61,5 +62,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
62
|
requirements: []
|
62
63
|
rubygems_version: 3.6.3
|
63
64
|
specification_version: 4
|
64
|
-
summary: Tools to
|
65
|
+
summary: Tools to handle collections of weakly-referenced values
|
65
66
|
test_files: []
|