flow_object 0.1.1 → 0.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: de4cd7409a15ea3447cf1427523fd3f8221c301d
4
- data.tar.gz: bff8cba338ee8d0c5bfb7a3d83f853ea72e523ec
3
+ metadata.gz: 0c4e0bf60b9bd1664f1e627b21f7ab1f9054abba
4
+ data.tar.gz: e8e9520592e1e9cc689c15be26d744580c774396
5
5
  SHA512:
6
- metadata.gz: 663072aed50253c0a99888f64572ba157509dc1fa52df45533e1fa9111ad85a5c684321d1dbb00e5ffbf779eb9aff88de868d76f6c3e08519a0787594e7e91ed
7
- data.tar.gz: fa40fafa6f7be856feadc55d470159b6bde8c10df534cb1ed7e3cb4258a4a0c83902a370b0351ffe616dfeb1aa2ed91bba8c90fa800edfe6f60edadbcef7507b
6
+ metadata.gz: 8edbfe815c46426494dfb7343f9f9d553e4f5889e79af5d7f09b2cd7202677cef7106c2b1ea6c5eb4892b3be24b6c6422465080c61db58b59f695a5511a1d054
7
+ data.tar.gz: b57d9c98ce427889d725c8297eadde4c3c190512ed9a9c54b8e2bf0ba224eafb785373cbedb0de56ae62729c4b40006323abad8ac042658d11c455c9373f95b0
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- flow_object (0.1.1)
5
- mature_factory (~> 0.2.10)
4
+ flow_object (0.2.0)
5
+ mature_factory (~> 0.2.13)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -10,7 +10,7 @@ GEM
10
10
  coderay (1.1.3)
11
11
  diff-lcs (1.4.4)
12
12
  dry-inflector (0.1.2)
13
- mature_factory (0.2.10)
13
+ mature_factory (0.2.13)
14
14
  dry-inflector (~> 0.1)
15
15
  simple_facade (~> 0.2)
16
16
  method_source (1.0.0)
data/flow_object.gemspec CHANGED
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
37
  spec.require_paths = ['lib']
38
38
 
39
- spec.add_dependency 'mature_factory', '~> 0.2.10'
39
+ spec.add_dependency 'mature_factory', '~> 0.2.13'
40
40
 
41
41
  spec.add_development_dependency 'bundler', '~> 1.17'
42
42
  spec.add_development_dependency 'pry'
@@ -6,106 +6,165 @@ module FlowObject
6
6
  composed_of :stages, :inputs, :outputs
7
7
 
8
8
  class << self
9
- attr_accessor :in, :out, :initial_values
9
+ attr_reader :in, :out, :initial_values
10
10
  end
11
11
 
12
12
  attr_reader :output, :given
13
13
 
14
14
  def initialize(given)
15
15
  @given = given
16
- @output = self.class.__fo_wrap_output__
16
+ @output = self.class.send(:__fo_wrap_output__)
17
+ self.class.after_output_initialize.call(@output) if self.class.after_output_initialize
17
18
  end
18
19
 
19
- def inherited(subclass)
20
- subclass.from(self.in)
21
- subclass.to(self.out)
20
+ def on_failure(failed_step = nil)
21
+ # NOOP
22
22
  end
23
23
 
24
- def self.from(input)
25
- self.in = input
26
- self
24
+ def on_success
25
+ # NOOP
27
26
  end
28
27
 
29
- def self.to(output)
30
- self.out = output
31
- self
32
- end
28
+ module ClassMethods
29
+ def inherited(subclass)
30
+ super
31
+ subclass.from(self.in)
32
+ subclass.to(self.out)
33
+ subclass.after_input_initialize(&self.after_input_initialize)
34
+ subclass.after_flow_initialize(&self.after_flow_initialize)
35
+ subclass.after_input_check(&self.after_input_check)
36
+ subclass.after_flow_check(&self.after_flow_check)
37
+ subclass.after_output_initialize(&self.after_output_initialize)
38
+ end
33
39
 
34
- def self.__fo_wrap_input__
35
- return initial_values if self.in.nil?
36
- input_class = public_send(:"#{self.in}_input_class")
37
- return initial_values if input_class.nil?
38
- public_send(:"new_#{self.in}_input_instance", *initial_values)
39
- end
40
+ def from(input)
41
+ @in = input
42
+ self
43
+ end
40
44
 
41
- def self.__fo_wrap_output__
42
- # rescue NoMethodError => ex
43
- # "You have not define output class. Please add `output :#{self.out}`"
44
- return if self.out.nil?
45
- return unless self.respond_to?(:"#{self.out}_output_class")
46
- output_class = public_send(:"#{self.out}_output_class")
47
- return if output_class.nil?
48
- public_send(:"new_#{self.out}_output_instance")
49
- end
45
+ def to(output)
46
+ @out = output
47
+ self
48
+ end
50
49
 
51
- def self.accept(*values)
52
- self.initial_values = values
53
- self
54
- end
50
+ def accept(*values)
51
+ @initial_values = values
52
+ self
53
+ end
55
54
 
56
- def self.__fo_resolve_cascade__(cascade, step, failure)
57
- new(cascade).tap do |flow|
58
- if failure
59
- __fo_notify_error__(flow, step)
60
- else
61
- __fo_notify_success__(flow)
55
+ def call(flow: :main)
56
+ __fo_resolve_cascade__(*__fo_process__(flow: flow))
57
+ end
58
+
59
+ def flow(name = :main, &block)
60
+ wrap(name, delegate: true, &block)
61
+ end
62
+
63
+ def after_input_initialize(&block)
64
+ block_given? ? @after_input_initialize = block : @after_input_initialize
65
+ end
66
+
67
+ def after_flow_initialize(&block)
68
+ block_given? ? @after_flow_initialize = block : @after_flow_initialize
69
+ end
70
+
71
+ def after_input_check(&block)
72
+ block_given? ? @after_input_check = block : @after_input_check
73
+ end
74
+
75
+ def after_flow_check(&block)
76
+ block_given? ? @after_flow_check = block : @after_flow_check
77
+ end
78
+
79
+ def after_output_initialize(&block)
80
+ block_given? ? @after_output_initialize = block : @after_output_initialize
81
+ end
82
+
83
+ private
84
+
85
+ def halt_flow?(object, id)
86
+ false
87
+ end
88
+
89
+ def __fo_process__(flow: :main)
90
+ failure, step_name, group = false, self.in, :input
91
+ plan = __fo_build_flow__(flow, step_name, group, __fo_wrap_input__)
92
+ cascade = __fo_run_flow__(plan,
93
+ proc{|_,id| step_name = id.title},
94
+ proc{ failure = true },
95
+ after_input_initialize,
96
+ after_flow_initialize)
97
+ after_flow_check.call(cascade.public_send(step_name)) if after_flow_check
98
+ [cascade, step_name, failure]
99
+ end
100
+
101
+ def __fo_build_flow__(flow, step_name, group, object)
102
+ public_send(:"build_#{flow}", title: step_name, group: group, object: object)
103
+ end
104
+
105
+ def __fo_run_flow__(plan, on_step, on_failure, on_input, on_flow)
106
+ step_index = 0
107
+ plan.call do |object, id|
108
+ on_input.call(object) if on_input && id.group == :input
109
+ on_flow.call(object) if step_index == 1 && on_flow && id.group == :stage
110
+ on_step.call(object, id)
111
+ step_index += 1
112
+ if halt_flow?(object, id)
113
+ on_failure.call(object, id)
114
+ throw :halt
115
+ end
116
+ after_input_check.call(object) if after_input_check && id.group == :input
62
117
  end
63
118
  end
64
- end
65
119
 
66
- def self.__fo_notify_error__(flow, step)
67
- if flow.output.respond_to?(:"on_#{step}_failure")
68
- flow.output.public_send(:"on_#{step}_failure", flow.given)
69
- elsif flow.output.respond_to?(:on_failure)
70
- flow.output.on_failure(flow.given, step)
71
- elsif flow.respond_to?(:"on_#{step}_failure")
72
- flow.public_send(:"on_#{step}_failure")
73
- else
74
- flow.on_failure(step)
120
+ def __fo_wrap_input__
121
+ return initial_values if self.in.nil?
122
+ input_class = public_send(:"#{self.in}_input_class")
123
+ return initial_values if input_class.nil?
124
+ public_send(:"new_#{self.in}_input_instance", *initial_values)
75
125
  end
76
- end
77
126
 
78
- def self.__fo_notify_success__(flow)
79
- if flow.output.respond_to?(:on_success)
80
- flow.output.on_success(flow.given)
81
- else
82
- flow.on_success
127
+ def __fo_wrap_output__
128
+ # rescue NoMethodError => ex
129
+ # "You have not define output class. Please add `output :#{self.out}`"
130
+ return if self.out.nil?
131
+ return unless self.respond_to?(:"#{self.out}_output_class")
132
+ output_class = public_send(:"#{self.out}_output_class")
133
+ return if output_class.nil?
134
+ public_send(:"new_#{self.out}_output_instance")
83
135
  end
84
- end
85
136
 
86
- def self.call(flow: :main)
87
- failure, previous_step, object = false, self.in, __fo_wrap_input__
88
- plan = public_send(:"build_#{flow}", previous_step, object)
89
- cascade = plan.call do |object, id|
90
- previous_step = id.to_sym
91
- if !object.valid?
92
- failure = true
93
- throw :halt
94
- end
95
- end
96
- __fo_resolve_cascade__(cascade, previous_step, failure)
97
- end
137
+ def __fo_resolve_cascade__(cascade, step, failure)
138
+ new(cascade).tap do |flow|
139
+ if failure
140
+ __fo_notify_error__(flow, step)
141
+ else
142
+ __fo_notify_success__(flow)
143
+ end
144
+ end
145
+ end
98
146
 
99
- def self.flow(name = 'main', &block)
100
- wrap(name, delegate: true, &block)
101
- end
147
+ def __fo_notify_error__(flow, step)
148
+ if flow.output.respond_to?(:"on_#{step}_failure")
149
+ flow.output.public_send(:"on_#{step}_failure", flow.given)
150
+ elsif flow.output.respond_to?(:on_failure)
151
+ flow.output.on_failure(flow.given, step)
152
+ elsif flow.respond_to?(:"on_#{step}_failure")
153
+ flow.public_send(:"on_#{step}_failure")
154
+ else
155
+ flow.on_failure(step)
156
+ end
157
+ end
102
158
 
103
- def on_failure(failed_step = nil)
104
- # NOOP
159
+ def __fo_notify_success__(flow)
160
+ if flow.output.respond_to?(:on_success)
161
+ flow.output.on_success(flow.given)
162
+ else
163
+ flow.on_success
164
+ end
165
+ end
105
166
  end
106
167
 
107
- def on_success
108
- # NOOP
109
- end
168
+ extend ClassMethods
110
169
  end
111
170
  end
@@ -1,3 +1,3 @@
1
1
  module FlowObject
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flow_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-08 00:00:00.000000000 Z
11
+ date: 2021-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mature_factory
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.2.10
19
+ version: 0.2.13
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.2.10
26
+ version: 0.2.13
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement