associate_rb 0.0.3 → 0.0.4

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: f6ddfeb80c1669afeec3e1d586af97bb94cad6f407c99b906fd971289c1adf49
4
- data.tar.gz: 74906eddf3068f8084e988f3f8cf2fb6cac036e96c6a0bb2824386fff1310639
3
+ metadata.gz: 9e620210ce4e5e2d5f0095124670962e25a424cf3842d82f31a0ce9b2aa6c958
4
+ data.tar.gz: 35a79c221feacaf7e97ac51f1808d3efde000d8d386ddb0fd19e9be466c6c45d
5
5
  SHA512:
6
- metadata.gz: 3456689d6ac803d68069c8326083c9911ce80915977acf418cfefa3bc5a5ba236f8f8a0d0b0755feff4845706548a143be2efade4bb0a142e006778ef45332ee
7
- data.tar.gz: f1e153ce3e4cd5ba3fbf969ba36be3f66703f55c77e02c3ae376073503f71b36609fd5f2da6439c6fd928cede72e61c73e7d40fe13db8d869f6f4f0862a5a079
6
+ metadata.gz: e60bc2dad674e055e1fbb7f09fbf9de96504e62bb87df76182791a1848947ee15e7e6d1e6d6a55f3f4d932bea7fcc5375452dd6df821552e98c9ba16bc5f2604
7
+ data.tar.gz: 6e1a7021589fcb879a59a8c1818686f100449387e89c961d2bc7707b8666b51e2ef512f0ff0e163f90ecf0a57a0f1b85748479df5bdf73a6ce41a75000431e80
@@ -0,0 +1,76 @@
1
+ module AssociateRB
2
+ module ArrayExtension
3
+ # Returns a [Hash] containing key-value pairs provided by the block
4
+ # applied to elements of the given [Array].
5
+ #
6
+ # @return [Hash] the association
7
+ #
8
+ # @yield [it] Executes the association block
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)
42
+ raise ArgumentError, 'No block provided' unless block_given?
43
+ raise ArgumentError, 'Given association is not a Hash' unless association.is_a?(Hash)
44
+
45
+ self.each do |it|
46
+ associated = yield(it)
47
+ raise ArgumentError, 'Block does not return Hash' unless associated.is_a?(Hash)
48
+
49
+ association.merge!(yield(it))
50
+ end
51
+ association
52
+ end
53
+
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
+ #
58
+ # @return [Hash] the association
59
+ #
60
+ # @yield [it] the value to be associated (becomes the key)
61
+ def associate_by_to(association)
62
+ associate_to(association) { |it| yield(it).to it }
63
+ end
64
+
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.
68
+ #
69
+ # @return [Hash] the association
70
+ #
71
+ # @yield [it] the value to be associated (becomes the value)
72
+ def associate_with_to(association)
73
+ associate_to(association) { |it| it.to yield(it) }
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,11 @@
1
+ module AssociateRB
2
+ module ObjectExtension
3
+ # Creates a key-value pair
4
+ #
5
+ # @param obj [Object] the value
6
+ # @return [Hash] the key-value pair, with the current object being the key
7
+ def to(obj)
8
+ { self => obj }
9
+ end
10
+ end
11
+ 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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Queiroz
@@ -31,6 +31,8 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/associate_rb.rb
34
+ - lib/associate_rb/extensions/array_extension.rb
35
+ - lib/associate_rb/extensions/object_extension.rb
34
36
  - lib/extensions/array_extension.rb
35
37
  - lib/extensions/object_extension.rb
36
38
  homepage: https://github.com/lucasqueiroz/associate-rb