associate_rb 0.0.1 → 0.0.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/extensions/array_extension.rb +47 -14
- 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: f6ddfeb80c1669afeec3e1d586af97bb94cad6f407c99b906fd971289c1adf49
|
4
|
+
data.tar.gz: 74906eddf3068f8084e988f3f8cf2fb6cac036e96c6a0bb2824386fff1310639
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3456689d6ac803d68069c8326083c9911ce80915977acf418cfefa3bc5a5ba236f8f8a0d0b0755feff4845706548a143be2efade4bb0a142e006778ef45332ee
|
7
|
+
data.tar.gz: f1e153ce3e4cd5ba3fbf969ba36be3f66703f55c77e02c3ae376073503f71b36609fd5f2da6439c6fd928cede72e61c73e7d40fe13db8d869f6f4f0862a5a079
|
@@ -1,14 +1,47 @@
|
|
1
1
|
module AssociateRB
|
2
2
|
module ArrayExtension
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# Returns a [Hash] containing key-value pairs provided by the block
|
4
|
+
# applied to elements of the given [Array].
|
5
|
+
#
|
5
6
|
# @return [Hash] the association
|
6
7
|
#
|
7
8
|
# @yield [it] Executes the association block
|
8
9
|
def associate
|
10
|
+
associate_to({}) { |it| yield(it) }
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns a [Hash] containing the elements from the given [Array] indexed
|
14
|
+
# by the key returned from the block applied to each element.
|
15
|
+
#
|
16
|
+
# @return [Hash] the association
|
17
|
+
#
|
18
|
+
# @yield [it] the value to be associated (becomes the key)
|
19
|
+
def associate_by
|
20
|
+
associate { |it| yield(it).to it }
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns a [Hash] where keys are elements from the given [Array] and values
|
24
|
+
# are produced by the block applied to each element.
|
25
|
+
#
|
26
|
+
# @return [Hash] the association
|
27
|
+
#
|
28
|
+
# @yield [it] the value to be associated (becomes the value)
|
29
|
+
def associate_with
|
30
|
+
associate { |it| it.to yield(it) }
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns a [Hash] containing key-value pairs provided by the block
|
34
|
+
# applied to elements of the given [Array], and the elements of the
|
35
|
+
# [Hash] sent to the method.
|
36
|
+
#
|
37
|
+
# @param association [Hash] the Hash to merge with the new values
|
38
|
+
# @return [Hash] the association
|
39
|
+
#
|
40
|
+
# @yield [it] the value to be associated
|
41
|
+
def associate_to(association)
|
9
42
|
raise ArgumentError, 'No block provided' unless block_given?
|
43
|
+
raise ArgumentError, 'Given association is not a Hash' unless association.is_a?(Hash)
|
10
44
|
|
11
|
-
association = {}
|
12
45
|
self.each do |it|
|
13
46
|
associated = yield(it)
|
14
47
|
raise ArgumentError, 'Block does not return Hash' unless associated.is_a?(Hash)
|
@@ -18,26 +51,26 @@ module AssociateRB
|
|
18
51
|
association
|
19
52
|
end
|
20
53
|
|
21
|
-
#
|
22
|
-
#
|
54
|
+
# Returns a [Hash] containing the elements from the given [Array] indexed
|
55
|
+
# by the key returned from the block applied to each element, and the
|
56
|
+
# elements of the [Hash] sent to the method.
|
57
|
+
#
|
23
58
|
# @return [Hash] the association
|
24
59
|
#
|
25
60
|
# @yield [it] the value to be associated (becomes the key)
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
self.associate { |it| yield(it).to it }
|
61
|
+
def associate_by_to(association)
|
62
|
+
associate_to(association) { |it| yield(it).to it }
|
30
63
|
end
|
31
64
|
|
32
|
-
#
|
65
|
+
# Returns a [Hash] where keys are elements from the given [Array] and values
|
66
|
+
# are produced by the block applied to each element, and the elements of
|
67
|
+
# the [Hash] sent to the method.
|
33
68
|
#
|
34
69
|
# @return [Hash] the association
|
35
70
|
#
|
36
71
|
# @yield [it] the value to be associated (becomes the value)
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
self.associate { |it| it.to yield(it) }
|
72
|
+
def associate_with_to(association)
|
73
|
+
associate_to(association) { |it| it.to yield(it) }
|
41
74
|
end
|
42
75
|
end
|
43
76
|
end
|