dozer 0.2.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dozer/mapperable.rb +16 -11
- data/lib/dozer/rule.rb +19 -13
- data/lib/dozer/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87b4851a11a83f14e78ff8c3f3c975a42eb70a88d4dc6e4c9e6bea83e475374b
|
4
|
+
data.tar.gz: 74dea50634b680aa55ad6c65e041f32e4709a00bd21e2568196c0568dd2a5d59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9091b06596a3ec2adbe8584ca03df3253e6f8fc6bb692f725659c31a73f93ed8156eb27577adbf51e5de8620a65f51620dad040811f37888ae054b34c7e03646
|
7
|
+
data.tar.gz: 0d9f6f598cab72c01c8e60ea6eff1899d07e4c96195f1403e76ec3beae00356660ef4d22bdea8de02373d6d30e9bbc8ef3b86f835f259a605387f43dc3b59ef4
|
data/lib/dozer/mapperable.rb
CHANGED
@@ -2,21 +2,26 @@ module Dozer
|
|
2
2
|
module Mapperable
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
included do
|
6
|
+
attr_accessor :input, :output, :options
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(input, data)
|
10
|
+
@input = input.with_indifferent_access
|
11
|
+
@output = ActiveSupport::HashWithIndifferentAccess.new
|
12
|
+
@options = data.with_indifferent_access
|
13
|
+
end
|
14
|
+
|
5
15
|
module ClassMethods
|
16
|
+
|
6
17
|
def mapping(options)
|
7
|
-
|
8
|
-
append_rule(rule)
|
18
|
+
append_rule(Dozer::Rule.new(options))
|
9
19
|
end
|
10
20
|
|
11
|
-
def transform(input,
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
key, value = kv.first, kv.last
|
16
|
-
output[key] = value
|
17
|
-
end
|
18
|
-
|
19
|
-
output
|
21
|
+
def transform(input, data={})
|
22
|
+
instance = self.new(input, data)
|
23
|
+
all_rules.each { |rule| rule.apply!(instance) }
|
24
|
+
instance.output
|
20
25
|
end
|
21
26
|
|
22
27
|
private
|
data/lib/dozer/rule.rb
CHANGED
@@ -1,23 +1,29 @@
|
|
1
1
|
module Dozer
|
2
2
|
class Rule
|
3
|
-
attr_accessor :
|
3
|
+
attr_accessor :from, :to, :func
|
4
4
|
|
5
5
|
def initialize(options)
|
6
6
|
options = options.with_indifferent_access
|
7
7
|
validate!(options)
|
8
8
|
|
9
|
-
@base_klass = options[:base_klass]
|
10
9
|
@from = options[:from].to_sym
|
11
10
|
@to = options[:to].to_sym
|
12
11
|
@func = options[:func]
|
13
12
|
end
|
14
13
|
|
15
|
-
def apply(
|
16
|
-
if applicable?(input)
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
def apply!(instance)
|
15
|
+
return if !applicable?(instance.input)
|
16
|
+
|
17
|
+
value = case
|
18
|
+
when func.nil?
|
19
|
+
instance.input[from]
|
20
|
+
when func.is_a?(Proc)
|
21
|
+
func.call(instance.input[from])
|
22
|
+
when func.is_a?(Symbol) && instance.respond_to?(func)
|
23
|
+
instance.send(func)
|
20
24
|
end
|
25
|
+
|
26
|
+
instance.output[to] = value
|
21
27
|
end
|
22
28
|
|
23
29
|
private
|
@@ -26,12 +32,12 @@ module Dozer
|
|
26
32
|
input.key?(from)
|
27
33
|
end
|
28
34
|
|
29
|
-
def evaluate(value)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
35
|
+
# def evaluate(value)
|
36
|
+
# return value if func.nil?
|
37
|
+
# return func.call(value) if func.is_a?(Proc)
|
38
|
+
# return base_klass.new.send(func, value) if func.is_a?(Symbol)
|
39
|
+
# nil
|
40
|
+
# end
|
35
41
|
|
36
42
|
def validate!(options)
|
37
43
|
raise ArgumentError, 'from is missing.' if options[:from].nil?
|
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.2
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Workstream.us
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
|
-
rubygems_version: 3.
|
126
|
+
rubygems_version: 3.4.19
|
127
127
|
signing_key:
|
128
128
|
specification_version: 4
|
129
129
|
summary: map a hash from one schema to another schema
|