eco-helpers 3.0.21 → 3.0.22

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.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.idea/.gitignore +10 -0
  3. data/CHANGELOG.md +34 -2
  4. data/eco-helpers.gemspec +3 -3
  5. data/lib/eco/api/common/class_hierarchy.rb +6 -9
  6. data/lib/eco/api/common/loaders/case_base.rb +12 -4
  7. data/lib/eco/api/common/loaders/config/workflow/cases_extension.rb +53 -0
  8. data/lib/eco/api/common/loaders/config/workflow/mailer.rb +2 -14
  9. data/lib/eco/api/common/loaders/config/workflow.rb +4 -0
  10. data/lib/eco/api/common/loaders/error_handler.rb +3 -2
  11. data/lib/eco/api/common/loaders/policy.rb +3 -2
  12. data/lib/eco/api/common/loaders/use_case/cli_identify.rb +28 -0
  13. data/lib/eco/api/common/loaders/use_case/target_model.rb +25 -0
  14. data/lib/eco/api/common/loaders/use_case/type.rb +37 -0
  15. data/lib/eco/api/common/loaders/use_case.rb +16 -43
  16. data/lib/eco/api/error/handler.rb +6 -4
  17. data/lib/eco/api/error/handlers.rb +12 -5
  18. data/lib/eco/api/microcases/people/apply_changes/set_account/account_excluded.rb +34 -0
  19. data/lib/eco/api/microcases/people/apply_changes/set_account.rb +28 -0
  20. data/lib/eco/api/microcases/people/apply_changes/set_core/core_excluded.rb +28 -0
  21. data/lib/eco/api/microcases/people/apply_changes/set_core.rb +27 -0
  22. data/lib/eco/api/microcases/people/apply_changes/set_core_with_supervisor.rb +38 -0
  23. data/lib/eco/api/microcases/people/apply_changes/set_supervisor.rb +69 -0
  24. data/lib/eco/api/microcases/people/apply_changes.rb +19 -0
  25. data/lib/eco/api/microcases/people/fetch/with_each.rb +109 -0
  26. data/lib/eco/api/microcases/people/fetch/with_each_leaver.rb +33 -0
  27. data/lib/eco/api/microcases/people/fetch/with_each_present.rb +42 -0
  28. data/lib/eco/api/microcases/people/fetch/with_each_starter.rb +43 -0
  29. data/lib/eco/api/microcases/people/fetch/with_each_subordinate.rb +41 -0
  30. data/lib/eco/api/microcases/people/fetch/with_supervisor.rb +46 -0
  31. data/lib/eco/api/microcases/people/fetch.rb +23 -0
  32. data/lib/eco/api/microcases/people/integrity/fix_default_group.rb +39 -0
  33. data/lib/eco/api/microcases/people/integrity/fix_filter_tags.rb +55 -0
  34. data/lib/eco/api/microcases/people/integrity/refresh_default_tag.rb +32 -0
  35. data/lib/eco/api/microcases/people/integrity.rb +17 -0
  36. data/lib/eco/api/microcases/people/macro/take_email.rb +141 -0
  37. data/lib/eco/api/microcases/people/macro.rb +13 -0
  38. data/lib/eco/api/microcases/people/manage/cache.rb +36 -0
  39. data/lib/eco/api/microcases/people/manage/load.rb +90 -0
  40. data/lib/eco/api/microcases/people/manage/refresh.rb +59 -0
  41. data/lib/eco/api/microcases/people/manage/search.rb +118 -0
  42. data/lib/eco/api/microcases/people/manage.rb +19 -0
  43. data/lib/eco/api/microcases/people/preserve/default_tag.rb +29 -0
  44. data/lib/eco/api/microcases/people/preserve/filter_tags.rb +34 -0
  45. data/lib/eco/api/microcases/people/preserve/policy_groups.rb +36 -0
  46. data/lib/eco/api/microcases/people/preserve.rb +17 -0
  47. data/lib/eco/api/microcases/people.rb +21 -0
  48. data/lib/eco/api/microcases/person_update.rb +1 -1
  49. data/lib/eco/api/microcases/s3upload_targets.rb +1 -1
  50. data/lib/eco/api/microcases/with_each_contractor_present.rb +40 -0
  51. data/lib/eco/api/microcases.rb +3 -23
  52. data/lib/eco/api/organization/people/multiple_search_results.rb +48 -0
  53. data/lib/eco/api/organization/{people_similarity.rb → people/similarity.rb} +89 -72
  54. data/lib/eco/api/organization/people.rb +40 -66
  55. data/lib/eco/api/organization.rb +0 -1
  56. data/lib/eco/api/policies/policy.rb +7 -5
  57. data/lib/eco/api/policies.rb +9 -4
  58. data/lib/eco/api/session/batch/feedback/generate.rb +60 -0
  59. data/lib/eco/api/session/batch/feedback/getters.rb +28 -0
  60. data/lib/eco/api/session/batch/feedback/identifiers.rb +20 -0
  61. data/lib/eco/api/session/batch/feedback/job_dependencies.rb +45 -0
  62. data/lib/eco/api/session/batch/feedback/request_stat.rb +27 -0
  63. data/lib/eco/api/session/batch/feedback/request_stats.rb +270 -0
  64. data/lib/eco/api/session/batch/feedback.rb +45 -119
  65. data/lib/eco/api/session/batch/job/sets.rb +23 -0
  66. data/lib/eco/api/session/batch/job/type.rb +22 -0
  67. data/lib/eco/api/session/batch/job.rb +8 -18
  68. data/lib/eco/api/session/batch/jobs.rb +6 -2
  69. data/lib/eco/api/session/batch/launcher/mode_size.rb +1 -1
  70. data/lib/eco/api/session/batch/launcher/retry.rb +4 -4
  71. data/lib/eco/api/session/batch/launcher.rb +2 -2
  72. data/lib/eco/api/session/batch/policies.rb +4 -4
  73. data/lib/eco/api/session/batch/searcher.rb +2 -1
  74. data/lib/eco/api/session/batch.rb +0 -1
  75. data/lib/eco/api/session/config/workflow.rb +19 -9
  76. data/lib/eco/api/usecases/base_case/model.rb +54 -0
  77. data/lib/eco/api/usecases/base_case/type.rb +52 -0
  78. data/lib/eco/api/usecases/base_case.rb +5 -29
  79. data/lib/eco/api/usecases/base_io/chaining.rb +48 -0
  80. data/lib/eco/api/usecases/base_io/validations.rb +114 -0
  81. data/lib/eco/api/usecases/base_io.rb +65 -115
  82. data/lib/eco/api/usecases/cli/dsl.rb +3 -1
  83. data/lib/eco/api/usecases/default/people/treat/analyse_people_case.rb +7 -4
  84. data/lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb +1 -3
  85. data/lib/eco/api/usecases/default/people/treat/supers_hierarchy_case.rb +1 -5
  86. data/lib/eco/api/usecases/default_cases/entries_to_csv_case.rb +2 -2
  87. data/lib/eco/api/usecases/default_cases/hris_case.rb +0 -2
  88. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +3 -6
  89. data/lib/eco/api/usecases/graphql/base.rb +3 -2
  90. data/lib/eco/api/usecases/graphql/helpers/base/case_env.rb +1 -1
  91. data/lib/eco/api/usecases/graphql/helpers/base/error_handling.rb +3 -3
  92. data/lib/eco/api/usecases/graphql/helpers/contractors/base/load.rb +17 -0
  93. data/lib/eco/api/usecases/graphql/helpers/contractors/base.rb +8 -0
  94. data/lib/eco/api/usecases/graphql/helpers/contractors.rb +6 -0
  95. data/lib/eco/api/usecases/graphql/helpers/location/base.rb +5 -4
  96. data/lib/eco/api/usecases/graphql/helpers/location/command.rb +2 -2
  97. data/lib/eco/api/usecases/graphql/helpers.rb +1 -0
  98. data/lib/eco/api/usecases/graphql/samples/contractors/dsl.rb +5 -0
  99. data/lib/eco/api/usecases/graphql/samples/contractors.rb +17 -0
  100. data/lib/eco/api/usecases/graphql/samples/location/service/base.rb +1 -1
  101. data/lib/eco/api/usecases/graphql/samples/location.rb +1 -1
  102. data/lib/eco/api/usecases/graphql/samples.rb +1 -0
  103. data/lib/eco/api/usecases/ooze_samples/ooze_base_case.rb +0 -1
  104. data/lib/eco/api/usecases/ooze_samples/ooze_run_base_case.rb +8 -8
  105. data/lib/eco/api/usecases/use_case/chainer.rb +12 -0
  106. data/lib/eco/api/usecases/use_case.rb +32 -23
  107. data/lib/eco/api/usecases/use_case_chain/chaining.rb +88 -0
  108. data/lib/eco/api/usecases/use_case_chain.rb +17 -62
  109. data/lib/eco/api/usecases/use_case_io/chain.rb +24 -0
  110. data/lib/eco/api/usecases/use_case_io.rb +17 -22
  111. data/lib/eco/api/usecases.rb +12 -4
  112. data/lib/eco/assets.rb +1 -1
  113. data/lib/eco/cli/config/filters/people_filters.rb +6 -5
  114. data/lib/eco/cli/config/input.rb +6 -2
  115. data/lib/eco/cli/config/use_cases.rb +4 -2
  116. data/lib/eco/cli_default/people_filters.rb +1 -0
  117. data/lib/eco/cli_default/workflow.rb +149 -138
  118. data/lib/eco/data/mapper.rb +51 -19
  119. data/lib/eco/language/methods/dsl_able.rb +3 -0
  120. data/lib/eco/version.rb +1 -1
  121. metadata +66 -33
  122. data/lib/eco/api/microcases/account_excluded.rb +0 -26
  123. data/lib/eco/api/microcases/core_excluded.rb +0 -20
  124. data/lib/eco/api/microcases/fix_default_group.rb +0 -33
  125. data/lib/eco/api/microcases/fix_filter_tags.rb +0 -49
  126. data/lib/eco/api/microcases/people_cache.rb +0 -30
  127. data/lib/eco/api/microcases/people_load.rb +0 -84
  128. data/lib/eco/api/microcases/people_refresh.rb +0 -53
  129. data/lib/eco/api/microcases/people_search.rb +0 -112
  130. data/lib/eco/api/microcases/preserve_default_tag.rb +0 -23
  131. data/lib/eco/api/microcases/preserve_filter_tags.rb +0 -28
  132. data/lib/eco/api/microcases/preserve_policy_groups.rb +0 -30
  133. data/lib/eco/api/microcases/refresh_default_tag.rb +0 -26
  134. data/lib/eco/api/microcases/set_account.rb +0 -18
  135. data/lib/eco/api/microcases/set_core.rb +0 -17
  136. data/lib/eco/api/microcases/set_core_with_supervisor.rb +0 -32
  137. data/lib/eco/api/microcases/set_supervisor.rb +0 -63
  138. data/lib/eco/api/microcases/take_email_from_account.rb +0 -129
  139. data/lib/eco/api/microcases/with_each.rb +0 -103
  140. data/lib/eco/api/microcases/with_each_leaver.rb +0 -27
  141. data/lib/eco/api/microcases/with_each_present.rb +0 -36
  142. data/lib/eco/api/microcases/with_each_starter.rb +0 -37
  143. data/lib/eco/api/microcases/with_each_subordinate.rb +0 -33
  144. data/lib/eco/api/microcases/with_supervisor.rb +0 -35
  145. data/lib/eco/api/session/batch/request_stats.rb +0 -266
@@ -95,7 +95,8 @@ module Eco
95
95
  private
96
96
 
97
97
  def get(params: {}, silent: false, options: self.options)
98
- msg = "cannot batch get without api connnection, please provide a valid api connection!"
98
+ msg = "cannot batch get without api connnection, "
99
+ msg << "please provide a valid api connection!"
99
100
  fatal msg unless (people_api = api&.people)
100
101
 
101
102
  params = {per_page: batch_size(options)}.merge(params)
@@ -27,7 +27,6 @@ end
27
27
 
28
28
  require_relative 'batch/job'
29
29
  require_relative 'batch/feedback'
30
- require_relative 'batch/request_stats'
31
30
  require_relative 'batch/base_policy'
32
31
  require_relative 'batch/policies'
33
32
  require_relative 'batch/status'
@@ -7,8 +7,9 @@ module Eco
7
7
 
8
8
  WORKFLOW_MODEL = [
9
9
  :options,
10
- {load: [{input: %i[get filter]}, {people: %i[get filter]}, :filter]},
11
- :usecases, :launch_jobs,
10
+ {load: [{input: %i[get filter]}, {data: %i[get filter]}, :filter]},
11
+ :usecases,
12
+ :launch_jobs,
12
13
  {post_launch: %i[usecases launch_jobs]},
13
14
  :report,
14
15
  :end,
@@ -24,8 +25,11 @@ module Eco
24
25
  super || {}
25
26
  end
26
27
 
27
- def validate_stage(stage)
28
- "Unknown Workflow stage '#{stage}'. Should be any of #{stages}" unless stages.include?(stage)
28
+ def validate_stage!(stage)
29
+ return true if stages.include?(stage)
30
+
31
+ msg = "Unknown Workflow stage '#{stage}'. Should be any of #{stages}"
32
+ raise ArgumentError, msg
29
33
  end
30
34
 
31
35
  def workflow_class(key)
@@ -98,7 +102,8 @@ module Eco
98
102
  # 1. if block is provided provided, it returns the **current stage** object (to ease chainig).
99
103
  # 2. if block is not provided, it returns the **stage** referred by `key`
100
104
  def for(key = nil, &block)
101
- raise ArgumentError, "With no 'key', a block should be given." unless key || block_given?
105
+ msg = "With no 'key', a block should be given."
106
+ raise ArgumentError, msg unless key || block_given?
102
107
 
103
108
  tap do
104
109
  next yield(self) unless key
@@ -121,7 +126,8 @@ module Eco
121
126
  # @yieldreturn [Eco::API::UseCases::BaseIO] the `io` input/output object carried througout all the _workflow_
122
127
  # @return [Eco::API::Session::Config::Workflow] the current stage object (to ease chainig).
123
128
  def on(key = nil, &block)
124
- raise ArgumentError, "A block should be given." unless block_given?
129
+ msg = "A block should be given."
130
+ raise ArgumentError, msg unless block_given?
125
131
 
126
132
  if key
127
133
  stage(key).on(&block)
@@ -172,13 +178,15 @@ module Eco
172
178
  # @yieldreturn [Eco::API::UseCases::BaseIO] `io` the input/output object carried througout all the _workflow_
173
179
  # @return [Eco::API::Session::Config::Workflow] the current stage object (to ease chainig).
174
180
  def before(key = nil, &block)
175
- raise ArgumentError, "A block should be given." unless block_given?
181
+ msg = "A block should be given."
182
+ raise ArgumentError, msg unless block_given?
176
183
 
177
184
  if key
178
185
  stage(key).before(&block)
179
186
  else
180
187
  @before.push(block)
181
188
  end
189
+
182
190
  self
183
191
  end
184
192
 
@@ -197,13 +205,15 @@ module Eco
197
205
  # @yieldreturn [Eco::API::UseCases::BaseIO] `io` the input/output object carried througout all the _workflow_
198
206
  # @return [Eco::API::Session::Config::Workflow] the current stage object (to ease chainig).
199
207
  def after(key = nil, &block)
200
- raise ArgumentError, "A block should be given." unless block_given?
208
+ msg = "A block should be given."
209
+ raise ArgumentError, msg unless block_given?
201
210
 
202
211
  if key
203
212
  stage(key).after(&block)
204
213
  else
205
214
  @after.push(block)
206
215
  end
216
+
207
217
  self
208
218
  end
209
219
 
@@ -347,7 +357,7 @@ module Eco
347
357
  end
348
358
 
349
359
  def stage(key)
350
- self.class.validate_stage(key)
360
+ self.class.validate_stage!(key)
351
361
  @stages[key] ||= self.class.workflow_class(key).new(
352
362
  key,
353
363
  _parent: self,
@@ -0,0 +1,54 @@
1
+ class Eco::API::UseCases::BaseCase
2
+ module Model
3
+ class UnsupportedModel < ArgumentError
4
+ def initialize(msg = nil, model:, models:)
5
+ msg ||= "Unsupported model. "
6
+ msg << " Given model '#{model}'. Valid models: #{models}"
7
+ super(msg)
8
+ end
9
+ end
10
+
11
+ MODELS = %i[
12
+ people
13
+ contractors
14
+ other
15
+ ].freeze
16
+
17
+ class << self
18
+ def included(base)
19
+ super
20
+ base.extend(ClassMethods)
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+ def type_to_default_model(type = nil)
26
+ return :other if type == :other
27
+
28
+ :people
29
+ end
30
+
31
+ def valid_model?(model)
32
+ models.include?(model)
33
+ end
34
+
35
+ def models
36
+ MODELS
37
+ end
38
+
39
+ def validate_model!(model, msg: nil, models: self.models)
40
+ return model if valid_model?(model)
41
+
42
+ raise UnsupportedModel.new(msg, model: model, models: models)
43
+ end
44
+ end
45
+
46
+ # INSTANCE METHODS
47
+
48
+ private
49
+
50
+ def validate_model!(...)
51
+ self.class.validate_model!(...)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,52 @@
1
+ class Eco::API::UseCases::BaseCase
2
+ module Type
3
+ class InvalidType < ArgumentError
4
+ def initialize(msg = nil, type:, types:)
5
+ msg ||= "Invalid type. "
6
+ msg << " Given type '#{type}'. Valid types: #{types}"
7
+ super(msg)
8
+ end
9
+ end
10
+
11
+ TYPES = %i[
12
+ import
13
+ filter
14
+ transform
15
+ sync
16
+ error_handler
17
+ export
18
+ other
19
+ ].freeze
20
+
21
+ class << self
22
+ def included(base)
23
+ super
24
+ base.extend(ClassMethods)
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def valid_type?(type)
30
+ types.include?(type)
31
+ end
32
+
33
+ def types
34
+ self::TYPES
35
+ end
36
+
37
+ def validate_type!(type, msg: nil, types: self.types)
38
+ return type if valid_type?(type)
39
+
40
+ raise InvalidType.new(msg, type: type, types: types)
41
+ end
42
+ end
43
+
44
+ # INSTANCE METHODS
45
+
46
+ private
47
+
48
+ def validate_type!(...)
49
+ self.class.validate_type!(...)
50
+ end
51
+ end
52
+ end
@@ -3,37 +3,13 @@ module Eco
3
3
  class UseCases
4
4
  # Core class of UseCases. It basically defines and manages allowed `types`
5
5
  class BaseCase
6
- class InvalidType < StandardError
7
- def initialize(msg = nil, type:, types:)
8
- msg ||= "Invalid type. "
9
- msg << "Given type '#{type}'. Valid types: #{types}"
10
- super(msg)
11
- end
12
- end
6
+ require_relative 'base_case/type'
7
+ require_relative 'base_case/model'
13
8
 
14
- extend Eco::API::Common::ClassHelpers
15
-
16
- @types = %i[
17
- import
18
- filter
19
- transform
20
- sync
21
- error_handler
22
- export
23
- other
24
- ]
25
-
26
- class << self
27
- attr_reader :types
9
+ include Type
10
+ include Model
28
11
 
29
- def valid_type?(type)
30
- types.include?(type)
31
- end
32
-
33
- def validate_type(type)
34
- raise InvalidType.new(type: type, types: types) unless valid_type?(type)
35
- end
36
- end
12
+ extend Eco::API::Common::ClassHelpers
37
13
  end
38
14
  end
39
15
  end
@@ -0,0 +1,48 @@
1
+ class Eco::API::UseCases::BaseIO
2
+ module Chaining
3
+ # Shortcut to run a usecase passing this `io` as parameter
4
+ # @note
5
+ # 1. It swaps `output` to `input` or `data` depending on `self.type`
6
+ # 2. does this, **before** the target `case_name` launch and **after
7
+ # @return [Eco::API::UseCases::BaseIO]
8
+ def process_case(case_name, case_type, **params)
9
+ session.process_case(
10
+ case_name,
11
+ io: chained,
12
+ type: case_type,
13
+ **params
14
+ ).chained
15
+ end
16
+
17
+ # Does the switch from output to result in a new IO object.
18
+ # @note if there isn't `output` it doesn't do the switch,
19
+ # provided that it preserves the target parameter
20
+ # (otherise it would blank it)
21
+ # @return [Eco::API::UseCases::BaseIO]
22
+ def chained(as: type) # rubocop:disable Naming/MethodParameterName
23
+ base.tap do |io_base|
24
+ next unless io_base.output
25
+
26
+ case as
27
+ when :import
28
+ io_base.output_be_input!
29
+ when :filter
30
+ io_base.output_be_data!
31
+ # when :transform, :sync, :export, :error_handler, :other
32
+ end
33
+ end
34
+ end
35
+
36
+ protected
37
+
38
+ def output_be_input!
39
+ @input, @output = output, nil # rubocop:disable Style/ParallelAssignment
40
+ input
41
+ end
42
+
43
+ def output_be_data!
44
+ @data, @output = output, nil # rubocop:disable Style/ParallelAssignment
45
+ data
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,114 @@
1
+ class Eco::API::UseCases::BaseIO
2
+ module Validations
3
+ class MissingParameter < ArgumentError
4
+ attr_reader :type, :required, :given, :klass
5
+
6
+ def initialize(msg = nil, required:, given:, type: nil, usecase: nil)
7
+ @type = type
8
+ @required = required
9
+ @given = given
10
+ @klass = to_class(usecase)
11
+
12
+ msg = "#{msg} of type '#{type}'" if type
13
+ msg = "#{msg} (#{klass})" if klass
14
+ msg = "#{msg} requires an object '#{required}'. Given: #{given}."
15
+ super(msg)
16
+ end
17
+
18
+ private
19
+
20
+ def to_class(value)
21
+ return unless value
22
+ return value if value.is_a?(Class)
23
+
24
+ value.class
25
+ end
26
+ end
27
+
28
+ class << self
29
+ def included(base)
30
+ super
31
+ base.extend(ClassMethods)
32
+ end
33
+ end
34
+
35
+ module ClassMethods
36
+ def input_required?(type)
37
+ return true unless valid_type?(type)
38
+
39
+ %i[import sync].include?(type)
40
+ end
41
+
42
+ def data_required?(type)
43
+ return true unless valid_type?(type)
44
+
45
+ %i[filter transform sync error_handler export].include?(type)
46
+ end
47
+ end
48
+
49
+ # Instance Methods
50
+
51
+ private
52
+
53
+ def validate_args! # rubocop:disable Metrics/AbcSize
54
+ return true unless type
55
+
56
+ if !session.is_a?(Eco::API::Session)
57
+ raise MissingParameter.new(
58
+ 'UseCase',
59
+ required: :session,
60
+ given: session.class,
61
+ usecase: self
62
+ )
63
+ elsif input_required? && !input
64
+ raise MissingParameter.new(
65
+ 'UseCase',
66
+ type: type,
67
+ required: :input,
68
+ given: input.class,
69
+ usecase: self
70
+ )
71
+ elsif missing_data?
72
+ raise MissingParameter.new(
73
+ 'UseCase',
74
+ type: type,
75
+ required: model,
76
+ given: data.class,
77
+ usecase: self
78
+ )
79
+ elsif !options.is_a?(Hash)
80
+ raise MissingParameter.new(
81
+ 'Use Case options',
82
+ required: :Hash,
83
+ given: options.class,
84
+ usecase: self
85
+ )
86
+ end
87
+
88
+ true
89
+ end
90
+
91
+ def missing_data?
92
+ return false unless data_required?
93
+
94
+ case model
95
+ when :people
96
+ !data.is_a?(Eco::API::Organization::People)
97
+ # when :contractors
98
+ # !data.is_a?(Eco::API::Organization::Contractors)
99
+ when :other
100
+ false
101
+ else
102
+ true
103
+ end
104
+ end
105
+
106
+ def input_required?
107
+ self.class.input_required?(type)
108
+ end
109
+
110
+ def data_required?
111
+ self.class.data_required?(type)
112
+ end
113
+ end
114
+ end
@@ -3,74 +3,85 @@ module Eco
3
3
  class UseCases
4
4
  # Basic class to manage InputOuput for usecases
5
5
  class BaseIO < BaseCase
6
- @types = BaseCase.types
7
-
8
6
  include Eco::Language::Methods::DslAble
9
7
  include Eco::Language::AuxiliarLogger
10
8
 
11
- class MissingParameter < StandardError
12
- attr_reader :type, :required, :given
13
-
14
- def initialize(msg = nil, required:, given:, type: nil)
15
- @type = type
16
- @required = required
17
- @given = given
18
- msg += " of type '#{type}'" if type
19
- msg += " requires an object '#{required}'. Given: #{given}."
20
- super(msg)
21
- end
22
- end
23
-
24
- class << self
25
- def input_required?(type)
26
- !valid_type?(type) || %i[import sync].include?(type)
27
- end
9
+ require_relative 'base_io/validations'
10
+ require_relative 'base_io/chaining'
28
11
 
29
- def people_required?(type)
30
- !valid_type?(type) || %i[filter transform sync error_handler export].include?(type)
31
- end
32
- end
12
+ include Validations
13
+ include Chaining
33
14
 
34
- attr_reader :input, :people, :session, :options
35
- attr_reader :type
15
+ attr_reader :input, :data, :session, :options
16
+ attr_reader :type, :model
36
17
  attr_accessor :output
37
18
 
38
19
  # @param type [Symbol] a valid type (among `self.class.types`)
39
20
  # @param input [Eco::API::Common::People::Entries, Eco::API::Organization::People] the input data of reference.
40
- # @param people [Eco::API::Organization::People] people object.
21
+ # @param data [Variant] the base objects that sit in the backend.
22
+ # The data as is it exists.
23
+ # - people [Eco::API::Organization::People] people object.
41
24
  # @param session [Eco::API:Session]
42
25
  # @param options [Hash] hash with symbol keys (i.e. behaviour modifiers, cli trackers, filters, etc.)
43
26
  def initialize( # rubocop:disable Lint/MissingSuper
44
27
  session:,
45
- type: nil, input: nil, people: nil,
46
- options: {}, output: nil, validate: true
28
+ type: nil,
29
+ model: nil,
30
+ input: nil,
31
+ data: nil,
32
+ options: {},
33
+ output: nil,
34
+ validate: true,
35
+ people: nil
47
36
  )
48
- self.type = type if type
37
+ people ||= data # backwards-compatibility
38
+ data ||= people
39
+
40
+ self.type = type if type
41
+ self.model = model if model
49
42
 
50
- if self.type && validate # rubocop:disable Style/IfUnlessModifier
51
- validate_args(input: input, people: people, session: session, options: options)
52
- end
43
+ @output = output
44
+ @input = input
45
+ @data = data
46
+ @session = session
47
+ @options = options
53
48
 
54
- @output = output
55
- @input = input
56
- @people = people
57
- @session = session
58
- @options = options
49
+ validate_args! if validate
50
+ end
51
+
52
+ # backwards-compatibility
53
+ # @todo remove
54
+ def people
55
+ data
56
+ end
57
+
58
+ # @note used for DSLs
59
+ def io
60
+ self
59
61
  end
60
62
 
61
63
  # @param value [Symbol] a valid type (among `self.class.types`)
62
64
  def type=(value)
63
- self.class.validate_type(value)
65
+ validate_type!(value)
66
+
64
67
  @type = value
65
68
  end
66
69
 
70
+ # @param value [Symbol] a valid type (among `self.class.models`)
71
+ def model=(value)
72
+ validate_model!(value)
73
+
74
+ @model = value
75
+ end
76
+
67
77
  # Helper to obtain an `BaseIO` object from any child class.
68
78
  # @return [Eco::API::UseCases::BaseIO]
69
79
  def base
70
80
  kargs = params(keyed: true).merge({
71
81
  type: type,
82
+ model: model,
72
83
  output: output
73
- }).slice(:type, :input, :people, :session, :options, :output) # :validate <- ?
84
+ }).slice(*params_for_clone.keys)
74
85
 
75
86
  Eco::API::UseCases::BaseIO.new(**kargs)
76
87
  end
@@ -78,94 +89,33 @@ module Eco
78
89
  # @see Eco::API::UseCases::BaseIO#initialize
79
90
  # @return [Eco::API::UseCases::BaseIO]
80
91
  def new(**kargs)
81
- default = {
82
- type: type,
83
- input: input,
84
- people: people,
85
- session: session,
86
- options: options,
87
- output: output,
88
- validate: true
89
- }
90
- self.class.new(**default.merge(kargs))
92
+ self.class.new(**params_for_clone, **kargs)
91
93
  end
92
94
 
93
95
  # Helper to build a `Hash` of symbol keys or `Array` with params to do callbacks.
96
+ # @note order matters
94
97
  def params(keyed: false, all: false)
95
98
  kargs = {}
96
- kargs.merge!(input: input) if input_required? || all
97
- kargs.merge!(people: people) if people_required? || all
99
+ kargs.merge!(input: input) if input_required? || all
100
+ kargs.merge!(data: data) if data_required? || all
98
101
  kargs.merge!(session: session, options: options)
99
102
 
100
103
  keyed ? kargs : kargs.values
101
104
  end
102
105
 
103
- # Shortcut to run a usecase passing this `io` as parameter
104
- # @note
105
- # 1. It swaps `output` to `input` or `people` depending on `self.type`
106
- # 2. does this, **before** the target `case_name` launch and **after
107
- # @return [Eco::API::UseCases::BaseIO]
108
- def process_case(case_name, case_type, **params)
109
- session.process_case(
110
- case_name,
111
- io: chained,
112
- type: case_type,
113
- **params
114
- ).chained
115
- end
116
-
117
- # Does the switch from output to result in a new IO object.
118
- # @note if there isn't `output` it doesn't do the switch,
119
- # provided that it preserves the target parameter
120
- # (otherise it would blank it)
121
- # @return [Eco::API::UseCases::BaseIO]
122
- def chained(as: type) # rubocop:disable Naming/MethodParameterName
123
- base.tap do |io_base|
124
- next unless io_base.output
125
-
126
- case as
127
- when :import
128
- io_base.output_be_input!
129
- when :filter
130
- io_base.output_be_people!
131
- # when :transform, :sync, :export, :error_handler, :other
132
- end
133
- end
134
- end
135
-
136
106
  protected
137
107
 
138
- def output_be_input!
139
- @input, @output = output, nil # rubocop:disable Style/ParallelAssignment
140
- input
141
- end
142
-
143
- def output_be_people!
144
- @people, @output = output, nil # rubocop:disable Style/ParallelAssignment
145
- people
146
- end
147
-
148
- private
149
-
150
- def validate_args(input:, people:, session:, options:)
151
- if !session.is_a?(Eco::API::Session)
152
- raise MissingParameter.new("UseCase", required: :session, given: session.class)
153
- elsif input_required? && !input
154
- raise MissingParameter.new("UseCase", type: type, required: :input, given: input.class)
155
- elsif people_required? && !people.is_a?(Eco::API::Organization::People)
156
- raise MissingParameter.new("UseCase", type: type, required: :people, given: people.class)
157
- elsif !options || (options && !options.is_a?(Hash))
158
- raise MissingParameter.new("Use Case options", required: :Hash, given: options.class)
159
- end
160
- true
161
- end
162
-
163
- def input_required?
164
- self.class.input_required?(type)
165
- end
166
-
167
- def people_required?
168
- self.class.people_required?(type)
108
+ def params_for_clone
109
+ {
110
+ type: type,
111
+ model: model,
112
+ input: input,
113
+ data: data,
114
+ session: session,
115
+ options: options,
116
+ output: output,
117
+ validate: true
118
+ }
169
119
  end
170
120
  end
171
121
  end
@@ -34,6 +34,7 @@ module Eco
34
34
  # Unless specified, assume Cli class hangs from its case namespace
35
35
  def usecase
36
36
  raise "#{self} is to use to extend a class" unless is_a?(Class)
37
+
37
38
  @usecase ||= Kernel.const_get(to_s.split('::')[0..-2].join('::'))
38
39
  end
39
40
 
@@ -60,6 +61,7 @@ module Eco
60
61
 
61
62
  def callback(&block)
62
63
  return @callback unless block_given?
64
+
63
65
  @callback = block
64
66
  end
65
67
 
@@ -69,7 +71,7 @@ module Eco
69
71
 
70
72
  def add_option(arg, desc = nil, &block)
71
73
  tap do
72
- "Overriding option '#{arg}' on case '#{name}'" if options.key?(arg)
74
+ puts "Overriding option '#{arg}' on case '#{name}'" if options.key?(arg)
73
75
  @options[arg] = Eco::API::UseCases::Cli::Option.new(arg, desc, &block)
74
76
  end
75
77
  end