dozer 0.2.1 → 0.3.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
  SHA256:
3
- metadata.gz: 9e680861f2a21ec0d718ba6369b0915de1bbbcadce54992186df483fbe985eb8
4
- data.tar.gz: b8443f913b615371c7fc6fb090f02eea76501bf8cb827140702e83ebe3594eca
3
+ metadata.gz: 87b4851a11a83f14e78ff8c3f3c975a42eb70a88d4dc6e4c9e6bea83e475374b
4
+ data.tar.gz: 74dea50634b680aa55ad6c65e041f32e4709a00bd21e2568196c0568dd2a5d59
5
5
  SHA512:
6
- metadata.gz: 10c9e24bf72fd131fd4c4f71ea883ec75a2fca4c5c6cf6cbd003ac8d32666198d35e5baf5afadf183861b6896f93d37574ec03c3e326599947111208fa276f8b
7
- data.tar.gz: ac562a007a21a391ef87816192729c600c4f8eb0b7fc4bc40d0e3b85ac89e4a13a5cc6cc72007283a5bfb63b7f9e2ed808edc976c5f7036df7c06b64ce7eeb6d
6
+ metadata.gz: 9091b06596a3ec2adbe8584ca03df3253e6f8fc6bb692f725659c31a73f93ed8156eb27577adbf51e5de8620a65f51620dad040811f37888ae054b34c7e03646
7
+ data.tar.gz: 0d9f6f598cab72c01c8e60ea6eff1899d07e4c96195f1403e76ec3beae00356660ef4d22bdea8de02373d6d30e9bbc8ef3b86f835f259a605387f43dc3b59ef4
@@ -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
- rule = Dozer::Rule.new(options.merge(base_klass: self))
8
- append_rule(rule)
18
+ append_rule(Dozer::Rule.new(options))
9
19
  end
10
20
 
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
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 :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?
data/lib/dozer/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dozer
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.2"
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.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: 2020-08-11 00:00:00.000000000 Z
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.0.6
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