business_flow 0.8.2 → 0.9.0

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: d21b7217a94afe45033d87426bf8628ca4575b9a
4
- data.tar.gz: 80243c17f8ce51b704eb16b7da5dc0d909a1072c
3
+ metadata.gz: b19571c7a852ebc68f87ac03d0fd74a7f904c517
4
+ data.tar.gz: aec53ddd83cc11ac5f0b969a78e1c5927d682fbb
5
5
  SHA512:
6
- metadata.gz: 91b1128808b743615388bc72b636b6dc838f0d0275a6ceacd1a1c7fa33916aae39124eae90dc6fbacc5659b74d236818803bc12a75447c54a9447ae3e374d117
7
- data.tar.gz: 82cbb8c46c60750c4d5a121c922f49d6a398e8eac537980dcc647bd0816e99cb98b045409687593c2806435b339cd54962e56022e685a8b6d8f0b2fc6f982d94
6
+ metadata.gz: b2cbfe1ab1e7cffa2355262084decfc8e376d2dd08ad3a9e3098f1df4aecc08408462cdc980386d2ed2b6f5463a775b3283022caaad8c5e706def64a7bed6be1
7
+ data.tar.gz: f31ecaaaf8795914c45dacb71cdfb76e0ceae1d756649c50ac9b2d46a33276548b84996cb3674537363f7174ab2de0092550e628b6698533dee5c19925cbdb89
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- business_flow (0.8.2)
4
+ business_flow (0.9.0)
5
5
  activemodel (>= 3.0)
6
6
  activesupport (>= 3.0)
7
7
 
@@ -8,7 +8,7 @@ module BusinessFlow
8
8
  def cache_key
9
9
  klass = self.class
10
10
  key = Digest::SHA256.hexdigest(klass.cache_key.call(self, nil).to_s)
11
- "#{klass.name.underscore}/#{key}"
11
+ "#{klass.name.underscore}/#{key}/v2"
12
12
  end
13
13
 
14
14
  # DSL Methods
@@ -56,14 +56,29 @@ module BusinessFlow
56
56
  end
57
57
  end
58
58
 
59
+ # :reek:TooManyStatements This can't be easily simplified any further,
60
+ # as best as I can tell.
59
61
  def execute(flow)
62
+ add_cache_key_to_result_class
60
63
  cache_store.fetch(flow.cache_key, cache_options.to_store_options) do
61
- super(flow)
62
- raise FlowFailedException, flow if flow.errors.any?
63
- flow
64
+ result = add_cache_key(super(flow), flow)
65
+ raise FlowFailedException, result if result.errors?
66
+ result
64
67
  end
65
- rescue FlowFailedException
66
- flow
68
+ rescue FlowFailedException => exc
69
+ exc.flow
70
+ end
71
+
72
+ def add_cache_key_to_result_class
73
+ return if @cache_key_added
74
+ DSL::PublicField.new(:cache_key).add_to(const_get(:Result))
75
+ @cache_key_added = true
76
+ end
77
+
78
+ # :reek:UtilityFunction Not entirey sure where else to put this.
79
+ def add_cache_key(result, flow)
80
+ result.instance_variable_set('@cache_key'.freeze, flow.cache_key)
81
+ result
67
82
  end
68
83
  end
69
84
  end
@@ -8,7 +8,9 @@ module BusinessFlow
8
8
  check_callable
9
9
  end
10
10
 
11
- # :reek:ManualDispatch
11
+ # :reek:ManualDispatch At this stage @callable is a symbol and is either
12
+ # a method or a constant. Checking respond_to? is easier and faster than
13
+ # generating a NoMethodError and catching it.
12
14
  def call(instance, inputs)
13
15
  if instance.respond_to?(@callable, true)
14
16
  send_callable(instance, inputs)
@@ -44,15 +44,15 @@ module BusinessFlow
44
44
  end
45
45
 
46
46
  def call(parameter_object)
47
- output = flow = build(parameter_object)
48
- catch(:halt_step) { output = execute(flow) }
49
- output
47
+ _business_flow_finalize_result_class
48
+ execute(build(parameter_object))
50
49
  end
51
50
 
52
- # :reek:UtilityFunction
51
+ # :reek:UtilityFunction This is a function on us so that other modules
52
+ # can change execution behavior.
53
53
  def execute(flow)
54
- flow.call
55
- flow
54
+ catch(:halt_step) { flow.call }
55
+ result_from(flow)
56
56
  end
57
57
 
58
58
  def build(parameter_object)
@@ -66,7 +66,7 @@ module BusinessFlow
66
66
 
67
67
  def call!(*args)
68
68
  flow = call(*args)
69
- raise FlowFailedException, flow if flow.errors.any?
69
+ raise FlowFailedException, flow if flow.errors?
70
70
  flow
71
71
  end
72
72
 
@@ -81,10 +81,56 @@ module BusinessFlow
81
81
  @executor_class ||= ::BusinessFlow::DefaultStepExecutor
82
82
  end
83
83
  end
84
+
85
+ def _business_flow_finalize_result_class
86
+ return if @result_finalized
87
+ FieldList.new(provides, PublicField, const_get(:Result))
88
+ @result_finalized = true
89
+ end
90
+
91
+ def result_from(flow)
92
+ result_class = const_get(:Result)
93
+ provides_hash = Hash[provides.map do |field|
94
+ [field, flow.public_send(field)]
95
+ end]
96
+ result_class.new(flow.instance_variable_get(:@errors), provides_hash)
97
+ end
84
98
  end
85
99
 
100
+ RESULT_DEF = %(
101
+ class Result
102
+ def initialize(errors, provides)
103
+ @errors = errors
104
+ provides.each do |(name, value)|
105
+ instance_variable_set("@" + name.to_s, value)
106
+ end
107
+ end
108
+
109
+ def errors
110
+ @errors ||= ActiveModel::Errors.new(self)
111
+ end
112
+
113
+ def errors?
114
+ # We're explicitly using the instance variable here so that if no
115
+ # errors have been created, we don't initialize the error object.
116
+ @errors.present?
117
+ end
118
+
119
+ def valid?(_context = nil)
120
+ # We're explicitly using the instance variable here so that if no
121
+ # errors have been created, we don't initialize the error object.
122
+ @errors.blank?
123
+ end
124
+
125
+ def invalid?(context = nil)
126
+ !valid?(context)
127
+ end
128
+ end
129
+ ).freeze
130
+
86
131
  def self.included(klass)
87
132
  klass.extend(ClassMethods)
133
+ klass.class_eval RESULT_DEF, __FILE__, __LINE__
88
134
  end
89
135
 
90
136
  attr_reader :parameter_object
@@ -99,7 +145,7 @@ module BusinessFlow
99
145
  # Responsible for setting the parameter object and validating inputs.
100
146
  # This is a method directly on the object instead of something we
101
147
  # handle through instance_eval/exec for performance reasons.
102
- # :reek:NilCheck
148
+ # :reek:NilCheck This is where we ensure that our needs are non-nil.
103
149
  private def _business_flow_dsl_initialize(parameter_object, needs)
104
150
  @parameter_object = parameter_object
105
151
  needs.each do |need|
@@ -116,10 +162,14 @@ module BusinessFlow
116
162
  end
117
163
 
118
164
  def errors?
165
+ # We're explicitly using the instance variable here so that if no
166
+ # errors have been created, we don't initialize the error object.
119
167
  @errors.present?
120
168
  end
121
169
 
122
170
  def valid?(_context = nil)
171
+ # We're explicitly using the instance variable here so that if no
172
+ # errors have been created, we don't initialize the error object.
123
173
  @errors.blank?
124
174
  end
125
175
 
@@ -132,9 +182,10 @@ module BusinessFlow
132
182
  attr_reader :field_list
133
183
 
134
184
  def initialize(field_list, field_klass, klass)
135
- @field_list = field_list
185
+ @field_list = []
136
186
  @field_klass = field_klass
137
187
  @klass = klass
188
+ add_fields(field_list)
138
189
  end
139
190
 
140
191
  def add_fields(fields)
@@ -297,6 +348,10 @@ module BusinessFlow
297
348
  value
298
349
  end
299
350
 
351
+ def to_s
352
+ @parameters.to_s
353
+ end
354
+
300
355
  private
301
356
 
302
357
  def inner_fetch(key)
@@ -1,3 +1,3 @@
1
1
  module BusinessFlow
2
- VERSION = '0.8.2'.freeze
2
+ VERSION = '0.9.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business_flow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Scarborough
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-02 00:00:00.000000000 Z
11
+ date: 2018-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel