key_mapable 0.1.0 → 0.2.0
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 +3 -2
- data/lib/key_mapable/mapper.rb +1 -1
- data/lib/key_mapable/version.rb +1 -1
- data/lib/key_mapable.rb +2 -29
- 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: c206c426ca310ad9ed08b6c2c68430f7df31d310e88a741fa0183093f1ec6496
|
4
|
+
data.tar.gz: 77082bfa61c500b83187eb46bd360b120ba4a3e61e792e8dc26b029591bad28e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '096b7422f4e77ead9f5886db3a0a9a091409319f199b9c1b18cf4b1b3be1ffc776f8e10daa4cf78ca9f953a5953ec61b7c6f93a128e2d3c88cd767470be9cf13'
|
7
|
+
data.tar.gz: c263a1fd58458d03388129dd49ab41a93613fa45a9cbd9caaf5df53983651a7b71432daa6a3099ee8b11642bae9449bba093682398b0367ac339ec0ca6960051
|
data/README.md
CHANGED
@@ -25,8 +25,9 @@ class MyClass
|
|
25
25
|
extend KeyMapable
|
26
26
|
|
27
27
|
# Define a method called `#to_h` that use the provided map. The keys
|
28
|
-
# will be accessed on the provided subject.
|
29
|
-
|
28
|
+
# will be accessed on the provided subject. Transform the resulting hash
|
29
|
+
# with the provided lambda.
|
30
|
+
define_map(:to_h, resolve: ->(val) { val }, subject: :my_reader) do
|
30
31
|
# Map the value of `#name` to the key 'Name'.
|
31
32
|
key_map(:name, 'Name')
|
32
33
|
|
data/lib/key_mapable/mapper.rb
CHANGED
data/lib/key_mapable/version.rb
CHANGED
data/lib/key_mapable.rb
CHANGED
@@ -3,40 +3,13 @@
|
|
3
3
|
require "key_mapable/version"
|
4
4
|
require "key_mapable/mapper"
|
5
5
|
|
6
|
-
# Used to map the instance of the extended class to a new format.
|
7
|
-
#
|
8
|
-
# Usage:
|
9
|
-
#
|
10
|
-
# class MyClass
|
11
|
-
# extend KeyMapable
|
12
|
-
#
|
13
|
-
# # Define a method called `#to_h` that use the provided map. The keys
|
14
|
-
# # will be accessed on the provided subject.
|
15
|
-
# define_map(:to_h, subject: :my_reader) do
|
16
|
-
# # Map the value of `#name` to the key 'Name'.
|
17
|
-
# key_map(:name, 'Name')
|
18
|
-
#
|
19
|
-
# # Map the value of `#maybe_value` to the key 'GuaranteedValue'.
|
20
|
-
# # Transform the value by calling `#to_s` first.
|
21
|
-
# key_map(:maybe_value, 'GuaranteedValue', &:to_s)
|
22
|
-
#
|
23
|
-
# # Map the key 'Name' to the value provided by the block.
|
24
|
-
# key_value('AConstant') { 'Foo' }
|
25
|
-
#
|
26
|
-
# # Map every item returned from `#rows`.
|
27
|
-
# array_key_map(:rows, 'Rows') do
|
28
|
-
# # Map the value of `#id` to the key 'Id'.
|
29
|
-
# key_map(:id, 'Id')
|
30
|
-
# end
|
31
|
-
# end
|
32
|
-
# end
|
33
6
|
module KeyMapable
|
34
|
-
def define_map(method_name, subject: :
|
7
|
+
def define_map(method_name, resolve: ->(val) { val }, subject: :itself, &block)
|
35
8
|
define_method(method_name) do
|
36
9
|
value = public_send(subject)
|
37
10
|
mapper = Mapper.new(value)
|
38
11
|
mapper.instance_eval(&block)
|
39
|
-
mapper.
|
12
|
+
resolve.call(mapper.tree)
|
40
13
|
end
|
41
14
|
end
|
42
15
|
end
|