flow_object 0.1.0 → 0.1.5

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: 5c959322ed0e2f7b2fae488979d0617125f6f4e2
4
- data.tar.gz: c319dca9016f79597ad204c51778f18465bca0d3
3
+ metadata.gz: fcfa33b52598259445a736e3e46b7eedd2012bc5
4
+ data.tar.gz: 0f4b07df841d4e15bc50ecddd09e1ea15e652f3d
5
5
  SHA512:
6
- metadata.gz: b4816326386a3ef0e3ed97b6ab0d384ac1f4400834606a39e631a682667129470c239ddb5b85cb9b1a4105ed9c0d9eda96ef51ec9d3257831b76c5aa58a8ec7f
7
- data.tar.gz: 4fdcd56de216a16bf6824acacc094ad1d952def603dd630fb149016716af6d11a759253825770b6156208e5f15bbfe01b097fc1c4311b322b292e65818a5812a
6
+ metadata.gz: 84817e3b8c4f23f6958bdaf77cb197e37664e4c95b70df31713b012e4edb327d9407ab2856d76b61701829685ea45a7b3392653d99385d05fc47df3cadbd158e
7
+ data.tar.gz: bdbe490405595d43a22fd33b55a97f697c4ede5abc6777ffb044f68884ae1d702dd05c9118a1add981c31a0e161bb1758f6497215ce2832bbde275c3997a1d53
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- flow_object (0.1.0)
5
- mature_factory (~> 0.2.9)
4
+ flow_object (0.1.5)
5
+ mature_factory (~> 0.2.12)
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.9)
13
+ mature_factory (0.2.12)
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.9'
39
+ spec.add_dependency 'mature_factory', '~> 0.2.12'
40
40
 
41
41
  spec.add_development_dependency 'bundler', '~> 1.17'
42
42
  spec.add_development_dependency 'pry'
@@ -13,99 +13,124 @@ module FlowObject
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
17
  end
18
18
 
19
- def inherited(subclass)
20
- subclass.from(self.in)
21
- subclass.to(self.out)
19
+ def on_failure(failed_step = nil)
20
+ # NOOP
22
21
  end
23
22
 
24
- def self.from(input)
25
- self.in = input
26
- self
23
+ def on_success
24
+ # NOOP
27
25
  end
28
26
 
29
- def self.to(output)
30
- self.out = output
31
- self
32
- end
27
+ module ClassMethods
28
+ def inherited(subclass)
29
+ super
30
+ subclass.from(self.in)
31
+ subclass.to(self.out)
32
+ end
33
33
 
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
34
+ def from(input)
35
+ self.in = input
36
+ self
37
+ end
40
38
 
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
39
+ def to(output)
40
+ self.out = output
41
+ self
42
+ end
50
43
 
51
- def self.accept(*values)
52
- self.initial_values = values
53
- self
54
- end
44
+ def accept(*values)
45
+ self.initial_values = values
46
+ self
47
+ end
55
48
 
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)
49
+ def call(flow: :main)
50
+ __fo_resolve_cascade__(*__fo_process__(flow: flow))
51
+ end
52
+
53
+ def flow(name = :main, &block)
54
+ wrap(name, delegate: true, &block)
55
+ end
56
+
57
+ private
58
+
59
+ def halt_flow?(object, id)
60
+ !object.valid?
61
+ end
62
+
63
+ def __fo_process__(flow: :main)
64
+ failure, step_name = false, self.in
65
+ plan = __fo_build_flow__(flow, step_name, __fo_wrap_input__)
66
+ cascade = __fo_run_flow__(plan,
67
+ proc{|_,id| step_name = id.title},
68
+ proc{ failure = true })
69
+ [cascade, step_name, failure]
70
+ end
71
+
72
+ def __fo_build_flow__(flow, step_name, object)
73
+ public_send(:"build_#{flow}", step_name, object)
74
+ end
75
+
76
+ def __fo_run_flow__(plan, on_step, on_failure)
77
+ plan.call do |object, id|
78
+ on_step.call(object, id)
79
+ if halt_flow?(object, id)
80
+ on_failure.call(object, id)
81
+ throw :halt
82
+ end
62
83
  end
63
84
  end
64
- end
65
85
 
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)
86
+ def __fo_wrap_input__
87
+ return initial_values if self.in.nil?
88
+ input_class = public_send(:"#{self.in}_input_class")
89
+ return initial_values if input_class.nil?
90
+ public_send(:"new_#{self.in}_input_instance", *initial_values)
75
91
  end
76
- end
77
92
 
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
93
+ def __fo_wrap_output__
94
+ # rescue NoMethodError => ex
95
+ # "You have not define output class. Please add `output :#{self.out}`"
96
+ return if self.out.nil?
97
+ return unless self.respond_to?(:"#{self.out}_output_class")
98
+ output_class = public_send(:"#{self.out}_output_class")
99
+ return if output_class.nil?
100
+ public_send(:"new_#{self.out}_output_instance")
83
101
  end
84
- end
85
102
 
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
103
+ def __fo_resolve_cascade__(cascade, step, failure)
104
+ new(cascade).tap do |flow|
105
+ if failure
106
+ __fo_notify_error__(flow, step)
107
+ else
108
+ __fo_notify_success__(flow)
109
+ end
110
+ end
111
+ end
98
112
 
99
- def self.flow(name = 'main', &block)
100
- wrap(name, delegate: true, &block)
101
- end
113
+ def __fo_notify_error__(flow, step)
114
+ if flow.output.respond_to?(:"on_#{step}_failure")
115
+ flow.output.public_send(:"on_#{step}_failure", flow.given)
116
+ elsif flow.output.respond_to?(:on_failure)
117
+ flow.output.on_failure(flow.given, step)
118
+ elsif flow.respond_to?(:"on_#{step}_failure")
119
+ flow.public_send(:"on_#{step}_failure")
120
+ else
121
+ flow.on_failure(step)
122
+ end
123
+ end
102
124
 
103
- def on_failure(failed_step = nil)
104
- # NOOP
125
+ def __fo_notify_success__(flow)
126
+ if flow.output.respond_to?(:on_success)
127
+ flow.output.on_success(flow.given)
128
+ else
129
+ flow.on_success
130
+ end
131
+ end
105
132
  end
106
133
 
107
- def on_success
108
- # NOOP
109
- end
134
+ extend ClassMethods
110
135
  end
111
136
  end
@@ -1,3 +1,3 @@
1
1
  module FlowObject
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.5'.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.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-15 00:00:00.000000000 Z
11
+ date: 2021-03-04 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.9
19
+ version: 0.2.12
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.9
26
+ version: 0.2.12
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement