dao-gateway 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -2
- data/lib/dao/gateway.rb +4 -0
- data/lib/dao/gateway/block_processor.rb +26 -0
- data/lib/dao/gateway/entity_processor.rb +15 -0
- data/lib/dao/gateway/pipe.rb +71 -0
- data/lib/dao/gateway/processor.rb +13 -0
- data/lib/dao/gateway/scope_transformer.rb +9 -9
- data/lib/dao/gateway/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2561db58543af87984ac12f2de82a5925351be2c
|
4
|
+
data.tar.gz: 1f9d890df5bb78085aeafe7be5f6747657083b9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fe09fbf6b7a737bd59918b5f6977ab69e98bdd82c0682bf86fd452996865b1f6a160631e8e132ad6a7bab4638aed41f3376f887ebabe48787443b3abca73301
|
7
|
+
data.tar.gz: ba6430ab5845b86a0fe10f64fa079071e56c701be6f4a1480a2eb7894d25cc3ff05869c53576767d954d8e988f4bca3fad95ad8f4875a9444b51dd61f12e250a
|
data/README.md
CHANGED
@@ -23,7 +23,6 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
$ gem install dao-gateway
|
25
25
|
|
26
|
-
|
27
26
|
## Development
|
28
27
|
|
29
28
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -38,4 +37,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/dao-rb
|
|
38
37
|
## License
|
39
38
|
|
40
39
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
-
|
data/lib/dao/gateway.rb
CHANGED
@@ -3,6 +3,10 @@ require 'dao/gateway/version'
|
|
3
3
|
require 'dao/gateway/record_not_found'
|
4
4
|
require 'dao/gateway/transaction_not_supported'
|
5
5
|
require 'dao/gateway/invalid_record'
|
6
|
+
require 'dao/gateway/processor'
|
7
|
+
require 'dao/gateway/entity_processor'
|
8
|
+
require 'dao/gateway/block_processor'
|
9
|
+
require 'dao/gateway/pipe'
|
6
10
|
|
7
11
|
require 'dao/gateway/scope_transformer'
|
8
12
|
require 'dao/gateway/base'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Dao
|
2
|
+
module Gateway
|
3
|
+
class BlockProcessor < Processor
|
4
|
+
def initialize(continuable = true, &block)
|
5
|
+
@continuable = continuable
|
6
|
+
@processor = block
|
7
|
+
end
|
8
|
+
|
9
|
+
def process(*args)
|
10
|
+
@processor.call(*args, self)
|
11
|
+
end
|
12
|
+
|
13
|
+
def continuable?
|
14
|
+
@continuable
|
15
|
+
end
|
16
|
+
|
17
|
+
def continuable!
|
18
|
+
@continuable = true
|
19
|
+
end
|
20
|
+
|
21
|
+
def stop!
|
22
|
+
@continuable = false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Dao
|
2
|
+
module Gateway
|
3
|
+
class EntityProcessor < Processor
|
4
|
+
def initialize(entity_class)
|
5
|
+
@entity_class = entity_class
|
6
|
+
end
|
7
|
+
|
8
|
+
def process(attributes, associations, *)
|
9
|
+
@entity_class.new(attributes).tap do |entity|
|
10
|
+
entity.initialized_with = associations
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Dao
|
2
|
+
module Gateway
|
3
|
+
class Pipe
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
def initialize(data, processors, associations)
|
7
|
+
@data = data
|
8
|
+
@processors = processors
|
9
|
+
@associations = associations
|
10
|
+
@data_processed = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def postprocess(processor)
|
14
|
+
@processors << processor
|
15
|
+
end
|
16
|
+
|
17
|
+
def preprocess(processor)
|
18
|
+
insert_processor_at(0, processor)
|
19
|
+
end
|
20
|
+
|
21
|
+
def insert_processor_at(index, processor)
|
22
|
+
@processors.insert(index, processor)
|
23
|
+
end
|
24
|
+
|
25
|
+
def preprocess_before(processor_type, processor)
|
26
|
+
index = @processors.index { |el| el.instance_of? processor_type }
|
27
|
+
insert_processor_at(index, processor)
|
28
|
+
end
|
29
|
+
|
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)
|
54
|
+
@data_processed = true
|
55
|
+
|
56
|
+
@data.each_with_index do |raw_element, index|
|
57
|
+
entity = raw_element
|
58
|
+
|
59
|
+
@processors.all? do |processor|
|
60
|
+
entity = processor.process(entity, @associations, raw_element)
|
61
|
+
processor.continuable?
|
62
|
+
end
|
63
|
+
|
64
|
+
@data[index] = entity
|
65
|
+
|
66
|
+
block.call(entity)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -1,12 +1,13 @@
|
|
1
1
|
module Dao
|
2
2
|
module Gateway
|
3
3
|
class ScopeTransformer
|
4
|
-
attr_reader :entity
|
5
4
|
attr_accessor :associations
|
6
5
|
|
7
6
|
def initialize(entity)
|
8
|
-
@entity = entity
|
9
7
|
@associations = []
|
8
|
+
@processors = [Dao::Gateway::EntityProcessor.new(entity)]
|
9
|
+
|
10
|
+
add_processors
|
10
11
|
end
|
11
12
|
|
12
13
|
def many(relation)
|
@@ -27,13 +28,12 @@ module Dao
|
|
27
28
|
|
28
29
|
protected
|
29
30
|
|
30
|
-
def transform(relation
|
31
|
-
relation
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
31
|
+
def transform(relation)
|
32
|
+
Pipe.new(relation, @processors, @associations)
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_processors
|
36
|
+
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
data/lib/dao/gateway/version.rb
CHANGED
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.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- llxff
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-07-
|
12
|
+
date: 2016-07-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -100,7 +100,11 @@ files:
|
|
100
100
|
- dao-gateway.gemspec
|
101
101
|
- lib/dao/gateway.rb
|
102
102
|
- lib/dao/gateway/base.rb
|
103
|
+
- lib/dao/gateway/block_processor.rb
|
104
|
+
- lib/dao/gateway/entity_processor.rb
|
103
105
|
- lib/dao/gateway/invalid_record.rb
|
106
|
+
- lib/dao/gateway/pipe.rb
|
107
|
+
- lib/dao/gateway/processor.rb
|
104
108
|
- lib/dao/gateway/record_not_found.rb
|
105
109
|
- lib/dao/gateway/scope_transformer.rb
|
106
110
|
- lib/dao/gateway/transaction_not_supported.rb
|