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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2226fb6bd49b3122bdf396d0cf45179dc6fc4c819c45ac67df4bc359338c70e6
4
- data.tar.gz: ebbd89597b05a4cfdb5c642a7b23f73ed0f09431bc327400792aa29fa727234e
3
+ metadata.gz: f6ddfeb80c1669afeec3e1d586af97bb94cad6f407c99b906fd971289c1adf49
4
+ data.tar.gz: 74906eddf3068f8084e988f3f8cf2fb6cac036e96c6a0bb2824386fff1310639
5
5
  SHA512:
6
- metadata.gz: f86605f0c5dda42e23832d87f585b84ca1d0b0b6b7d93fa795ac0041bb94e9dc4ce0a49521830b5b0e15615daa9590638a8882ddb9235681b78c95e719a7aa0e
7
- data.tar.gz: 3ec09d6dd17d3a343751bcb5c060ea4656144a548e83054eb30a8c4cf937408dd98785019b26e1667453f5bc68b98f5e4e73b1fd70c876a427331b39ec3f34b4
6
+ metadata.gz: 3456689d6ac803d68069c8326083c9911ce80915977acf418cfefa3bc5a5ba236f8f8a0d0b0755feff4845706548a143be2efade4bb0a142e006778ef45332ee
7
+ data.tar.gz: f1e153ce3e4cd5ba3fbf969ba36be3f66703f55c77e02c3ae376073503f71b36609fd5f2da6439c6fd928cede72e61c73e7d40fe13db8d869f6f4f0862a5a079
@@ -1,14 +1,47 @@
1
1
  module AssociateRB
2
2
  module ArrayExtension
3
- # Associates each element of the Array with the given value
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
- # Associates each element of the Array by the value of the provided block
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 associate_by
27
- raise ArgumentError, 'No block provided' unless block_given?
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
- # Associates each element of the Array with the value of the provided block
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 associate_with
38
- raise ArgumentError, 'No block provided' unless block_given?
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: associate_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Queiroz