dozer 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e680861f2a21ec0d718ba6369b0915de1bbbcadce54992186df483fbe985eb8
4
- data.tar.gz: b8443f913b615371c7fc6fb090f02eea76501bf8cb827140702e83ebe3594eca
3
+ metadata.gz: 836066ffb0fbb3e71155b9c8c5310429caae2ab5c461a482758dbbe3e0e7d94f
4
+ data.tar.gz: 6a513410751eee299ee93c4eef48aabf880dfcf6ddf2c74113925b52295f30be
5
5
  SHA512:
6
- metadata.gz: 10c9e24bf72fd131fd4c4f71ea883ec75a2fca4c5c6cf6cbd003ac8d32666198d35e5baf5afadf183861b6896f93d37574ec03c3e326599947111208fa276f8b
7
- data.tar.gz: ac562a007a21a391ef87816192729c600c4f8eb0b7fc4bc40d0e3b85ac89e4a13a5cc6cc72007283a5bfb63b7f9e2ed808edc976c5f7036df7c06b64ce7eeb6d
6
+ metadata.gz: 74be06f97c5baa3fa776e82187ed262b1276f33973a62d2cd1ca0c0a46a0ba77c3bf82ea723df4b4be19b837959b785f632636b1d761097d1b3d1d7d03f9f34e
7
+ data.tar.gz: 357302fad4df18f5dcebbc3aa6cd5e4270c4342d6b9192b3a629e986f74705ab1fd34d31f1e97e1f8dfdae6029a911e0148b237212e3c6f9aeb770d3365d0d11
@@ -9,6 +9,6 @@ module Dozer
9
9
 
10
10
  # transform the data from one schema to another schema.
11
11
  def self.map(hash, mapper, options={})
12
- mapper.transform(hash, options)
12
+ mapper.transform(hash)
13
13
  end
14
14
  end
@@ -2,21 +2,25 @@ module Dozer
2
2
  module Mapperable
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ included do
6
+ attr_accessor :input, :output
7
+ end
8
+
9
+ def initialize(input)
10
+ @input = input.with_indifferent_access
11
+ @output = ActiveSupport::HashWithIndifferentAccess.new
12
+ end
13
+
5
14
  module ClassMethods
15
+
6
16
  def mapping(options)
7
- rule = Dozer::Rule.new(options.merge(base_klass: self))
8
- append_rule(rule)
17
+ append_rule(Dozer::Rule.new(options))
9
18
  end
10
19
 
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
-
19
- output
20
+ def transform(input)
21
+ instance = self.new(input)
22
+ all_rules.each { |rule| rule.apply!(instance) }
23
+ instance.output
20
24
  end
21
25
 
22
26
  private
@@ -1,23 +1,29 @@
1
1
  module Dozer
2
2
  class Rule
3
- attr_accessor :base_klass, :from, :to, :func
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(input)
16
- if applicable?(input)
17
- [to, evaluate(input[from])]
18
- else
19
- nil
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
- 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
+ # 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?
@@ -1,3 +1,3 @@
1
1
  module Dozer
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.1"
3
3
  end
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.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Workstream.us
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-11 00:00:00.000000000 Z
11
+ date: 2020-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler