mappy 0.0.2 → 0.0.3
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 +4 -4
- data/lib/mappy.rb +2 -2
- data/lib/mappy/configuration.rb +6 -4
- data/lib/mappy/resolver.rb +12 -4
- data/lib/mappy/version.rb +1 -1
- data/spec/lib/mappy/resolver_spec.rb +22 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81a2df0e93a30d9dc7301a2dc43b437e71288edc
|
4
|
+
data.tar.gz: 68ae080d16ea598b40dc21cb77f9d35b7990ca92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 09b819564c909b535c0358f18ecfc25493c10b913f79c1cfc9d6421a98fe8f67841bee0de5f19fe677cc1bb5ed73988d16a4dd96461959207413b7b40d74287a
|
7
|
+
data.tar.gz: 05dcd31687345b91f2963781ae8f79a257689bbb7289868fd31d316debb49284860958132eb0c47481ed569ece7e8f6b1bda9d701752632c28084b1bf9c81d7d
|
data/lib/mappy.rb
CHANGED
@@ -25,14 +25,14 @@ module Mappy
|
|
25
25
|
# The Rails load sequence means that some of the configured Targets may
|
26
26
|
# not be loaded; As such I am not calling configure! instead relying on
|
27
27
|
# Mappy::Railtie to handle the configure! call
|
28
|
-
|
28
|
+
finalize! unless defined?(Rails)
|
29
29
|
end
|
30
30
|
|
31
31
|
def map(source, options = {})
|
32
32
|
configuration.map(source, options)
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
35
|
+
def finalize!
|
36
36
|
if @configuration_block.respond_to?(:call)
|
37
37
|
self.configuration ||= Configuration.new
|
38
38
|
@configuration_block.call(configuration)
|
data/lib/mappy/configuration.rb
CHANGED
@@ -19,15 +19,17 @@ module Mappy
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def map(source_instance, options = {})
|
22
|
-
source = source_instance.to_mappy_type
|
23
22
|
target = options.fetch(:target)
|
24
23
|
target_builder_finder = options.fetch(:target_builder_finder) { Mappy::TargetBuilderFactory }
|
25
|
-
legend = map_store.fetch(target).fetch(source)
|
26
24
|
|
25
|
+
source = source_instance.to_mappy_type
|
26
|
+
legend = map_store.fetch(target).fetch(source)
|
27
27
|
target_builder = target_builder_finder.call(target)
|
28
28
|
|
29
|
-
|
30
|
-
|
29
|
+
resolve(source_instance, target_builder, legend)
|
30
|
+
end
|
31
|
+
protected
|
32
|
+
def resolve(source_instance, target_builder, legend)
|
31
33
|
Resolver.new(
|
32
34
|
target_builder: target_builder,
|
33
35
|
legend: legend
|
data/lib/mappy/resolver.rb
CHANGED
@@ -9,15 +9,23 @@ module Mappy
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def call(source)
|
12
|
-
|
13
|
-
|
12
|
+
return source if source.is_a?(target_builder)
|
13
|
+
attributes = extract_target_attributes_from(source)
|
14
|
+
instantiate_target(attributes)
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def extract_target_attributes_from(source)
|
20
|
+
legend.each_with_object({}) do |(source_method, target_attribute), m|
|
14
21
|
m[target_attribute] = extract_attribute_for(source, source_method)
|
15
22
|
m
|
16
23
|
end
|
17
|
-
target_builder.new(attributes)
|
18
24
|
end
|
19
25
|
|
20
|
-
|
26
|
+
def instantiate_target(attributes)
|
27
|
+
target_builder.new(attributes)
|
28
|
+
end
|
21
29
|
|
22
30
|
def extract_attribute_for(source, method_name_or_proc)
|
23
31
|
if method_name_or_proc.respond_to?(:call)
|
data/lib/mappy/version.rb
CHANGED
@@ -3,8 +3,6 @@ require 'ostruct'
|
|
3
3
|
|
4
4
|
module Mappy
|
5
5
|
describe Resolver do
|
6
|
-
let(:source_publisher) { lambda {|source| source.publishers.join("; ")}}
|
7
|
-
let(:source) { double('Source', title: 'A Title', publishers: ["John", "Ringo"]) }
|
8
6
|
let(:target_builder) { OpenStruct }
|
9
7
|
let(:legend) {
|
10
8
|
[
|
@@ -12,13 +10,31 @@ module Mappy
|
|
12
10
|
[source_publisher, :publisher]
|
13
11
|
]
|
14
12
|
}
|
13
|
+
let(:source_publisher) { lambda {|source| source.publishers.join("; ")}}
|
14
|
+
subject { described_class.new(target_builder: target_builder, legend: legend).call(source) }
|
15
15
|
|
16
16
|
context '#call' do
|
17
|
-
|
18
|
-
|
17
|
+
context 'source is not an instance of the target builder' do
|
18
|
+
let(:source) { double('Source', title: 'A Title', publishers: ["John", "Ringo"]) }
|
19
|
+
let(:resolved_publisher) { source_publisher.call(source) }
|
20
|
+
|
21
|
+
it 'should return an instance of the target_builder' do
|
22
|
+
expect(subject).to be_an_instance_of(target_builder)
|
23
|
+
end
|
24
|
+
|
25
|
+
its(:publisher) { should eq resolved_publisher }
|
26
|
+
its(:title) { should eq source.title }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'source is an instance of the target builder' do
|
30
|
+
let(:source) { target_builder.new(publisher: 'Hello', title: 'World') }
|
19
31
|
|
20
|
-
|
21
|
-
|
32
|
+
it 'should return the unaltered source' do
|
33
|
+
expect(subject).to eq source
|
34
|
+
end
|
35
|
+
its(:publisher) { should eq source.publisher }
|
36
|
+
its(:title) { should eq source.title }
|
37
|
+
end
|
22
38
|
end
|
23
39
|
end
|
24
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mappy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Friesen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|