carry_out 0.1.4 → 0.2.1

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: cda4d8a6853705150ef9a286b9cd50097897eb88
4
- data.tar.gz: 5cd6ae6c588da5c4cb43f08687c577040277194c
3
+ metadata.gz: 2527666eec64444e35bf101c6b084c6a4397a20b
4
+ data.tar.gz: da3bd9e8d2f51dbbb758d99565858f2cd9aeb5ce
5
5
  SHA512:
6
- metadata.gz: be7ecd2990dd1887f7127860256e28150017d323d3e1384461eea0d8c3e72cfcd8c43342018e889eff0ec15cca9b261db5f7f614b71721f83d380941b8839e7c
7
- data.tar.gz: bbbedcb6edd30a9ab968e6a8568f6cf876b6401f8de3b5e2a531da4eabdc711244cb1d3a55be3640e8b17edfc3553bf843f7dad33a9c9f760c879681043c019b
6
+ metadata.gz: 25154eb134c0f970e4f23fe896278b94a87a68fc5edb1e8836a87a37b94f9226fe76da2a8b39283b2853591969eae5f111912c93c7e30ff35d761bac50f91e9f
7
+ data.tar.gz: 8e088f4e96b57d1f7cc80548cdfe764c72d29f8c9ee63411492b09d8ab0407ba49697f7357d579f0115e5c6d92233bc9c510025f28499438742d1e8d64c79d98
@@ -3,8 +3,9 @@ module CarryOut
3
3
 
4
4
  def initialize(unit = nil, options = {})
5
5
  @nodes = {}
6
+ @node_meta = {}
6
7
  @previously_added_node = nil
7
- @initial_node_key = add_node(PlanNode.new)
8
+ @wrapper = options[:within]
8
9
 
9
10
  unless unit.nil?
10
11
  self.then(unit, options)
@@ -12,27 +13,25 @@ module CarryOut
12
13
  end
13
14
 
14
15
  def execute(&block)
15
- node = @nodes[@initial_node_key]
16
- label = node.next unless node.nil?
17
-
18
- Result.new.tap do |result|
19
- while node = @nodes[label] do
20
- begin
21
- node.execute(ResultManipulator.new(result, label), result.artifacts)
22
- rescue UnitError => error
23
- result.add(label, CarryOut::Error.new(error.error.message, error.error))
24
- break
16
+ if @wrapper
17
+ if @wrapper.respond_to?(:execute)
18
+ @wrapper.execute do |context|
19
+ execute_internal(Result.new(context), &block)
25
20
  end
26
-
27
- label = node.next
28
- end
29
-
30
- unless block.nil?
31
- block.call(result)
21
+ else
22
+ @wrapper.call(Proc.new do |context|
23
+ execute_internal(Result.new(context), &block)
24
+ end)
32
25
  end
26
+ else
27
+ execute_internal(&block)
33
28
  end
34
29
  end
35
30
 
31
+ def will(*args)
32
+ self.then(*args)
33
+ end
34
+
36
35
  def then(unit, options = {})
37
36
  add_node(PlanNode.new(unit), options[:as])
38
37
  self
@@ -53,22 +52,43 @@ module CarryOut
53
52
 
54
53
  private
55
54
  def add_node(node, as = nil)
56
- label = (as || generate_node_name)
57
-
58
- if as.nil?
59
- until @nodes[label].nil?
60
- label = generate_node_name
61
- end
62
- end
55
+ id = generate_node_name
63
56
 
64
- unless @previously_added_node.nil?
65
- @previously_added_node.next = label
57
+ if @previously_added_node.nil?
58
+ @initial_node_key = id
59
+ else
60
+ @previously_added_node.next = id
66
61
  end
67
62
 
68
- @nodes[label] = node
63
+ @nodes[id] = node
64
+ @node_meta[id] = { as: as }
69
65
  @previously_added_node = node
70
66
 
71
- label
67
+ id
68
+ end
69
+
70
+ def execute_internal(result = nil, &block)
71
+ id = @initial_node_key
72
+
73
+ (result || Result.new).tap do |result|
74
+ while node = @nodes[id] do
75
+ publish_to = @node_meta[id][:as]
76
+
77
+ begin
78
+ node_result = node.execute(result.artifacts)
79
+ result.add(publish_to, node_result) unless publish_to.nil?
80
+ rescue UnitError => error
81
+ result.add(publish_to || id, CarryOut::Error.new(error.error.message, error.error))
82
+ break
83
+ end
84
+
85
+ id = node.next
86
+ end
87
+
88
+ unless block.nil?
89
+ block.call(result)
90
+ end
91
+ end
72
92
  end
73
93
 
74
94
  def generate_node_name
@@ -27,16 +27,16 @@ module CarryOut
27
27
  @unitClass.instance_methods.include?(method) || super
28
28
  end
29
29
 
30
- def execute(result, artifacts)
31
- unit = @unitClass.new
30
+ def execute(context)
31
+ unit = @unitClass.respond_to?(:execute) ? @unitClass : @unitClass.new
32
32
 
33
33
  @messages.each do |message|
34
- arg = message[:block] ? message[:block].call(artifacts) : message[:argument]
34
+ arg = message[:block] ? message[:block].call(context) : message[:argument]
35
35
  unit.send(message[:method], arg)
36
36
  end
37
37
 
38
38
  begin
39
- unit.execute(result)
39
+ unit.execute
40
40
  rescue StandardError => error
41
41
  raise UnitError.new(error)
42
42
  end
@@ -1,14 +1,22 @@
1
1
  module CarryOut
2
2
  class Result
3
3
 
4
- def add(group, label, object = nil)
5
- if label.kind_of?(CarryOut::Error)
6
- add_error(group, label)
7
- elsif !object.nil?
8
- group = artifacts[group] ||= {}
9
- group[label] = object
4
+ def initialize(hash = nil)
5
+ @artifacts = Hash[hash] unless hash.nil?
6
+ end
7
+
8
+ def add(group, object)
9
+ if object.kind_of?(CarryOut::Error)
10
+ add_error(group, object)
11
+ elsif object.kind_of?(Result)
12
+ add(group, object.to_hash)
13
+ elsif object.kind_of?(Hash)
14
+ artifacts[group] ||= {}
15
+ object.each { |k,v| artifacts[group][k] = v }
16
+ elsif !artifacts[group].nil?
17
+ artifacts[group] = [ artifacts[group], object ].flatten
10
18
  else
11
- artifacts[group] = label
19
+ artifacts[group] = object
12
20
  end
13
21
  end
14
22
 
@@ -24,6 +32,10 @@ module CarryOut
24
32
  @errors.nil? || @errors.empty?
25
33
  end
26
34
 
35
+ def to_hash
36
+ artifacts
37
+ end
38
+
27
39
  private
28
40
  def add_error(group, error)
29
41
  group = errors[group] ||= []
@@ -1,7 +1,7 @@
1
1
  module CarryOut
2
2
  class Unit
3
3
 
4
- def execute(result)
4
+ def execute
5
5
  end
6
6
 
7
7
  def self.parameter(method_name, var = nil)
@@ -1,3 +1,3 @@
1
1
  module CarryOut
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/carry_out.rb CHANGED
@@ -4,7 +4,6 @@ require "carry_out/error"
4
4
  require "carry_out/plan"
5
5
  require "carry_out/plan_node"
6
6
  require "carry_out/result"
7
- require "carry_out/result_manipulator"
8
7
  require "carry_out/unit"
9
8
  require "carry_out/unit_error"
10
9
 
@@ -12,4 +11,8 @@ module CarryOut
12
11
  def self.will(*args)
13
12
  Plan.new(*args)
14
13
  end
14
+
15
+ def self.within(unit = nil, &block)
16
+ Plan.new(nil, within: unit || block)
17
+ end
15
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carry_out
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Fields
@@ -92,7 +92,6 @@ files:
92
92
  - lib/carry_out/plan.rb
93
93
  - lib/carry_out/plan_node.rb
94
94
  - lib/carry_out/result.rb
95
- - lib/carry_out/result_manipulator.rb
96
95
  - lib/carry_out/unit.rb
97
96
  - lib/carry_out/unit_error.rb
98
97
  - lib/carry_out/version.rb
@@ -1,13 +0,0 @@
1
- module CarryOut
2
- class ResultManipulator
3
-
4
- def initialize(result, group)
5
- @result = result
6
- @group = group
7
- end
8
-
9
- def add(label, object = nil)
10
- @result.add(@group, label, object) unless @group.nil?
11
- end
12
- end
13
- end