flow_object 0.2.0 → 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: 0c4e0bf60b9bd1664f1e627b21f7ab1f9054abba
4
- data.tar.gz: e8e9520592e1e9cc689c15be26d744580c774396
3
+ metadata.gz: b6e87c3678c7dfc6a5b2293cdde82114f9f5630d
4
+ data.tar.gz: c1cd29c871342cc8a434cf60026fabd2a5d23394
5
5
  SHA512:
6
- metadata.gz: 8edbfe815c46426494dfb7343f9f9d553e4f5889e79af5d7f09b2cd7202677cef7106c2b1ea6c5eb4892b3be24b6c6422465080c61db58b59f695a5511a1d054
7
- data.tar.gz: b57d9c98ce427889d725c8297eadde4c3c190512ed9a9c54b8e2bf0ba224eafb785373cbedb0de56ae62729c4b40006323abad8ac042658d11c455c9373f95b0
6
+ metadata.gz: bd56f181a60349d8dbfa029d5da3d6e2fd9746fbf1bbbb05b70745082af186ea2985c9928a659f9d9346ee8515f9b1e437bcd513c2845650da6b6c94d62cc2f0
7
+ data.tar.gz: bfa56fa807813ffe8dce4c3c8fd3d8e2d90587a9e14355fd9ea3d2b7a619fe16039145a87501748b96273145cff6f2186b4ae1fe04866406f489df5f9f7f5ef5
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- flow_object (0.2.0)
5
- mature_factory (~> 0.2.13)
4
+ flow_object (0.2.1)
5
+ mature_factory (~> 0.2.14)
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.13)
13
+ mature_factory (0.2.14)
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.13'
39
+ spec.add_dependency 'mature_factory', '~> 0.2.14'
40
40
 
41
41
  spec.add_development_dependency 'bundler', '~> 1.17'
42
42
  spec.add_development_dependency 'pry'
data/lib/flow_object.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'mature_factory'
2
+ require 'flow_object/callbacks'
3
+ require 'flow_object/runner'
2
4
  require 'flow_object/base'
3
5
  require 'flow_object/version'
4
6
 
@@ -3,18 +3,21 @@ module FlowObject
3
3
  include MatureFactory
4
4
  include MatureFactory::Features::Assemble
5
5
 
6
- composed_of :stages, :inputs, :outputs
6
+ produces :stages, :inputs, :outputs
7
7
 
8
8
  class << self
9
+ extend Forwardable
9
10
  attr_reader :in, :out, :initial_values
11
+ def_delegators :callbacks, :after_input_initialize, :after_flow_initialize,
12
+ :after_input_check, :after_flow_check, :after_output_initialize
10
13
  end
11
14
 
12
- attr_reader :output, :given
15
+ attr_reader :output
13
16
 
14
- def initialize(given)
15
- @given = given
17
+ def initialize(flow, step_name = :flow)
16
18
  @output = self.class.send(:__fo_wrap_output__)
17
- self.class.after_output_initialize.call(@output) if self.class.after_output_initialize
19
+ self.class.link_with_delegation(@output, flow, step_name, true)
20
+ self.class.after_output_initialize.call(@output)
18
21
  end
19
22
 
20
23
  def on_failure(failed_step = nil)
@@ -37,6 +40,10 @@ module FlowObject
37
40
  subclass.after_output_initialize(&self.after_output_initialize)
38
41
  end
39
42
 
43
+ def callbacks
44
+ @callbacks ||= Callbacks.new
45
+ end
46
+
40
47
  def from(input)
41
48
  @in = input
42
49
  self
@@ -53,31 +60,16 @@ module FlowObject
53
60
  end
54
61
 
55
62
  def call(flow: :main)
56
- __fo_resolve_cascade__(*__fo_process__(flow: flow))
63
+ __fo_resolve__(__fo_process__(flow: flow))
57
64
  end
58
65
 
59
66
  def flow(name = :main, &block)
60
67
  wrap(name, delegate: true, &block)
61
68
  end
62
69
 
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
70
+ def link_with_delegation(target, object, accessor, delegate)
71
+ MatureFactory::Features::Assemble::AbstractBuilder
72
+ .link_with_delegation(target, object, accessor, delegate)
81
73
  end
82
74
 
83
75
  private
@@ -87,36 +79,14 @@ module FlowObject
87
79
  end
88
80
 
89
81
  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]
82
+ plan = __fo_build_flow__(flow, self.in, :input, __fo_wrap_input__)
83
+ Runner.new(plan, callbacks, method(:halt_flow?)).execute_plan
99
84
  end
100
85
 
101
86
  def __fo_build_flow__(flow, step_name, group, object)
102
87
  public_send(:"build_#{flow}", title: step_name, group: group, object: object)
103
88
  end
104
89
 
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
117
- end
118
- end
119
-
120
90
  def __fo_wrap_input__
121
91
  return initial_values if self.in.nil?
122
92
  input_class = public_send(:"#{self.in}_input_class")
@@ -134,33 +104,33 @@ module FlowObject
134
104
  public_send(:"new_#{self.out}_output_instance")
135
105
  end
136
106
 
137
- def __fo_resolve_cascade__(cascade, step, failure)
138
- new(cascade).tap do |flow|
139
- if failure
140
- __fo_notify_error__(flow, step)
107
+ def __fo_resolve__(runner)
108
+ new(runner.flow, runner.step_name).tap do |handler|
109
+ if runner.failure
110
+ __fo_notify_error__(handler, runner.step_name)
141
111
  else
142
- __fo_notify_success__(flow)
112
+ __fo_notify_success__(handler)
143
113
  end
144
114
  end
145
115
  end
146
116
 
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")
117
+ def __fo_notify_error__(handler, step)
118
+ if handler.output.respond_to?(:"on_#{step}_failure")
119
+ handler.output.public_send(:"on_#{step}_failure")
120
+ elsif handler.output.respond_to?(:on_failure)
121
+ handler.output.on_failure(step)
122
+ elsif handler.respond_to?(:"on_#{step}_failure")
123
+ handler.public_send(:"on_#{step}_failure")
154
124
  else
155
- flow.on_failure(step)
125
+ handler.on_failure(step)
156
126
  end
157
127
  end
158
128
 
159
- def __fo_notify_success__(flow)
160
- if flow.output.respond_to?(:on_success)
161
- flow.output.on_success(flow.given)
129
+ def __fo_notify_success__(handler)
130
+ if handler.output.respond_to?(:on_success)
131
+ handler.output.on_success
162
132
  else
163
- flow.on_success
133
+ handler.on_success
164
134
  end
165
135
  end
166
136
  end
@@ -0,0 +1,29 @@
1
+ module FlowObject
2
+ class Callbacks
3
+ SUPPORTED = %i[
4
+ after_input_initialize
5
+ after_flow_initialize
6
+ after_input_check
7
+ after_flow_check
8
+ after_output_initialize
9
+ ].freeze
10
+
11
+ NOOP = proc {}.freeze
12
+
13
+ def initialize
14
+ @callbacks = Hash.new { |hash, key| hash[key] = NOOP }
15
+ end
16
+
17
+ def method_missing(name, *_attrs, &block)
18
+ if SUPPORTED.include?(name)
19
+ block_given? ? @callbacks[name] = block : @callbacks[name]
20
+ else
21
+ super
22
+ end
23
+ end
24
+
25
+ def respond_to_missing?(name, *)
26
+ SUPPORTED.include?(name)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,67 @@
1
+ module FlowObject
2
+ class Runner
3
+ attr_reader :step_name, :failure, :flow
4
+
5
+ def initialize(plan, callbacks, halt_if_proc)
6
+ @plan = plan
7
+ @flow = flow
8
+ @failure = false
9
+ @step_name = nil
10
+ @callbacks = callbacks
11
+ @step_index = 0
12
+ @halt_if_proc = halt_if_proc
13
+ end
14
+
15
+ def execute_plan
16
+ @flow = call_flow_builder
17
+ after_flow_check(flow.public_send(@step_name))
18
+ self
19
+ end
20
+
21
+ private
22
+
23
+ def call_flow_builder
24
+ @plan.call do |object, id|
25
+ handle_step(object, id) { throw :halt }
26
+ end
27
+ end
28
+
29
+ def after_flow_check(object)
30
+ @callbacks.after_flow_check.call(object)
31
+ end
32
+
33
+ def after_input_initialize(object)
34
+ @callbacks.after_input_initialize.call(object)
35
+ end
36
+
37
+ def after_flow_initialize(object)
38
+ @callbacks.after_flow_initialize.call(object)
39
+ end
40
+
41
+ def after_input_check(object)
42
+ @callbacks.after_input_check.call(object)
43
+ end
44
+
45
+ def second_step?
46
+ @step_index == 1
47
+ end
48
+
49
+ def input_step?(id)
50
+ id.group == :input
51
+ end
52
+
53
+ def flow_step?(id)
54
+ id.group == :stage
55
+ end
56
+
57
+ def handle_step(object, id)
58
+ after_input_initialize(object) if input_step?(id)
59
+ after_flow_initialize(object) if second_step? && flow_step?(id)
60
+ @step_name = id.title
61
+ @step_index += 1
62
+ yield if @failure = @halt_if_proc.call(object, id)
63
+ after_input_check(object) if input_step?(id)
64
+ end
65
+
66
+ end
67
+ end
@@ -1,3 +1,3 @@
1
1
  module FlowObject
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.2.1'.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.2.0
4
+ version: 0.2.1
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-03-05 00:00:00.000000000 Z
11
+ date: 2021-03-18 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.13
19
+ version: 0.2.14
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.13
26
+ version: 0.2.14
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +116,8 @@ files:
116
116
  - flow_object.gemspec
117
117
  - lib/flow_object.rb
118
118
  - lib/flow_object/base.rb
119
+ - lib/flow_object/callbacks.rb
120
+ - lib/flow_object/runner.rb
119
121
  - lib/flow_object/version.rb
120
122
  homepage: https://github.com/andriy-baran/flow_object
121
123
  licenses: