delight 0.1.1 → 0.1.3
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 → enumerator}/collection_matcher.rb +1 -1
- data/lib/delight/enumerator/detect_bang.rb +14 -0
- data/lib/delight/enumerator/detect_by.rb +17 -0
- data/lib/delight/enumerator/detect_by_key.rb +17 -0
- data/lib/delight/enumerator/error/element_not_found.rb +8 -0
- data/lib/delight/enumerator/error/sole_item_expected.rb +8 -0
- data/lib/delight/enumerator/error.rb +6 -0
- data/lib/delight/enumerator/hash_collection_matcher.rb +11 -0
- data/lib/delight/{delight_enumerator/by_methods.rb → enumerator/select_by.rb} +2 -4
- data/lib/delight/enumerator/select_by_key.rb +11 -0
- data/lib/delight/enumerator/sole_by.rb +27 -0
- data/lib/delight/enumerator/sole_by_key.rb +9 -0
- data/lib/delight/enumerator/try_sole_by.rb +25 -0
- data/lib/delight/enumerator.rb +21 -0
- data/lib/delight/version.rb +1 -1
- metadata +17 -6
- data/lib/delight/delight_enumerator.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b71843e7ea31f5cd0ead34733ab94d3505e09db476072a9e40f189aa969df72
|
4
|
+
data.tar.gz: 259873047a56a06f96a907c394a1ce6280021ea886831e43283e3cc1886938fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3300b566241e7874db2f180fd6fb5f406cb0a48bbf81955b8fb2d561a5e83f56319144083d9e98cfd74642f4081b6586e92288ffc368a6840bef0e751b8b17e4
|
7
|
+
data.tar.gz: a44f89bbf3927b3bc4b79fe677912311d16f29d16a414eb5ab4132aa3c939864cc840aad2c820b8433d75f44b749b62b5b79962f25e6db0c14db8149a4d4a781
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Delight
|
2
|
+
module Enumerator
|
3
|
+
module DetectBang
|
4
|
+
# This method finds required element or raises error if the element is
|
5
|
+
# not found. This is similar to sole, but returns first element (so
|
6
|
+
# duplicates are possible) but doesn't need to traverse whole Array.
|
7
|
+
|
8
|
+
def detect!(error_message: nil, &)
|
9
|
+
default_error_message = "No element found"
|
10
|
+
detect(&) || raise(Error::ElementNotFound, error_message || default_error_message)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Delight
|
2
|
+
module Enumerator
|
3
|
+
module DetectBy
|
4
|
+
def detect_by(**)
|
5
|
+
detect do |element|
|
6
|
+
collection_matcher(element, **)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def detect_by!(**)
|
11
|
+
detect! do |element|
|
12
|
+
collection_matcher(element, **)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Delight
|
2
|
+
module Enumerator
|
3
|
+
module DetectByKey
|
4
|
+
def detect_by_key(**)
|
5
|
+
detect do |element|
|
6
|
+
hash_collection_matcher(element, **)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def detect_by_key!(**)
|
11
|
+
detect! do |element|
|
12
|
+
hash_collection_matcher(element, **)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Delight
|
2
|
+
module Enumerator
|
3
|
+
module SoleBy
|
4
|
+
def sole_by(**)
|
5
|
+
sole_by_implementation(:collection_matcher, **)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def sole_by_implementation(searcher, **)
|
11
|
+
results = lazy.select do |element|
|
12
|
+
public_send(searcher, element, **)
|
13
|
+
end
|
14
|
+
|
15
|
+
found, undesired = results.first(2)
|
16
|
+
|
17
|
+
if found.nil?
|
18
|
+
raise Error::ElementNotFound
|
19
|
+
elsif undesired
|
20
|
+
raise Error::SoleItemExpected, undesired
|
21
|
+
end
|
22
|
+
|
23
|
+
found
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Delight
|
2
|
+
module Enumerator
|
3
|
+
module TrySoleBy
|
4
|
+
def try_sole_by(**)
|
5
|
+
try_sole_by_implementation(:collection_matcher, **)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def try_sole_by_implementation(searcher, **)
|
11
|
+
results = lazy.select do |element|
|
12
|
+
public_send(searcher, element, **)
|
13
|
+
end
|
14
|
+
|
15
|
+
found, undesired = results.first(2)
|
16
|
+
|
17
|
+
if undesired
|
18
|
+
raise Error::SoleItemExpected, undesired
|
19
|
+
end
|
20
|
+
|
21
|
+
found
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Delight
|
2
|
+
module Enumerator
|
3
|
+
refine Enumerable do
|
4
|
+
import_methods CollectionMatcher
|
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
|
19
|
+
end
|
20
|
+
end
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mariusz Droździel
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: zeitwerk
|
@@ -34,9 +34,20 @@ files:
|
|
34
34
|
- README.md
|
35
35
|
- Rakefile
|
36
36
|
- lib/delight.rb
|
37
|
-
- lib/delight/
|
38
|
-
- lib/delight/
|
39
|
-
- lib/delight/
|
37
|
+
- lib/delight/enumerator.rb
|
38
|
+
- lib/delight/enumerator/collection_matcher.rb
|
39
|
+
- lib/delight/enumerator/detect_bang.rb
|
40
|
+
- lib/delight/enumerator/detect_by.rb
|
41
|
+
- lib/delight/enumerator/detect_by_key.rb
|
42
|
+
- lib/delight/enumerator/error.rb
|
43
|
+
- lib/delight/enumerator/error/element_not_found.rb
|
44
|
+
- lib/delight/enumerator/error/sole_item_expected.rb
|
45
|
+
- lib/delight/enumerator/hash_collection_matcher.rb
|
46
|
+
- lib/delight/enumerator/select_by.rb
|
47
|
+
- lib/delight/enumerator/select_by_key.rb
|
48
|
+
- lib/delight/enumerator/sole_by.rb
|
49
|
+
- lib/delight/enumerator/sole_by_key.rb
|
50
|
+
- lib/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
|
@@ -59,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
70
|
- !ruby/object:Gem::Version
|
60
71
|
version: '0'
|
61
72
|
requirements: []
|
62
|
-
rubygems_version: 3.6.
|
73
|
+
rubygems_version: 3.6.7
|
63
74
|
specification_version: 4
|
64
75
|
summary: Delightful extensions to Ruby's standard library.
|
65
76
|
test_files: []
|