light_mapper 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: ec180a4b4062fa63e032fef32a556894a365dcb4
4
- data.tar.gz: 40f8416865d57b19af6c16bf0b82fe4306261dc1
3
+ metadata.gz: 739e3317c888c4ead5db06449dee80bf264cff23
4
+ data.tar.gz: 353f10793c1cd990659934465b064fe035771f55
5
5
  SHA512:
6
- metadata.gz: 35af7945db5ac71c6353fa873f46a86677a9bcea56e9d4645c57acfbb2a850dc48ff4ee1e847d46fad8ca4bfb7a856ce69cc330cc57a1bf204ebcf2aa7ba719a
7
- data.tar.gz: 9e2693e1fbcc48e7a6a95c10d609aa3536303dcbad048bd52221516bbd96f3f1691775b6d1cceb75f7987c23f42fa4461e3c0dcc2b6f27aa003fba18f8efd8d9
6
+ metadata.gz: 6748d296cb4d18fa6c2f49e6777ec4e269e5eb19c961baa6e57677d8014958aa59c8464cbda208da8ff8b5b5a975974bdfffd86eaf9e6ae3ad00a68f619c8f68
7
+ data.tar.gz: b52174375c53a98b10c09664096fa01ee0a6ec18e841c06a0f12571cd704580c48935c907faec8b32dd77aa6e60a05b518a969ff986f9e3d5e55a62db339131c
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # LightMapper
2
2
 
3
- TODO: Write a gem description
3
+ This is simple mapper for hash.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,7 +20,71 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ ### Basic usage
24
+
25
+ ```ruby
26
+ {
27
+ 'FirstName' => 'Pawel',
28
+ 'LastName' => 'Niemczyk'
29
+ }.extend(LightMapper).mapping(
30
+ 'FirstName' => :first_name,
31
+ 'LastName' => :last_name
32
+ )
33
+ ```
34
+ result is obvious:
35
+
36
+ ```ruby
37
+ { first_name: 'Pawel', last_name: 'Niemczyk' }
38
+ ```
39
+
40
+ The most popular usage:
41
+
42
+ ```ruby
43
+ PersonMapper = {
44
+ 'FirstName' => :first_name,
45
+ 'LastName' => :last_name,
46
+ 'Age' => 'age'
47
+ }
48
+
49
+ data = {
50
+ 'FirstName' => 'Pawel',
51
+ 'LastName' => 'Niemczyk',
52
+ 'Age' => 5
53
+ }
54
+
55
+ data.extend(LightMapper).mapping(PersonMapper)
56
+ ```
57
+
58
+ ### When you require all keys
59
+
60
+ ```ruby
61
+ {
62
+ 'FirstName' => 'Pawel'
63
+ }.extend(LightMapper, require_keys: true).mapping(
64
+ 'FirstName' => :first_name,
65
+ 'LastName' => :last_name
66
+ )
67
+ ```
68
+
69
+ it will raise KeyError
70
+
71
+ ### When you want to pass string or symbol keys
72
+
73
+ ```ruby
74
+ {
75
+ 'FirstName' => 'Pawel',
76
+ second_name: 'Niemczyk'
77
+ }.extend(LightMapper, any_keys_kind: true).mapping(
78
+ 'FirstName' => :first_name,
79
+ 'second_name' => :last_name
80
+ )
81
+ ```
82
+
83
+ result will be:
84
+
85
+ ```ruby
86
+ { first_name: 'Pawel', last_name: 'Niemczyk' }
87
+ ```
24
88
 
25
89
  ## Contributing
26
90
 
data/lib/light_mapper.rb CHANGED
@@ -6,11 +6,24 @@ module LightMapper
6
6
  end
7
7
 
8
8
  def self.mapping(hash, mappings, opts = {})
9
- require_keys = opts[:require_keys] == true
9
+ require_keys = opts[:require_keys] == true
10
+ any_keys_kind = opts[:any_keys_kind] == true
11
+ fetch_method = if any_keys_kind
12
+ if require_keys
13
+ -> (hash, key) { hash.fetch(key.to_s, hash.fetch(key.to_sym)) }
14
+ else
15
+ -> (hash, key) { hash[key.to_s] || hash[key.to_sym] }
16
+ end
17
+ else
18
+ if require_keys
19
+ -> (hash, key) { hash.fetch(key) }
20
+ else
21
+ -> (hash, key) { hash[key] }
22
+ end
23
+ end
10
24
 
11
- fetch_method = require_keys ? :fetch : :[]
12
25
  {}.tap do |h|
13
- mappings.each { |k, v| h[v] = hash.public_send(fetch_method, k) }
26
+ mappings.each { |k, v| h[v] = fetch_method.call(hash, k) }
14
27
  end
15
28
  end
16
29
  end
@@ -1,3 +1,3 @@
1
1
  module LightMapper
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -43,4 +43,48 @@ describe LightMapper do
43
43
  expect { subject.mapping(maper, require_keys: true) }.to raise_error(KeyError)
44
44
  end
45
45
  end
46
+
47
+ describe 'basic data mapping when any keys kind is allowed' do
48
+ let(:original_hash) do
49
+ {
50
+ 'A' => 'test',
51
+ 'b' => 1,
52
+ c: 10
53
+
54
+ }
55
+ end
56
+
57
+ let(:maper) do
58
+ {
59
+ 'A' => :a,
60
+ 'c' => :c,
61
+ b: 'z'
62
+ }
63
+ end
64
+
65
+ it 'return correct hash' do
66
+ expect(subject.mapping(maper, any_keys_kind: true)).to eq(
67
+ a: original_hash['A'],
68
+ c: original_hash[:c],
69
+ 'z' => original_hash['b']
70
+ )
71
+ end
72
+
73
+ describe 'raise KeyError' do
74
+ let(:maper) do
75
+ {
76
+ 'A' => :a,
77
+ 'c' => :c,
78
+ k: 'z'
79
+ }
80
+ end
81
+
82
+ it ' when required key missing' do
83
+ expect { subject.mapping(maper, require_keys: true, any_keys_kind: true) }.to raise_error(KeyError)
84
+ end
85
+ end
86
+ end
87
+
88
+ it 'nested keys'
89
+ it 'value conversion'
46
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pawel Niemczyk