mappy 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mappy.rb +1 -0
- data/lib/mappy/configuration.rb +13 -9
- data/lib/mappy/exceptions.rb +9 -0
- data/lib/mappy/map_store.rb +29 -0
- data/lib/mappy/version.rb +1 -1
- data/spec/lib/mappy/configuration_spec.rb +2 -2
- data/spec/lib/mappy/map_store_spec.rb +25 -0
- data/spec/lib/mappy_spec.rb +15 -6
- data/spec/lib/model_support.rb +2 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67e684a0fd493690919a03ee753f5e028805d822
|
4
|
+
data.tar.gz: 19d582b225116a7d35e859f060caaeb2d5d06efd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86d39e05a66b27082f4a6063f6ca57ac106da70bf7d945da71022498fb84ac18b1f6ef9fab87a93c453af80a9e29f8255d7eefa95ef803d73282e273ca6f364a
|
7
|
+
data.tar.gz: 17a04b501f516948821fb4f720e7e2837c65f7906ac6032a54b7c361dc1a61d1e64b167dcac1e4297f9afcb75a5e056627516159cb8d303de08ea0e2fc1afd19
|
data/lib/mappy.rb
CHANGED
data/lib/mappy/configuration.rb
CHANGED
@@ -1,29 +1,25 @@
|
|
1
1
|
require "mappy/resolver"
|
2
2
|
require "mappy/target_builder_factory"
|
3
|
+
require "mappy/map_store"
|
3
4
|
|
4
5
|
module Mappy
|
5
6
|
|
6
7
|
class Configuration
|
7
8
|
attr_reader :map_store
|
8
9
|
def initialize(config = {})
|
9
|
-
@map_store = config.fetch(:map_store) {
|
10
|
+
@map_store = config.fetch(:map_store) { Mappy::MapStore.new }
|
10
11
|
end
|
11
12
|
|
12
13
|
def legend(options = {})
|
13
|
-
|
14
|
-
target = options.fetch(:target)
|
15
|
-
legend = options.fetch(:legend)
|
16
|
-
|
17
|
-
map_store[target] ||= {}
|
18
|
-
map_store[target][source] = legend
|
14
|
+
map_store.write(options)
|
19
15
|
end
|
20
16
|
|
21
17
|
def map(source_instance, options = {})
|
22
18
|
target = options.fetch(:target)
|
23
19
|
target_builder_finder = options.fetch(:target_builder_finder) { Mappy::TargetBuilderFactory }
|
24
20
|
|
25
|
-
|
26
|
-
legend =
|
21
|
+
source_type = extract_source_type(source_instance)
|
22
|
+
legend = extract_legend(target, source_type)
|
27
23
|
target_builder = target_builder_finder.call(target)
|
28
24
|
|
29
25
|
resolve(source_instance, target_builder, legend)
|
@@ -35,6 +31,14 @@ module Mappy
|
|
35
31
|
legend: legend
|
36
32
|
).call(source_instance)
|
37
33
|
end
|
34
|
+
|
35
|
+
def extract_legend(target, source_type)
|
36
|
+
map_store.read(source: source_type, target: target)
|
37
|
+
end
|
38
|
+
|
39
|
+
def extract_source_type(source_instance)
|
40
|
+
Mappy.to_type(source_instance)
|
41
|
+
end
|
38
42
|
end
|
39
43
|
|
40
44
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path('../exceptions', __FILE__)
|
2
|
+
|
3
|
+
module Mappy
|
4
|
+
|
5
|
+
class MapStore
|
6
|
+
def initialize
|
7
|
+
@storage = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def write(options = {})
|
11
|
+
source = options.fetch(:source)
|
12
|
+
target = options.fetch(:target)
|
13
|
+
legend = options.fetch(:legend)
|
14
|
+
@storage[target] ||= {}
|
15
|
+
@storage[target][source] = legend
|
16
|
+
end
|
17
|
+
|
18
|
+
def read(options = {})
|
19
|
+
source = options.fetch(:source)
|
20
|
+
target = options.fetch(:target)
|
21
|
+
begin
|
22
|
+
@storage.fetch(target).fetch(source)
|
23
|
+
rescue KeyError
|
24
|
+
raise MapNotFound.new(self, source: source, target: target)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/mappy/version.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path('../../../../lib/mappy/configuration', __FILE__)
|
|
2
2
|
|
3
3
|
module Mappy
|
4
4
|
describe Configuration do
|
5
|
-
let(:map_store) {
|
5
|
+
let(:map_store) { Mappy::MapStore.new }
|
6
6
|
let(:legend) { [[:title, :title]] }
|
7
7
|
|
8
8
|
subject { described_class.new(map_store: map_store) }
|
@@ -10,7 +10,7 @@ module Mappy
|
|
10
10
|
context '#legend' do
|
11
11
|
it 'should yield a map storage' do
|
12
12
|
subject.legend(source: :article, target: :document, legend: legend)
|
13
|
-
expect(map_store.
|
13
|
+
expect(map_store.read(source: :article, target: :document)).to eq(legend)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path('../../../../lib/mappy/map_store', __FILE__)
|
2
|
+
|
3
|
+
module Mappy
|
4
|
+
describe MapStore do
|
5
|
+
subject { described_class.new }
|
6
|
+
|
7
|
+
context 'write/read' do
|
8
|
+
let(:source) { double('Source')}
|
9
|
+
let(:target) { double('Target')}
|
10
|
+
let(:legend) { double('Legend')}
|
11
|
+
it 'should read the written values' do
|
12
|
+
subject.write(source: source, target: target, legend: legend)
|
13
|
+
expect(subject.read(source: source, target: target)).to eq(legend)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should raise an error when attempting to write missing options' do
|
17
|
+
expect { subject.write }.to raise_error KeyError
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should raise an error when attempting to write missing options' do
|
21
|
+
expect { subject.read(source: source, target: target) }.to raise_error Mappy::MapNotFound
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/lib/mappy_spec.rb
CHANGED
@@ -3,11 +3,17 @@ require File.expand_path('../model_support', __FILE__)
|
|
3
3
|
|
4
4
|
describe Mappy do
|
5
5
|
context '.to_type' do
|
6
|
-
|
7
|
-
let(:object) { double(to_mappy_type: mappy_type)}
|
6
|
+
|
8
7
|
it 'with explicit to mappy type' do
|
9
|
-
|
8
|
+
object = double(to_mappy_type: 'hello-world')
|
9
|
+
expect(Mappy.to_type(object)).to eq('hello-world')
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'without explicit to mappy type' do
|
13
|
+
object = Mappy::WithoutExplicitMappyType.new
|
14
|
+
expect(Mappy.to_type(object)).to eq('mappy/without_explicit_mappy_type')
|
10
15
|
end
|
16
|
+
|
11
17
|
end
|
12
18
|
|
13
19
|
context '.map' do
|
@@ -29,10 +35,13 @@ describe Mappy do
|
|
29
35
|
|
30
36
|
let(:title) { 'A Rocking Title' }
|
31
37
|
let(:journal) { double('Journal', to_mappy_type: :journal, title: title) }
|
32
|
-
subject { Mappy.map(journal, target: 'mappy/mock_valid_model') }
|
33
38
|
|
34
|
-
|
35
|
-
|
39
|
+
context 'with a full source to target map' do
|
40
|
+
subject { Mappy.map(journal, target: 'mappy/mock_valid_model') }
|
41
|
+
|
42
|
+
its(:work_type) { should eq('long-journal')}
|
43
|
+
its(:title) { should eq(title) }
|
44
|
+
end
|
36
45
|
|
37
46
|
end
|
38
47
|
end
|
data/spec/lib/model_support.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Friesen
|
@@ -95,11 +95,14 @@ files:
|
|
95
95
|
- Rakefile
|
96
96
|
- lib/mappy.rb
|
97
97
|
- lib/mappy/configuration.rb
|
98
|
+
- lib/mappy/exceptions.rb
|
99
|
+
- lib/mappy/map_store.rb
|
98
100
|
- lib/mappy/resolver.rb
|
99
101
|
- lib/mappy/target_builder_factory.rb
|
100
102
|
- lib/mappy/version.rb
|
101
103
|
- mappy.gemspec
|
102
104
|
- spec/lib/mappy/configuration_spec.rb
|
105
|
+
- spec/lib/mappy/map_store_spec.rb
|
103
106
|
- spec/lib/mappy/resolver_spec.rb
|
104
107
|
- spec/lib/mappy/target_builder_factory_spec.rb
|
105
108
|
- spec/lib/mappy_spec.rb
|
@@ -130,6 +133,7 @@ specification_version: 4
|
|
130
133
|
summary: Map one object to another
|
131
134
|
test_files:
|
132
135
|
- spec/lib/mappy/configuration_spec.rb
|
136
|
+
- spec/lib/mappy/map_store_spec.rb
|
133
137
|
- spec/lib/mappy/resolver_spec.rb
|
134
138
|
- spec/lib/mappy/target_builder_factory_spec.rb
|
135
139
|
- spec/lib/mappy_spec.rb
|