delight 0.1.4 → 0.1.6
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/README.md +50 -21
- data/lib/delight/enumerator/collection_matcher.rb +1 -1
- data/lib/delight/enumerator/select_by.rb +3 -27
- data/lib/delight/enumerator/select_by_methods.rb +34 -0
- data/lib/delight/enumerator.rb +1 -1
- data/lib/delight/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb589267629b62a9906d523d22ecf83c034f1df0ce9bf301079891a537d9a688
|
4
|
+
data.tar.gz: 1af7bc10bb90205a4127f9aecb936b677e23d561b5a0d96e0785a181aa554ca8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc015857edf6623769c2af429ae7a4308b243b24bd4ad8f6516f7ed885007280615c56119fb12bf43d3323c3d3b56c801341e492d6fbc3c092f75ecba97fb25b
|
7
|
+
data.tar.gz: bceac43c952c822ba667faa8f16157108699487f50e006add390bf4e4e059869cf1807b70bad44900db64559cf9312d90069a2704adced962484b10196170351
|
data/README.md
CHANGED
@@ -1,43 +1,72 @@
|
|
1
1
|
# Delight
|
2
2
|
|
3
|
-
|
3
|
+
Delight is a Ruby gem offering small yet useful extensions to the core Ruby classes. Those extensions are implemented as Refinements, which means they are only available in the scope where they are explicitly activated. This allows you to use them without worrying about polluting the global namespace or causing conflicts with other gems.
|
4
4
|
|
5
|
-
|
5
|
+
## [Installation](Installation)
|
6
6
|
|
7
|
-
|
7
|
+
```bash
|
8
|
+
bundle add delight
|
9
|
+
```
|
8
10
|
|
9
|
-
|
11
|
+
## Usage
|
10
12
|
|
11
|
-
|
13
|
+
In order to import all available methods use the global `Delight::Enumerable` refinement.
|
12
14
|
|
13
|
-
```
|
14
|
-
|
15
|
+
```ruby
|
16
|
+
class MyClass
|
17
|
+
using Delight::Enumerable
|
18
|
+
|
19
|
+
def initialize(some_list)
|
20
|
+
@some_list = some_list
|
21
|
+
end
|
22
|
+
|
23
|
+
def my_method
|
24
|
+
some_list.select_by(country: "PL")
|
25
|
+
end
|
26
|
+
end
|
15
27
|
```
|
16
28
|
|
17
|
-
|
29
|
+
### `.select_by`
|
18
30
|
|
19
|
-
|
20
|
-
|
31
|
+
The `.select_by` method allows you to filter an array of objects based on a
|
32
|
+
value(s) of single or multiple methods. Object in the collection must respond to
|
33
|
+
the methods you are filtering by. Calling non-existing method will raise an
|
34
|
+
`NoMethodError`.
|
35
|
+
|
36
|
+
Following two examples are equivalent:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
addresses.select_by(country: "PL", city: "Warsaw")
|
40
|
+
|
41
|
+
addresses.select do |address|
|
42
|
+
address.country == "PL" && address.city == "Warsaw"
|
43
|
+
end
|
21
44
|
```
|
22
45
|
|
23
|
-
|
46
|
+
Object attributes are compared using `===` operator, so you can use any object
|
47
|
+
that implements it. For example, you can use a range:
|
24
48
|
|
25
|
-
|
49
|
+
```ruby
|
50
|
+
addresses.select_by(age: 18..)
|
26
51
|
|
27
|
-
|
52
|
+
addresses.select do |address|
|
53
|
+
address.age >= 18
|
54
|
+
end
|
55
|
+
```
|
28
56
|
|
29
|
-
|
57
|
+
Warning, be aware of of the `===` operator behavior. For example, if you would
|
58
|
+
like to filter out the object by class, you need to use the object itself as the
|
59
|
+
argument, thus pass the `itself` method:
|
30
60
|
|
31
|
-
|
61
|
+
```ruby
|
62
|
+
[18, 2.5, "foo"].select_by(itself: Numeric)
|
63
|
+
# => [18, 2.5]
|
64
|
+
```
|
32
65
|
|
33
|
-
##
|
66
|
+
## Development
|
34
67
|
|
35
|
-
|
68
|
+
[...]
|
36
69
|
|
37
70
|
## License
|
38
71
|
|
39
72
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
-
|
41
|
-
## Code of Conduct
|
42
|
-
|
43
|
-
Everyone interacting in the Delight project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/delight/blob/main/CODE_OF_CONDUCT.md).
|
@@ -1,33 +1,9 @@
|
|
1
1
|
module Delight
|
2
2
|
module Enumerator
|
3
3
|
module SelectBy
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
# @return [Array] containingg elements that match the passed arguments.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# ["String", 1, 3, :symbol, 'string'].select_by(class: String)
|
11
|
-
# # => ["String", 'string']
|
12
|
-
#
|
13
|
-
# Person = Struct.new(:name, :age)
|
14
|
-
#
|
15
|
-
# people = [
|
16
|
-
# Person.new("John", 30),
|
17
|
-
# Person.new("Jane", 25),
|
18
|
-
# Person.new("Alice", 30),
|
19
|
-
# ]
|
20
|
-
#
|
21
|
-
# people.select_by(age: 30)
|
22
|
-
# # => [
|
23
|
-
# # Person.new("John", 30),
|
24
|
-
# # Person.new("Alice", 30),
|
25
|
-
# # ]
|
26
|
-
#
|
27
|
-
def select_by(**)
|
28
|
-
select do |element|
|
29
|
-
collection_matcher(element, **)
|
30
|
-
end
|
4
|
+
refine Enumerable do
|
5
|
+
import_methods CollectionMatcher
|
6
|
+
import_methods SelectByMethods
|
31
7
|
end
|
32
8
|
end
|
33
9
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Delight
|
2
|
+
module Enumerator
|
3
|
+
module SelectByMethods
|
4
|
+
# Filter collection elements using method calls and values passed as
|
5
|
+
# keyword arguments.
|
6
|
+
#
|
7
|
+
# @return [Array] containingg elements that match the passed arguments.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# ["String", 1, 3, :symbol, 'string'].select_by(class: String)
|
11
|
+
# # => ["String", 'string']
|
12
|
+
#
|
13
|
+
# Person = Struct.new(:name, :age)
|
14
|
+
#
|
15
|
+
# people = [
|
16
|
+
# Person.new("John", 30),
|
17
|
+
# Person.new("Jane", 25),
|
18
|
+
# Person.new("Alice", 30),
|
19
|
+
# ]
|
20
|
+
#
|
21
|
+
# people.select_by(age: 30)
|
22
|
+
# # => [
|
23
|
+
# # Person.new("John", 30),
|
24
|
+
# # Person.new("Alice", 30),
|
25
|
+
# # ]
|
26
|
+
#
|
27
|
+
def select_by(**)
|
28
|
+
select do |element|
|
29
|
+
collection_matcher(element, **)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/delight/enumerator.rb
CHANGED
data/lib/delight/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mariusz Droździel
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- lib/delight/enumerator/hash_collection_matcher.rb
|
46
46
|
- lib/delight/enumerator/select_by.rb
|
47
47
|
- lib/delight/enumerator/select_by_key.rb
|
48
|
+
- lib/delight/enumerator/select_by_methods.rb
|
48
49
|
- lib/delight/enumerator/sole_by.rb
|
49
50
|
- lib/delight/enumerator/sole_by_key.rb
|
50
51
|
- lib/delight/enumerator/try_sole_by.rb
|