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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81a2df0e93a30d9dc7301a2dc43b437e71288edc
4
- data.tar.gz: 68ae080d16ea598b40dc21cb77f9d35b7990ca92
3
+ metadata.gz: 67e684a0fd493690919a03ee753f5e028805d822
4
+ data.tar.gz: 19d582b225116a7d35e859f060caaeb2d5d06efd
5
5
  SHA512:
6
- metadata.gz: 09b819564c909b535c0358f18ecfc25493c10b913f79c1cfc9d6421a98fe8f67841bee0de5f19fe677cc1bb5ed73988d16a4dd96461959207413b7b40d74287a
7
- data.tar.gz: 05dcd31687345b91f2963781ae8f79a257689bbb7289868fd31d316debb49284860958132eb0c47481ed569ece7e8f6b1bda9d701752632c28084b1bf9c81d7d
6
+ metadata.gz: 86d39e05a66b27082f4a6063f6ca57ac106da70bf7d945da71022498fb84ac18b1f6ef9fab87a93c453af80a9e29f8255d7eefa95ef803d73282e273ca6f364a
7
+ data.tar.gz: 17a04b501f516948821fb4f720e7e2837c65f7906ac6032a54b7c361dc1a61d1e64b167dcac1e4297f9afcb75a5e056627516159cb8d303de08ea0e2fc1afd19
@@ -10,6 +10,7 @@ module Mappy
10
10
 
11
11
  def to_type(object)
12
12
  return object.to_mappy_type if object.respond_to?(:to_mappy_type)
13
+ object.class.to_s.underscore
13
14
  end
14
15
 
15
16
  # Used for configuring available RemoteService and any additional
@@ -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
- source = options.fetch(:source)
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
- source = source_instance.to_mappy_type
26
- legend = map_store.fetch(target).fetch(source)
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,9 @@
1
+ module Mappy
2
+
3
+ class MapNotFound < RuntimeError
4
+ def initialize(map_store, keys)
5
+ super("#{keys.inspect} not found in #{map_store.inspect}")
6
+ end
7
+ end
8
+
9
+ 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
@@ -1,3 +1,3 @@
1
1
  module Mappy
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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.fetch(:document).fetch(:article)).to eq(legend)
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
@@ -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
- let(:mappy_type) { 'hello-world' }
7
- let(:object) { double(to_mappy_type: mappy_type)}
6
+
8
7
  it 'with explicit to mappy type' do
9
- expect(Mappy.to_type(object)).to eq(mappy_type)
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
- its(:work_type) { should eq('long-journal')}
35
- its(:title) { should eq(title) }
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
@@ -11,4 +11,6 @@ module Mappy
11
11
  class MockInvalidModel
12
12
  end
13
13
 
14
+ class WithoutExplicitMappyType
15
+ end
14
16
  end
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.3
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