usecasing 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,15 +20,13 @@ module UseCase
20
20
  end
21
21
 
22
22
  def perform(ctx = { })
23
- any_ciclic, ciclic = CyclicFinder.new(self).cyclic?
24
- raise StandardError.new("cyclic detected: #{ciclic}") if any_ciclic
25
- execution_order = []
26
- build_execution_order(self, execution_order)
27
- tx(execution_order, ctx) do |usecase, context|
23
+ tx(ExecutionOrder.run(self), ctx) do |usecase, context|
28
24
  usecase.new(context).perform
29
25
  end
30
26
  end
31
27
 
28
+ private
29
+
32
30
  def tx(execution_order, context)
33
31
  ctx = Context.new(context)
34
32
  executed = []
@@ -48,35 +46,6 @@ module UseCase
48
46
  context
49
47
  end
50
48
 
51
- def build_execution_order(node, result)
52
- return result.push(node) if node.dependencies.empty?
53
-
54
- node.dependencies.each do |item|
55
- build_execution_order(item, result)
56
- end
57
-
58
- result.push(node)
59
- end
60
-
61
- # def build_execution_order
62
- # stack = [self]
63
- # result = []
64
- # visited = {}
65
-
66
- # until stack.empty?
67
- # node = stack.last
68
- # if(node.dependencies.empty? || visited[node.name])
69
- # result.push(stack.pop)
70
- # else
71
- # stack.push(*(node.dependencies.reverse))
72
- # visited[node.name] = true
73
- # end
74
- # end
75
-
76
- # return result
77
-
78
- # end
79
-
80
49
  end #ClassMethods
81
50
  end #BaseClassMethod
82
51
 
@@ -0,0 +1,23 @@
1
+ module UseCase
2
+
3
+ class ExecutionOrder
4
+
5
+ def self.run(start_node)
6
+ any_ciclic, ciclic = CyclicFinder.new(start_node).cyclic?
7
+ raise StandardError.new("cyclic detected: #{ciclic}") if any_ciclic
8
+ post_order(start_node, [])
9
+ end
10
+
11
+ private
12
+ def self.post_order(node, result)
13
+ return result.push(node) if node.dependencies.empty?
14
+
15
+ node.dependencies.each do |item|
16
+ post_order(item, result)
17
+ end
18
+
19
+ result.push(node)
20
+ end
21
+
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Usecasing
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/usecasing.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require "usecasing/version"
2
2
 
3
3
  module UseCase
4
- autoload :Context, 'usecasing/context'
5
- autoload :Base, 'usecasing/base'
6
- autoload :CyclicFinder, 'usecasing/cyclic_finder'
4
+ autoload :Context, 'usecasing/context'
5
+ autoload :Base, 'usecasing/base'
6
+ autoload :CyclicFinder, 'usecasing/cyclic_finder'
7
+ autoload :ExecutionOrder, 'usecasing/execution_order'
7
8
  end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe UseCase::ExecutionOrder do
4
+
5
+ context "with only one node" do
6
+ it 'returns post order' do
7
+ EOFirst = Class.new(UseCase::Base)
8
+ expect(UseCase::ExecutionOrder.run(EOFirst)).to eql([EOFirst])
9
+ end
10
+ end
11
+
12
+ context "with two nodes" do
13
+ it 'returns the dependency first' do
14
+ EODepdency = Class.new(UseCase::Base)
15
+ EODependent = Class.new(UseCase::Base) do
16
+ depends EODepdency
17
+ end
18
+
19
+ expect(UseCase::ExecutionOrder.run(EODependent)).to eql([EODepdency, EODependent])
20
+
21
+ end
22
+ end
23
+
24
+ context 'with repeated nodes' do
25
+ it 'returns duplicated nodes' do
26
+ EORepeatedSMS = Class.new(UseCase::Base)
27
+
28
+ EOAlert = Class.new(UseCase::Base) do
29
+ depends EORepeatedSMS
30
+ end
31
+
32
+ EOCreate = Class.new(UseCase::Base) do
33
+ depends EOAlert, EORepeatedSMS
34
+ end
35
+
36
+ expect(UseCase::ExecutionOrder.run(EOCreate)).to eql([EORepeatedSMS, EOAlert, EORepeatedSMS, EOCreate])
37
+ end
38
+ end
39
+
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usecasing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -77,8 +77,10 @@ files:
77
77
  - lib/usecasing/base.rb
78
78
  - lib/usecasing/context.rb
79
79
  - lib/usecasing/cyclic_finder.rb
80
+ - lib/usecasing/execution_order.rb
80
81
  - lib/usecasing/version.rb
81
82
  - spec/context_spec.rb
83
+ - spec/execution_order_spec.rb
82
84
  - spec/spec_helper.rb
83
85
  - spec/usecase_base_spec.rb
84
86
  - usecasing.gemspec
@@ -96,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
98
  version: '0'
97
99
  segments:
98
100
  - 0
99
- hash: 1663280317763873983
101
+ hash: 183179680279514934
100
102
  required_rubygems_version: !ruby/object:Gem::Requirement
101
103
  none: false
102
104
  requirements:
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
107
  version: '0'
106
108
  segments:
107
109
  - 0
108
- hash: 1663280317763873983
110
+ hash: 183179680279514934
109
111
  requirements: []
110
112
  rubyforge_project:
111
113
  rubygems_version: 1.8.24
@@ -114,5 +116,6 @@ specification_version: 3
114
116
  summary: UseCase Driven Approach
115
117
  test_files:
116
118
  - spec/context_spec.rb
119
+ - spec/execution_order_spec.rb
117
120
  - spec/spec_helper.rb
118
121
  - spec/usecase_base_spec.rb