delight 0.1.1 → 0.1.2
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/lib/delight/delight_enumerator/detect_bang.rb +16 -0
- data/lib/delight/delight_enumerator/detect_by.rb +19 -0
- data/lib/delight/delight_enumerator/detect_by_key.rb +19 -0
- data/lib/delight/delight_enumerator/error/element_not_found.rb +8 -0
- data/lib/delight/delight_enumerator/error/sole_item_expected.rb +8 -0
- data/lib/delight/delight_enumerator/error.rb +6 -0
- data/lib/delight/delight_enumerator/hash_collection_matcher.rb +11 -0
- data/lib/delight/delight_enumerator/{by_methods.rb → select_by.rb} +1 -1
- data/lib/delight/delight_enumerator/select_by_key.rb +13 -0
- data/lib/delight/delight_enumerator/sole_by.rb +29 -0
- data/lib/delight/delight_enumerator/sole_by_key.rb +12 -0
- data/lib/delight/delight_enumerator/try_sole_by.rb +27 -0
- data/lib/delight/delight_enumerator.rb +14 -1
- data/lib/delight/version.rb +1 -1
- metadata +14 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f8b296bea6885bf28e8099087e72cab52ef75a843e1c390a0d68b9b8e83798a
|
4
|
+
data.tar.gz: b1eac6efa6ba0c1aae4a279185de6fea118b9c796306326171cd0e7bea477c19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5ecfa1a5774c61d58e19bc0801be073a6899c916111bcdb754c53a5ba13d80dec48eba01bb6c68f0249215cc7d2e91a7e6a444e0546d121f8269e602b1d3dd1
|
7
|
+
data.tar.gz: 3a7c646c237a93f747bcd7beff8e457245619f118bd51452bb5771d5e3ab8b151f837ee905c56cfc1a1e876d58a485cd2d46557d3bb5c22460864c6d61637291
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Delight
|
2
|
+
module DelightEnumerator
|
3
|
+
module DetectBang
|
4
|
+
include CollectionMatcher
|
5
|
+
|
6
|
+
# This method finds required element or raises error if the element is
|
7
|
+
# not found. This is similar to sole, but returns first element (so
|
8
|
+
# duplicates are possible) but doesn't need to traverse whole Array.
|
9
|
+
|
10
|
+
def detect!(error_message: nil, &)
|
11
|
+
default_error_message = "No element found"
|
12
|
+
detect(&) || raise(Error::ElementNotFound, error_message || default_error_message)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Delight
|
2
|
+
module DelightEnumerator
|
3
|
+
module DetectBy
|
4
|
+
include CollectionMatcher
|
5
|
+
|
6
|
+
def detect_by(**)
|
7
|
+
detect do |element|
|
8
|
+
collection_matcher(element, **)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def detect_by!(**)
|
13
|
+
detect! do |element|
|
14
|
+
collection_matcher(element, **)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Delight
|
2
|
+
module DelightEnumerator
|
3
|
+
module DetectByKey
|
4
|
+
include HashCollectionMatcher
|
5
|
+
|
6
|
+
def detect_by_key(**)
|
7
|
+
detect do |element|
|
8
|
+
hash_collection_matcher(element, **)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def detect_by_key!(**)
|
13
|
+
detect! do |element|
|
14
|
+
hash_collection_matcher(element, **)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Delight
|
2
|
+
module DelightEnumerator
|
3
|
+
module SoleBy
|
4
|
+
include CollectionMatcher
|
5
|
+
|
6
|
+
def sole_by(**)
|
7
|
+
sole_by_implementation(:collection_matcher, **)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def sole_by_implementation(searcher, **)
|
13
|
+
results = lazy.select do |element|
|
14
|
+
public_send(searcher, element, **)
|
15
|
+
end
|
16
|
+
|
17
|
+
found, undesired = results.first(2)
|
18
|
+
|
19
|
+
if found.nil?
|
20
|
+
raise Error::ElementNotFound
|
21
|
+
elsif undesired
|
22
|
+
raise Error::SoleItemExpected, undesired
|
23
|
+
end
|
24
|
+
|
25
|
+
found
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Delight
|
2
|
+
module DelightEnumerator
|
3
|
+
module TrySoleBy
|
4
|
+
include CollectionMatcher
|
5
|
+
|
6
|
+
def try_sole_by(**)
|
7
|
+
try_sole_by_implementation(:collection_matcher, **)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def try_sole_by_implementation(searcher, **)
|
13
|
+
results = lazy.select do |element|
|
14
|
+
public_send(searcher, element, **)
|
15
|
+
end
|
16
|
+
|
17
|
+
found, undesired = results.first(2)
|
18
|
+
|
19
|
+
if undesired
|
20
|
+
raise Error::SoleItemExpected, undesired
|
21
|
+
end
|
22
|
+
|
23
|
+
found
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -2,7 +2,20 @@ module Delight
|
|
2
2
|
module DelightEnumerator
|
3
3
|
refine Enumerable do
|
4
4
|
import_methods CollectionMatcher
|
5
|
-
import_methods
|
5
|
+
import_methods HashCollectionMatcher
|
6
|
+
|
7
|
+
import_methods SelectBy
|
8
|
+
import_methods SelectByKey
|
9
|
+
|
10
|
+
import_methods DetectBang
|
11
|
+
import_methods DetectBy
|
12
|
+
import_methods DetectByKey
|
13
|
+
|
14
|
+
import_methods SoleBy
|
15
|
+
import_methods SoleByKey
|
16
|
+
|
17
|
+
import_methods TrySoleBy
|
18
|
+
# import_methods TrySoleByKey
|
6
19
|
end
|
7
20
|
end
|
8
21
|
end
|
data/lib/delight/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delight
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mariusz Droździel
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-05-13 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: zeitwerk
|
@@ -35,8 +35,19 @@ files:
|
|
35
35
|
- Rakefile
|
36
36
|
- lib/delight.rb
|
37
37
|
- lib/delight/delight_enumerator.rb
|
38
|
-
- lib/delight/delight_enumerator/by_methods.rb
|
39
38
|
- lib/delight/delight_enumerator/collection_matcher.rb
|
39
|
+
- lib/delight/delight_enumerator/detect_bang.rb
|
40
|
+
- lib/delight/delight_enumerator/detect_by.rb
|
41
|
+
- lib/delight/delight_enumerator/detect_by_key.rb
|
42
|
+
- lib/delight/delight_enumerator/error.rb
|
43
|
+
- lib/delight/delight_enumerator/error/element_not_found.rb
|
44
|
+
- lib/delight/delight_enumerator/error/sole_item_expected.rb
|
45
|
+
- lib/delight/delight_enumerator/hash_collection_matcher.rb
|
46
|
+
- lib/delight/delight_enumerator/select_by.rb
|
47
|
+
- lib/delight/delight_enumerator/select_by_key.rb
|
48
|
+
- lib/delight/delight_enumerator/sole_by.rb
|
49
|
+
- lib/delight/delight_enumerator/sole_by_key.rb
|
50
|
+
- lib/delight/delight_enumerator/try_sole_by.rb
|
40
51
|
- lib/delight/version.rb
|
41
52
|
- sig/delight.rbs
|
42
53
|
homepage: https://github.com/marzdrel/delight
|