dao-gateway 1.1.2 → 1.2.0

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
  SHA1:
3
- metadata.gz: 6ad737c9638afa22a5e58a91b456f274d39c720e
4
- data.tar.gz: c043216767c08459cbcfcc1501b2f851c3972937
3
+ metadata.gz: dffe3a717ea65a32a26bdf3ffc8a99bb4cdb9f05
4
+ data.tar.gz: 0ec648dd5aa0392d1c4c489b61ec12fdddb9a98e
5
5
  SHA512:
6
- metadata.gz: d195160519b00647a4a9e7dd32c065b82bdfbbfdcada2038a242a31edb70752f6ef47d344639eaed8e03a649db04bd227ee6100b61f04cbd7be164b0f67c5c6e
7
- data.tar.gz: a06447aca4241e2e70e808973e791d32ebc98d90299afef0cc8fcf7e121efbb790b3dd75b588eb4df02de31984179f32cb8d33b816d2c89c86f468c7683f90a5
6
+ metadata.gz: 89dbb81bbb6aa39fc67fb714720e7a32692e8623bcb820667bd4cc9ef809d6f40d47418b4378938e58e1a5e41b58b1b4457fcd85a4a5edbbb544d744b4ef2217
7
+ data.tar.gz: bd1e958a84cecb7dc7e99fd9b9c0d19c43c7abac0be32cab2e16a3a2b28e7720a58e2cd0a90264b71ae3aa74cd7e1ce2dcfd12efc9c453c59413f57d9cbf920f
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in dao-gateway.gemspec
4
3
  gemspec
data/lib/dao/gateway.rb CHANGED
@@ -7,6 +7,7 @@ require 'dao/gateway/processor'
7
7
  require 'dao/gateway/entity_processor'
8
8
  require 'dao/gateway/block_processor'
9
9
  require 'dao/gateway/pipe'
10
+ require 'dao/gateway/iterator'
10
11
 
11
12
  require 'dao/gateway/scope_transformer'
12
13
  require 'dao/gateway/base'
@@ -44,8 +44,26 @@ module Dao
44
44
  fail 'export is not implemented'
45
45
  end
46
46
 
47
- def import(_relation, _associations)
48
- fail 'import is not implemented'
47
+ def import(relation, associations)
48
+ @transformer.associations = associations
49
+
50
+ unless relation.nil?
51
+ if source_scope?(relation)
52
+ @transformer.one(relation)
53
+ elsif collection_scope?(relation)
54
+ @transformer.many(relation)
55
+ else
56
+ @transformer.other(relation)
57
+ end
58
+ end
59
+ end
60
+
61
+ def collection_scope?(relation)
62
+ relation.respond_to? :each
63
+ end
64
+
65
+ def source_scope?(relation)
66
+ relation.is_a?(source)
49
67
  end
50
68
 
51
69
  def record(_domain_id)
@@ -1,25 +1,29 @@
1
1
  module Dao
2
2
  module Gateway
3
3
  class BlockProcessor < Processor
4
- def initialize(continuable = true, &block)
5
- @continuable = continuable
4
+ def initialize(need_to_continue_lookup = true, &block)
5
+ @original_need_to_continue_lookup = need_to_continue_lookup
6
6
  @processor = block
7
7
  end
8
8
 
9
- def process(*args)
10
- @processor.call(*args, self)
9
+ def prepared
10
+ @need_to_continue_lookup = @original_need_to_continue_lookup
11
11
  end
12
12
 
13
- def continuable?
14
- @continuable
13
+ def need_to_continue_lookup?
14
+ @need_to_continue_lookup
15
15
  end
16
16
 
17
- def continuable!
18
- @continuable = true
17
+ def process(entity)
18
+ @processor.call(entity, @associations, @raw_record, self)
19
+ end
20
+
21
+ def need_to_continue_lookup!
22
+ @need_to_continue_lookup = true
19
23
  end
20
24
 
21
25
  def stop!
22
- @continuable = false
26
+ @need_to_continue_lookup = false
23
27
  end
24
28
  end
25
29
  end
@@ -5,9 +5,9 @@ module Dao
5
5
  @entity_class = entity_class
6
6
  end
7
7
 
8
- def process(attributes, associations, *)
8
+ def process(attributes)
9
9
  @entity_class.new(attributes).tap do |entity|
10
- entity.initialized_with = associations
10
+ entity.initialized_with = @associations
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,54 @@
1
+ module Dao
2
+ module Gateway
3
+ class Iterator
4
+ include Enumerable
5
+
6
+ attr_reader :pipe
7
+
8
+ def initialize(data, pipe, associations)
9
+ @data = data
10
+ @pipe = pipe
11
+ @associations = associations
12
+ @data_processed = false
13
+ end
14
+
15
+ def processed?
16
+ @data_processed
17
+ end
18
+
19
+ def each(&block)
20
+ if processed?
21
+ @data.each(&block)
22
+ else
23
+ process_data(&block)
24
+ end
25
+ end
26
+
27
+ def fork
28
+ raise 'Data was already processed' if processed?
29
+ fork!
30
+ end
31
+
32
+ def fork!
33
+ self.class.new(@data.dup, pipe.dup, @associations)
34
+ end
35
+
36
+ private
37
+
38
+ def process_data(&block)
39
+ @data_processed = true
40
+ result = []
41
+
42
+ @data.each_with_index do |raw_element, index|
43
+ entity = pipe.process(raw_element, @associations)
44
+
45
+ result[index] = entity
46
+
47
+ block.call(entity)
48
+ end
49
+
50
+ @data = result
51
+ end
52
+ end
53
+ end
54
+ end
@@ -3,19 +3,16 @@ module Dao
3
3
  class Pipe
4
4
  include Enumerable
5
5
 
6
- def initialize(data, processors, associations)
7
- @data = data
8
- @processors = processors
9
- @associations = associations
10
- @data_processed = false
6
+ def initialize
7
+ @processors = []
11
8
  end
12
9
 
13
10
  def postprocess(processor)
14
- @processors << processor
11
+ @processors.unshift(processor)
15
12
  end
16
13
 
17
14
  def preprocess(processor)
18
- insert_processor_at(0, processor)
15
+ @processors << processor
19
16
  end
20
17
 
21
18
  def insert_processor_at(index, processor)
@@ -27,47 +24,22 @@ module Dao
27
24
  insert_processor_at(index, processor)
28
25
  end
29
26
 
30
- def processed?
31
- @data_processed
32
- end
33
-
34
- def each(&block)
35
- if processed?
36
- @data.each(&block)
37
- else
38
- process_data(&block)
39
- end
40
- end
41
-
42
- def fork
43
- raise 'Data was already processed' if processed?
44
- fork!
45
- end
46
-
47
- def fork!
48
- self.class.new(@data.dup, @processors.dup, @associations)
49
- end
50
-
51
- private
52
-
53
- def process_data(&block)
27
+ def process(raw_element, associations)
54
28
  @data_processed = true
55
- result = []
56
29
 
57
- @data.each_with_index do |raw_element, index|
58
- entity = raw_element
30
+ processors = []
59
31
 
60
- @processors.all? do |processor|
61
- entity = processor.process(entity, @associations, raw_element)
62
- processor.continuable?
63
- end
32
+ @processors.all? do |processor|
33
+ processor.prepare(associations, raw_element)
64
34
 
65
- result[index] = entity
35
+ processors.unshift(processor)
66
36
 
67
- block.call(entity)
37
+ processor.need_to_continue_lookup?
68
38
  end
69
39
 
70
- @data = result
40
+ processors.inject(raw_element) do |entity, processor|
41
+ processor.process(entity)
42
+ end
71
43
  end
72
44
  end
73
45
  end
@@ -1,13 +1,25 @@
1
1
  module Dao
2
2
  module Gateway
3
3
  class Processor
4
- def process(attributes, _associations, _raw_record)
4
+ def prepare(associations, raw_record)
5
+ @associations = associations
6
+ @raw_record = raw_record
7
+ prepared
8
+ end
9
+
10
+ def process(attributes)
5
11
  attributes
6
12
  end
7
13
 
8
- def continuable?
14
+ def need_to_continue_lookup?
9
15
  true
10
16
  end
17
+
18
+ protected
19
+
20
+ def prepared
21
+
22
+ end
11
23
  end
12
24
  end
13
25
  end
@@ -2,12 +2,15 @@ module Dao
2
2
  module Gateway
3
3
  class ScopeTransformer
4
4
  attr_reader :entity
5
+ attr_reader :pipe
5
6
  attr_accessor :associations
6
7
 
7
8
  def initialize(entity)
8
9
  @associations = []
9
10
  @entity = entity
10
- @processors = [Dao::Gateway::EntityProcessor.new(entity)]
11
+
12
+ @pipe = Pipe.new
13
+ @pipe.postprocess(Dao::Gateway::EntityProcessor.new(entity))
11
14
 
12
15
  add_processors
13
16
  end
@@ -17,7 +20,7 @@ module Dao
17
20
  end
18
21
 
19
22
  def one(relation)
20
- transform(relation).first
23
+ transform(Array(relation)).first
21
24
  end
22
25
 
23
26
  def other(relation)
@@ -31,7 +34,7 @@ module Dao
31
34
  protected
32
35
 
33
36
  def transform(relation)
34
- Pipe.new(relation, @processors, @associations)
37
+ Iterator.new(relation, pipe, @associations)
35
38
  end
36
39
 
37
40
  def add_processors
@@ -1,5 +1,5 @@
1
1
  module Dao
2
2
  module Gateway
3
- VERSION = '1.1.2'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dao-gateway
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - llxff
@@ -103,6 +103,7 @@ files:
103
103
  - lib/dao/gateway/block_processor.rb
104
104
  - lib/dao/gateway/entity_processor.rb
105
105
  - lib/dao/gateway/invalid_record.rb
106
+ - lib/dao/gateway/iterator.rb
106
107
  - lib/dao/gateway/pipe.rb
107
108
  - lib/dao/gateway/processor.rb
108
109
  - lib/dao/gateway/record_not_found.rb