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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04faca4917e7b3ad32a2714d2c2e1efdf4572accc1d0cf8668514fbb93e91e84
4
- data.tar.gz: 85dbcebfcf053aa9cae04a9c0f8d0616cd1ed9985c108af4ddf93db9ac25f493
3
+ metadata.gz: c206c426ca310ad9ed08b6c2c68430f7df31d310e88a741fa0183093f1ec6496
4
+ data.tar.gz: 77082bfa61c500b83187eb46bd360b120ba4a3e61e792e8dc26b029591bad28e
5
5
  SHA512:
6
- metadata.gz: a7f232c795d1e8c1dd6e00c2f591166796b3efde87c43d71f24bcb87d0251a5ee8502048f37b57138ea8059ae48cfdd5a29f4bd064531642837a44fdfb48239a
7
- data.tar.gz: 03114cb42d6193b74aa80ddb965134c90d5dcf8dde35602a174a325484cceee6d9cbfae0c102d776479cecb7266990f23f012ff3eb9236a4dd1a50b360420468
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
- define_map(:to_h, subject: :my_reader) do
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
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  # Used internally by the KeyMapable concern.
4
4
  class KeyMapable::Mapper
5
- attr_reader :value
5
+ attr_reader :value, :tree
6
6
 
7
7
  def initialize(value)
8
8
  @value = value
@@ -1,3 +1,3 @@
1
1
  module KeyMapable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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: :self, &block)
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.resolve
12
+ resolve.call(mapper.tree)
40
13
  end
41
14
  end
42
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: key_mapable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emric