flow_object 0.1.4 → 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: ffa8dd6903e47060958b6623ed8a8c6b96074130
4
- data.tar.gz: 8d7772615968378288dc52f32a83eeb6bfb14db2
3
+ metadata.gz: fcfa33b52598259445a736e3e46b7eedd2012bc5
4
+ data.tar.gz: 0f4b07df841d4e15bc50ecddd09e1ea15e652f3d
5
5
  SHA512:
6
- metadata.gz: 57215e81d52180ee8d28600be3ae0326834106e270e7c2fc00838fcd067ec5666ab21bc2696128a735e79d1a6ee1ea74dca46fa91de98d324b249bee8572d162
7
- data.tar.gz: fc71295067cbc45c298aea3fa0452f6e431ca02ed1ccbc5f370064b8fffc302fee14e66ae7d1b2a0fd63eaca9bd23aaa5dc351b2e93abf6122ae8e3850bc9600
6
+ metadata.gz: 84817e3b8c4f23f6958bdaf77cb197e37664e4c95b70df31713b012e4edb327d9407ab2856d76b61701829685ea45a7b3392653d99385d05fc47df3cadbd158e
7
+ data.tar.gz: bdbe490405595d43a22fd33b55a97f697c4ede5abc6777ffb044f68884ae1d702dd05c9118a1add981c31a0e161bb1758f6497215ce2832bbde275c3997a1d53
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- flow_object (0.1.4)
4
+ flow_object (0.1.5)
5
5
  mature_factory (~> 0.2.12)
6
6
 
7
7
  GEM
@@ -13,100 +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 self.inherited(subclass)
20
- super
21
- subclass.from(self.in)
22
- subclass.to(self.out)
19
+ def on_failure(failed_step = nil)
20
+ # NOOP
23
21
  end
24
22
 
25
- def self.from(input)
26
- self.in = input
27
- self
23
+ def on_success
24
+ # NOOP
28
25
  end
29
26
 
30
- def self.to(output)
31
- self.out = output
32
- self
33
- end
27
+ module ClassMethods
28
+ def inherited(subclass)
29
+ super
30
+ subclass.from(self.in)
31
+ subclass.to(self.out)
32
+ end
34
33
 
35
- def self.__fo_wrap_input__
36
- return initial_values if self.in.nil?
37
- input_class = public_send(:"#{self.in}_input_class")
38
- return initial_values if input_class.nil?
39
- public_send(:"new_#{self.in}_input_instance", *initial_values)
40
- end
34
+ def from(input)
35
+ self.in = input
36
+ self
37
+ end
41
38
 
42
- def self.__fo_wrap_output__
43
- # rescue NoMethodError => ex
44
- # "You have not define output class. Please add `output :#{self.out}`"
45
- return if self.out.nil?
46
- return unless self.respond_to?(:"#{self.out}_output_class")
47
- output_class = public_send(:"#{self.out}_output_class")
48
- return if output_class.nil?
49
- public_send(:"new_#{self.out}_output_instance")
50
- end
39
+ def to(output)
40
+ self.out = output
41
+ self
42
+ end
51
43
 
52
- def self.accept(*values)
53
- self.initial_values = values
54
- self
55
- end
44
+ def accept(*values)
45
+ self.initial_values = values
46
+ self
47
+ end
56
48
 
57
- def self.__fo_resolve_cascade__(cascade, step, failure)
58
- new(cascade).tap do |flow|
59
- if failure
60
- __fo_notify_error__(flow, step)
61
- else
62
- __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
63
83
  end
64
84
  end
65
- end
66
85
 
67
- def self.__fo_notify_error__(flow, step)
68
- if flow.output.respond_to?(:"on_#{step}_failure")
69
- flow.output.public_send(:"on_#{step}_failure", flow.given)
70
- elsif flow.output.respond_to?(:on_failure)
71
- flow.output.on_failure(flow.given, step)
72
- elsif flow.respond_to?(:"on_#{step}_failure")
73
- flow.public_send(:"on_#{step}_failure")
74
- else
75
- 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)
76
91
  end
77
- end
78
92
 
79
- def self.__fo_notify_success__(flow)
80
- if flow.output.respond_to?(:on_success)
81
- flow.output.on_success(flow.given)
82
- else
83
- 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")
84
101
  end
85
- end
86
102
 
87
- def self.call(flow: :main)
88
- failure, previous_step, object = false, :"#{self.in}_input", __fo_wrap_input__
89
- plan = public_send(:"build_#{flow}", previous_step, object)
90
- cascade = plan.call do |object, id|
91
- previous_step = id.to_sym
92
- if !object.valid?
93
- failure = true
94
- throw :halt
95
- end
96
- end
97
- __fo_resolve_cascade__(cascade, previous_step, failure)
98
- 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
99
112
 
100
- def self.flow(name = 'main', &block)
101
- wrap(name, delegate: true, &block)
102
- 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
103
124
 
104
- def on_failure(failed_step = nil)
105
- # 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
106
132
  end
107
133
 
108
- def on_success
109
- # NOOP
110
- end
134
+ extend ClassMethods
111
135
  end
112
136
  end
@@ -1,3 +1,3 @@
1
1
  module FlowObject
2
- VERSION = '0.1.4'.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.4
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: 2021-01-08 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