delight 0.1.5 → 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 +52 -1
- data/lib/delight/enumerator/collection_matcher.rb +1 -1
- data/lib/delight/version.rb +1 -1
- metadata +1 -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
@@ -10,7 +10,58 @@ bundle add delight
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
|
13
|
+
In order to import all available methods use the global `Delight::Enumerable` refinement.
|
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
|
27
|
+
```
|
28
|
+
|
29
|
+
### `.select_by`
|
30
|
+
|
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
|
44
|
+
```
|
45
|
+
|
46
|
+
Object attributes are compared using `===` operator, so you can use any object
|
47
|
+
that implements it. For example, you can use a range:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
addresses.select_by(age: 18..)
|
51
|
+
|
52
|
+
addresses.select do |address|
|
53
|
+
address.age >= 18
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
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:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
[18, 2.5, "foo"].select_by(itself: Numeric)
|
63
|
+
# => [18, 2.5]
|
64
|
+
```
|
14
65
|
|
15
66
|
## Development
|
16
67
|
|
data/lib/delight/version.rb
CHANGED