light_mapper 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +66 -2
- data/lib/light_mapper.rb +16 -3
- data/lib/light_mapper/version.rb +1 -1
- data/spec/lib/light_mapper_spec.rb +44 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 739e3317c888c4ead5db06449dee80bf264cff23
|
4
|
+
data.tar.gz: 353f10793c1cd990659934465b064fe035771f55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6748d296cb4d18fa6c2f49e6777ec4e269e5eb19c961baa6e57677d8014958aa59c8464cbda208da8ff8b5b5a975974bdfffd86eaf9e6ae3ad00a68f619c8f68
|
7
|
+
data.tar.gz: b52174375c53a98b10c09664096fa01ee0a6ec18e841c06a0f12571cd704580c48935c907faec8b32dd77aa6e60a05b518a969ff986f9e3d5e55a62db339131c
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# LightMapper
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
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] =
|
26
|
+
mappings.each { |k, v| h[v] = fetch_method.call(hash, k) }
|
14
27
|
end
|
15
28
|
end
|
16
29
|
end
|
data/lib/light_mapper/version.rb
CHANGED
@@ -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
|