dozer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -4
- data/lib/dozer.rb +1 -0
- data/lib/dozer/mapperable.rb +21 -24
- data/lib/dozer/rule.rb +46 -0
- data/lib/dozer/version.rb +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c48283b209b97ad20861d07a987e82f19f989ac9e4c579bfcb9c78edf6ecf96b
|
4
|
+
data.tar.gz: 513fc4e131a49c134a20778b8d0c8b878d668f785ba3900f09dd3ee465e8a1e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b30fffa2ff17caa0a8ddf55a600be3ed9c6e5bd6f462806b28745bf11b114993215d9367158324212b58ac9221d0afeaecd6a34ef49702e614f6e5624578f32
|
7
|
+
data.tar.gz: 2264635efc3122b76b3a860a424659733c57e2713768262a9b1f15b6625f1b1f5a6507e66c36b425ff0d490c47912026ab23a1f084c0f6d90bd8b681221ebaaf
|
data/README.md
CHANGED
@@ -32,12 +32,45 @@ Or install it yourself as:
|
|
32
32
|
class AdpMapper
|
33
33
|
include Dozer::Mapperable
|
34
34
|
|
35
|
-
mapping from: 'adp/firstName',
|
36
|
-
mapping from: 'adp/lastName',
|
37
|
-
mapping from: 'adp/gender',
|
35
|
+
mapping from: 'adp/firstName', to: :first_name
|
36
|
+
mapping from: 'adp/lastName', to: :last_name
|
37
|
+
mapping from: 'adp/gender', to: :gender
|
38
38
|
end
|
39
39
|
```
|
40
|
-
|
40
|
+
|
41
|
+
How to transform the value when mapping? You can do it in two ways: proc or method.
|
42
|
+
|
43
|
+
Usage example of Proc
|
44
|
+
|
45
|
+
```
|
46
|
+
class AdpMapper
|
47
|
+
include Dozer::Mapperable
|
48
|
+
|
49
|
+
mapping from: 'adp/gender', to: :gender, func: -> (value) { {male: 'M', female: 'F'}.fetch(value) }
|
50
|
+
mapping from: 'marital_status', to: :marriage,
|
51
|
+
mapping from: 'adp/gender', to: :gender
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Usage example of method
|
56
|
+
|
57
|
+
```
|
58
|
+
class AdpMapper
|
59
|
+
include Dozer::Mapperable
|
60
|
+
|
61
|
+
mapping from: 'gender', to: :gender, func: :gender_to_integer
|
62
|
+
mapping from: 'marital_status', to: :marriage,
|
63
|
+
|
64
|
+
def gender_to_integer(val)
|
65
|
+
new_value = {male: 1, female: 2}.fetch(val, nil)
|
66
|
+
new_value
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
41
74
|
If you have multiple integrations, you can define a set of mapper classes in your Rails application folder.
|
42
75
|
|
43
76
|
```
|
data/lib/dozer.rb
CHANGED
data/lib/dozer/mapperable.rb
CHANGED
@@ -4,39 +4,36 @@ module Dozer
|
|
4
4
|
|
5
5
|
module ClassMethods
|
6
6
|
def mapping(options)
|
7
|
-
|
8
|
-
|
9
|
-
raise ArgumentError, 'to is missing' if options[:to].nil?
|
10
|
-
|
11
|
-
dozer_forward_mapper[options[:from]] = options[:to].to_sym
|
12
|
-
dozer_backward_mapper[options[:to]] = options[:from].to_sym
|
7
|
+
rule = Dozer::Rule.new(options.merge(base_klass: self))
|
8
|
+
append_rule(rule)
|
13
9
|
end
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
def transform(input, options={})
|
12
|
+
input, output = input.with_indifferent_access, ActiveSupport::HashWithIndifferentAccess.new
|
13
|
+
kvs = all_rules.map { |rule| rule.apply(input) }.compact!
|
14
|
+
kvs.each do |kv|
|
15
|
+
key, value = kv.first, kv.last
|
16
|
+
output[key] = value
|
17
|
+
end
|
18
18
|
|
19
|
-
|
20
|
-
@dozer_backward_mapper ||= {}.with_indifferent_access
|
19
|
+
output
|
21
20
|
end
|
22
21
|
|
23
|
-
|
24
|
-
result = {}.with_indifferent_access
|
25
|
-
|
26
|
-
options = options.with_indifferent_access
|
27
|
-
mapper = if options[:reverse] == true
|
28
|
-
dozer_backward_mapper
|
29
|
-
else
|
30
|
-
dozer_forward_mapper
|
31
|
-
end
|
22
|
+
private
|
32
23
|
|
33
|
-
|
34
|
-
|
35
|
-
|
24
|
+
def append_rule(new_rule)
|
25
|
+
if all_rules.any? {|r| r.to == new_rule.to}
|
26
|
+
raise ArgumentError, "to: :#{new_rule.to} has been declared."
|
36
27
|
end
|
37
28
|
|
38
|
-
|
29
|
+
all_rules << new_rule
|
39
30
|
end
|
31
|
+
|
32
|
+
|
33
|
+
private def all_rules
|
34
|
+
@__all_rules ||= []
|
35
|
+
end
|
36
|
+
|
40
37
|
end # end of ClassMethods
|
41
38
|
|
42
39
|
end # end of Mapperable
|
data/lib/dozer/rule.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Dozer
|
2
|
+
class Rule
|
3
|
+
attr_accessor :base_klass, :from, :to, :func
|
4
|
+
|
5
|
+
def initialize(options)
|
6
|
+
options = options.with_indifferent_access
|
7
|
+
validate!(options)
|
8
|
+
|
9
|
+
@base_klass = options[:base_klass]
|
10
|
+
@from = options[:from].to_sym
|
11
|
+
@to = options[:to].to_sym
|
12
|
+
@func = options[:func]
|
13
|
+
end
|
14
|
+
|
15
|
+
def apply(input)
|
16
|
+
if applicable?(input)
|
17
|
+
[to, evaluate(input[from])]
|
18
|
+
else
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def applicable?(input)
|
26
|
+
input.key?(from)
|
27
|
+
end
|
28
|
+
|
29
|
+
def evaluate(value)
|
30
|
+
return value if func.nil?
|
31
|
+
return func.call(value) if func.is_a?(Proc)
|
32
|
+
return base_klass.new.send(func, value) if func.is_a?(Symbol)
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def validate!(options)
|
37
|
+
raise ArgumentError, 'from is missing.' if options[:from].nil?
|
38
|
+
raise ArgumentError, 'to is missing.' if options[:to].nil?
|
39
|
+
if !(options[:func].nil? || options[:func].is_a?(Symbol) || options[:func].is_a?(Proc))
|
40
|
+
raise ArgumentError, 'func should be a symbol or proc.'
|
41
|
+
end
|
42
|
+
true
|
43
|
+
end
|
44
|
+
|
45
|
+
end # end of Rule
|
46
|
+
end # end of Dozer
|
data/lib/dozer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dozer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Workstream.us
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -102,12 +102,13 @@ files:
|
|
102
102
|
- dozer.gemspec
|
103
103
|
- lib/dozer.rb
|
104
104
|
- lib/dozer/mapperable.rb
|
105
|
+
- lib/dozer/rule.rb
|
105
106
|
- lib/dozer/version.rb
|
106
107
|
homepage: https://github.com/helloworld1812/dozer
|
107
108
|
licenses:
|
108
109
|
- MIT
|
109
110
|
metadata: {}
|
110
|
-
post_install_message:
|
111
|
+
post_install_message:
|
111
112
|
rdoc_options: []
|
112
113
|
require_paths:
|
113
114
|
- lib
|
@@ -123,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
124
|
version: '0'
|
124
125
|
requirements: []
|
125
126
|
rubygems_version: 3.0.6
|
126
|
-
signing_key:
|
127
|
+
signing_key:
|
127
128
|
specification_version: 4
|
128
129
|
summary: map a hash from one schema to another schema
|
129
130
|
test_files: []
|