eco-helpers 1.5.1 → 1.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +114 -2
  3. data/LICENSE +21 -0
  4. data/eco-helpers.gemspec +1 -1
  5. data/lib/eco/api.rb +2 -0
  6. data/lib/eco/api/common.rb +4 -0
  7. data/lib/eco/api/common/base_loader.rb +54 -0
  8. data/lib/eco/api/common/class_auto_loader.rb +109 -0
  9. data/lib/eco/api/common/class_helpers.rb +33 -0
  10. data/lib/eco/api/common/class_hierarchy.rb +1 -1
  11. data/lib/eco/api/common/class_meta_basics.rb +16 -0
  12. data/lib/eco/api/common/loaders.rb +13 -0
  13. data/lib/eco/api/common/loaders/error_handler.rb +41 -0
  14. data/lib/eco/api/common/loaders/parser.rb +127 -0
  15. data/lib/eco/api/common/loaders/policy.rb +25 -0
  16. data/lib/eco/api/common/loaders/use_case.rb +40 -0
  17. data/lib/eco/api/common/people/default_parsers.rb +3 -12
  18. data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +13 -23
  19. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +20 -35
  20. data/lib/eco/api/common/people/default_parsers/date_parser.rb +15 -26
  21. data/lib/eco/api/common/people/default_parsers/freemium_parser.rb +15 -25
  22. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +26 -0
  23. data/lib/eco/api/common/people/default_parsers/multi_parser.rb +15 -27
  24. data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +14 -19
  25. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +24 -35
  26. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +15 -25
  27. data/lib/eco/api/common/people/entries.rb +54 -24
  28. data/lib/eco/api/common/people/entry_factory.rb +10 -8
  29. data/lib/eco/api/common/people/person_attribute_parser.rb +29 -12
  30. data/lib/eco/api/common/people/person_entry.rb +308 -216
  31. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +3 -2
  32. data/lib/eco/api/common/people/person_parser.rb +51 -18
  33. data/lib/eco/api/common/session/logger.rb +4 -0
  34. data/lib/eco/api/common/version_patches.rb +1 -0
  35. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +2 -0
  36. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +1 -1
  37. data/lib/eco/api/common/version_patches/exception.rb +22 -0
  38. data/lib/eco/api/common/version_patches/object.rb +10 -0
  39. data/lib/eco/api/custom.rb +13 -0
  40. data/lib/eco/api/custom/error_handler.rb +20 -0
  41. data/lib/eco/api/custom/namespace.rb +7 -0
  42. data/lib/eco/api/custom/parser.rb +50 -0
  43. data/lib/eco/api/custom/policy.rb +28 -0
  44. data/lib/eco/api/custom/use_case.rb +16 -0
  45. data/lib/eco/api/error.rb +1 -0
  46. data/lib/eco/api/error/handlers.rb +10 -3
  47. data/lib/eco/api/microcases.rb +17 -13
  48. data/lib/eco/api/microcases/account_excluded.rb +24 -0
  49. data/lib/eco/api/microcases/append_usergroups.rb +19 -0
  50. data/lib/eco/api/microcases/core_excluded.rb +4 -4
  51. data/lib/eco/api/microcases/{set_default_group.rb → fix_default_group.rb} +10 -9
  52. data/lib/eco/api/microcases/fix_filter_tags.rb +26 -6
  53. data/lib/eco/api/microcases/people_cache.rb +17 -0
  54. data/lib/eco/api/microcases/people_load.rb +59 -0
  55. data/lib/eco/api/microcases/people_refresh.rb +31 -0
  56. data/lib/eco/api/microcases/people_search.rb +65 -0
  57. data/lib/eco/api/microcases/refresh_abilities.rb +19 -0
  58. data/lib/eco/api/microcases/refresh_default_tag.rb +27 -0
  59. data/lib/eco/api/microcases/s3upload_targets.rb +39 -0
  60. data/lib/eco/api/microcases/set_account.rb +7 -19
  61. data/lib/eco/api/microcases/set_core.rb +5 -5
  62. data/lib/eco/api/microcases/set_core_with_supervisor.rb +23 -0
  63. data/lib/eco/api/microcases/set_supervisor.rb +17 -13
  64. data/lib/eco/api/microcases/strict_search.rb +12 -7
  65. data/lib/eco/api/microcases/with_each.rb +27 -0
  66. data/lib/eco/api/microcases/with_each_leaver.rb +24 -0
  67. data/lib/eco/api/microcases/with_each_present.rb +30 -0
  68. data/lib/eco/api/microcases/with_each_starter.rb +30 -0
  69. data/lib/eco/api/microcases/with_each_subordinate.rb +34 -0
  70. data/lib/eco/api/microcases/with_supervisor.rb +36 -0
  71. data/lib/eco/api/organization/people.rb +72 -35
  72. data/lib/eco/api/organization/presets_factory.rb +13 -4
  73. data/lib/eco/api/policies.rb +11 -7
  74. data/lib/eco/api/session.rb +54 -24
  75. data/lib/eco/api/session/batch.rb +1 -1
  76. data/lib/eco/api/session/batch/base_policy.rb +7 -6
  77. data/lib/eco/api/session/batch/errors.rb +28 -4
  78. data/lib/eco/api/session/batch/feedback.rb +7 -1
  79. data/lib/eco/api/session/batch/job.rb +40 -23
  80. data/lib/eco/api/session/batch/jobs.rb +9 -4
  81. data/lib/eco/api/session/batch/jobs_groups.rb +1 -1
  82. data/lib/eco/api/session/batch/request_stats.rb +95 -58
  83. data/lib/eco/api/session/batch/status.rb +35 -31
  84. data/lib/eco/api/session/config.rb +104 -42
  85. data/lib/eco/api/session/config/api.rb +17 -6
  86. data/lib/eco/api/session/config/logger.rb +2 -2
  87. data/lib/eco/api/session/config/post_launch.rb +1 -1
  88. data/lib/eco/api/session/config/workflow.rb +8 -7
  89. data/lib/eco/api/usecases.rb +47 -33
  90. data/lib/eco/api/usecases/backup/append_usergroups_case.rb +36 -0
  91. data/lib/eco/api/usecases/backup/create_case.rb +104 -0
  92. data/lib/eco/api/usecases/backup/create_details_case.rb +31 -0
  93. data/lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb +48 -0
  94. data/lib/eco/api/usecases/backup/hris_case.rb +124 -0
  95. data/lib/eco/api/usecases/backup/set_default_tag_case.rb +49 -0
  96. data/lib/eco/api/usecases/backup/set_supervisor_case.rb +41 -0
  97. data/lib/eco/api/usecases/backup/transfer_account_case.rb +90 -0
  98. data/lib/eco/api/usecases/backup/update_case.rb +112 -0
  99. data/lib/eco/api/usecases/backup/update_details_case.rb +64 -0
  100. data/lib/eco/api/usecases/backup/upsert_case.rb +114 -0
  101. data/lib/eco/api/usecases/base_case.rb +2 -0
  102. data/lib/eco/api/usecases/base_io.rb +3 -3
  103. data/lib/eco/api/usecases/default_cases.rb +23 -53
  104. data/lib/eco/api/usecases/default_cases/append_usergroups_case.rb +10 -31
  105. data/lib/eco/api/usecases/default_cases/change_email_case.rb +23 -47
  106. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +56 -43
  107. data/lib/eco/api/usecases/default_cases/create_case.rb +15 -101
  108. data/lib/eco/api/usecases/default_cases/create_details_case.rb +11 -26
  109. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +12 -43
  110. data/lib/eco/api/usecases/default_cases/delete_sync_case.rb +11 -0
  111. data/lib/eco/api/usecases/default_cases/delete_trans_case.rb +14 -0
  112. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +10 -21
  113. data/lib/eco/api/usecases/default_cases/hris_case.rb +23 -120
  114. data/lib/eco/api/usecases/default_cases/new_email_case.rb +10 -23
  115. data/lib/eco/api/usecases/default_cases/new_id_case.rb +11 -25
  116. data/lib/eco/api/usecases/default_cases/new_id_case0.rb +14 -0
  117. data/lib/eco/api/usecases/default_cases/org_data_convert_case.rb +101 -0
  118. data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +30 -0
  119. data/lib/eco/api/usecases/default_cases/refresh_case.rb +7 -20
  120. data/lib/eco/api/usecases/default_cases/reinvite_sync_case.rb +11 -0
  121. data/lib/eco/api/usecases/default_cases/reinvite_trans_case.rb +17 -0
  122. data/lib/eco/api/usecases/default_cases/remove_account_sync_case.rb +11 -0
  123. data/lib/eco/api/usecases/default_cases/remove_account_trans_case.rb +17 -0
  124. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +9 -19
  125. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +104 -0
  126. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +32 -40
  127. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +15 -33
  128. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +66 -57
  129. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -44
  130. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +40 -55
  131. data/lib/eco/api/usecases/default_cases/transfer_account_case.rb +264 -84
  132. data/lib/eco/api/usecases/default_cases/update_case.rb +15 -109
  133. data/lib/eco/api/usecases/default_cases/update_details_case.rb +14 -61
  134. data/lib/eco/api/usecases/default_cases/upsert_case.rb +16 -111
  135. data/lib/eco/api/usecases/use_case.rb +1 -1
  136. data/lib/eco/api/usecases/use_case_io.rb +9 -10
  137. data/lib/eco/cli/config.rb +10 -2
  138. data/lib/eco/cli/config/default.rb +2 -1
  139. data/lib/eco/cli/config/default/input_filters.rb +58 -0
  140. data/lib/eco/cli/config/default/options.rb +60 -25
  141. data/lib/eco/cli/config/default/people.rb +4 -4
  142. data/lib/eco/cli/config/default/people_filters.rb +108 -0
  143. data/lib/eco/cli/config/default/usecases.rb +89 -31
  144. data/lib/eco/cli/config/default/workflow.rb +37 -27
  145. data/lib/eco/cli/config/filters.rb +50 -0
  146. data/lib/eco/cli/config/filters/input_filters.rb +29 -0
  147. data/lib/eco/cli/config/filters/people_filters.rb +29 -0
  148. data/lib/eco/cli/config/help.rb +49 -0
  149. data/lib/eco/cli/config/options_set.rb +17 -1
  150. data/lib/eco/cli/config/use_cases.rb +79 -53
  151. data/lib/eco/cli/scripting.rb +10 -2
  152. data/lib/eco/cli/scripting/args_helpers.rb +25 -15
  153. data/lib/eco/cli/scripting/argument.rb +1 -0
  154. data/lib/eco/cli/scripting/arguments.rb +1 -1
  155. data/lib/eco/csv/table.rb +1 -1
  156. data/lib/eco/data/crypto/encryption.rb +3 -0
  157. data/lib/eco/language/match.rb +19 -9
  158. data/lib/eco/language/match_modifier.rb +13 -5
  159. data/lib/eco/language/models/collection.rb +77 -56
  160. data/lib/eco/language/models/parser_serializer.rb +39 -15
  161. data/lib/eco/version.rb +1 -1
  162. metadata +64 -18
  163. data/lib/eco/api/microcases/set_default_tag.rb +0 -23
  164. data/lib/eco/api/session/task.rb +0 -175
  165. data/lib/eco/api/usecases/default_case.rb +0 -19
  166. data/lib/eco/api/usecases/default_cases/delete_case.rb +0 -32
  167. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +0 -99
  168. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +0 -26
  169. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +0 -41
  170. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +0 -38
  171. data/lib/eco/api/usecases/microed_cases/hris_case.rb +0 -53
  172. data/lib/eco/api/usecases/microed_cases/update_case.rb +0 -33
  173. data/lib/eco/api/usecases/microed_cases/update_details_case.rb +0 -30
  174. data/lib/eco/api/usecases/microed_cases/upsert_case.rb +0 -36
  175. data/lib/eco/cli/config/default/filters.rb +0 -70
  176. data/lib/eco/cli/config/people_filters.rb +0 -38
@@ -4,7 +4,7 @@ ASSETS.cli.config do |config|
4
4
  io = nil
5
5
  # default rescue
6
6
  wf.rescue do |exception, io|
7
- io.session.logger.error(exception.patch_full_message)
7
+ io.session.logger.debug(exception.patch_full_message)
8
8
  wf.run(:close, io: io)
9
9
  io
10
10
  end
@@ -14,38 +14,42 @@ ASSETS.cli.config do |config|
14
14
  end
15
15
 
16
16
  wf.for(:load) do |wf_load|
17
- active_cases = nil
18
-
19
- wf_load.on(:input) do |wf_input, io|
20
- active_cases = config.usecases.active(io: io)
17
+ wf_load.for(:input) do |wf_input|
18
+ wf_input.on(:get) do |wf_input_get, io|
19
+ cases_with_input = config.usecases.active(io: io).select do |usecase, data|
20
+ io.class.input_required?(usecase.type)
21
+ end
22
+ next io unless (!io.input || io.input.empty?) && !cases_with_input.empty?
21
23
 
22
- cases_with_input = active_cases.select do |usecase, data|
23
- io.class.input_required?(usecase.type)
24
+ if io.options.dig(:input, :entries_from)
25
+ io = io.new(input: config.input.get(io: io))
26
+ else
27
+ opt_case = cases_with_input.values.first[:option]
28
+ io = io.new(input: config.input.get(io: io, option: opt_case))
29
+ end
30
+ io
24
31
  end
25
32
 
26
- next io unless (!io.input || io.input.empty?) && !cases_with_input.empty?
27
-
28
- if io.options.dig(:input, :entries_from)
29
- io = io.new(input: config.input.get(io: io))
30
- else
31
- opt_case = cases_with_input.values.first[:option]
32
- io = io.new(input: config.input.get(io: io, option: opt_case))
33
+ wf_input.on(:filter) do |wf_input_filter, io|
34
+ next io unless io.input && !io.input.empty?
35
+ io = io.new(input: config.input_filters.process(io: io))
33
36
  end
34
- io
35
37
  end
36
38
 
37
- wf_load.on(:people) do |wf_people, io|
38
- cases_with_people = active_cases.select do |usecase, data|
39
- io.class.people_required?(usecase.type)
39
+ wf_load.for(:people) do |wf_people|
40
+ wf_people.on(:get) do |wf_people_get, io|
41
+ cases_with_people = config.usecases.active(io: io).select do |usecase, data|
42
+ io.class.people_required?(usecase.type)
43
+ end
44
+ get_people = io.options.dig(:people, :get, :from) == :remote
45
+ next io unless !cases_with_people.empty? || get_people
46
+ io = io.new(people: config.people(io: io))
40
47
  end
41
- get_people = io.options.dig(:people, :get, :from) == :remote
42
- next io unless !cases_with_people.empty? || get_people
43
- io = io.new(people: config.people(io: io))
44
- end
45
48
 
46
- wf_load.on(:filter) do |wf_filter, io|
47
- next io unless io.people && !io.people.empty?
48
- io = io.new(people: config.people_filters.process(io: io))
49
+ wf_people.on(:filter) do |wf_people_filter, io|
50
+ next io unless io.people && !io.people.empty?
51
+ io = io.new(people: config.people_filters.process(io: io))
52
+ end
49
53
  end
50
54
  end
51
55
 
@@ -85,7 +89,7 @@ ASSETS.cli.config do |config|
85
89
  partial_update = io.options.dig(:people, :get, :type) == :partial
86
90
  if !io.options[:dry_run] && partial_update
87
91
  # get target people afresh
88
- people = io.session.do.people_refresh(people: io.people, include_created: true)
92
+ people = io.session.micro.people_refresh(people: io.people, include_created: true)
89
93
  io = io.new(people: people)
90
94
  else
91
95
  wf_post.skip!
@@ -116,10 +120,16 @@ ASSETS.cli.config do |config|
116
120
  end
117
121
  end
118
122
 
123
+ wf.on(:report) do |wf_report, io|
124
+ #config.reports.active(io: io)
125
+ #io.session.reports
126
+ io
127
+ end
128
+
119
129
  wf.on(:end) do |wf_end, io|
120
130
  partial_update = io.options.dig(:people, :get, :type) == :partial
121
131
  unless !io.options[:end_get] || io.options[:dry_run] || partial_update
122
- people = io.session.do.file_people
132
+ people = io.session.micro.people_cache
123
133
  io = io.new(people: people)
124
134
  end
125
135
  io
@@ -0,0 +1,50 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class Filters
5
+ include Eco::CLI::Config::Help
6
+ attr_reader :core_config
7
+
8
+ def initialize(core_config:)
9
+ @core_config = core_config
10
+ @filters = {}
11
+ @description = {}
12
+ end
13
+
14
+ # @return [String] summary of the filters.
15
+ def help(msg = "The following are the available filters:")
16
+ [msg].yield_self do |lines|
17
+ max_len = keys_max_len(@filters.keys)
18
+ @filters.keys.sort.each do |key|
19
+ lines << help_line(key, @description[key], max_len)
20
+ end
21
+ lines
22
+ end.join("\n")
23
+ end
24
+
25
+ # @param option [String] the command line option that activates this filter.
26
+ # @param desc [String] description of the filter.
27
+ def add(option, desc = nil)
28
+ raise "Missing block to define the filters builder" unless block_given?
29
+ callback = Proc.new
30
+ [option].flatten.compact.each do |opt|
31
+ @filters[opt] = callback
32
+ @description[opt] = desc
33
+ end
34
+ self
35
+ end
36
+
37
+ def process(io:)
38
+ raise "You need to override this method in child classes"
39
+ end
40
+
41
+ private
42
+
43
+
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ require_relative 'filters/people_filters'
50
+ require_relative 'filters/input_filters'
@@ -0,0 +1,29 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class Filters
5
+ class InputFilters < Eco::CLI::Config::Filters
6
+
7
+ def help
8
+ super("The following are the available filters on the input entries:")
9
+ end
10
+
11
+ def process(io:)
12
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
13
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
14
+ end
15
+
16
+ io = io.new(type: :import)
17
+ @filters.each do |arg, callback|
18
+ if SCR.get_arg(arg)
19
+ io = io.new(input: callback.call(*io.params))
20
+ end
21
+ end
22
+ io.input
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class Filters
5
+ class PeopleFilters < Eco::CLI::Config::Filters
6
+
7
+ def help
8
+ super("The following are the available filters on people:")
9
+ end
10
+
11
+ def process(io:)
12
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
13
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
14
+ end
15
+
16
+ io = io.new(type: :transform)
17
+ @filters.each do |arg, callback|
18
+ if SCR.get_arg(arg)
19
+ io = io.new(people: callback.call(*io.params))
20
+ end
21
+ end
22
+ io.people
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ module Help
5
+
6
+ def help
7
+ raise "this needs to be reimplemented in the child class and return a string"
8
+ end
9
+
10
+ private
11
+
12
+ def keys_max_len(keys)
13
+ keys.max {|a, b| a.length <=> b.length}.length
14
+ end
15
+
16
+ # Creatas a well aligned line
17
+ def help_line(key, desc, keys_max_len = key.length, line_len = 100)
18
+ blanks = keys_max_len + 3 - key.length
19
+ top_line = " #{key}#{" "*blanks} "
20
+ indent = top_line.length
21
+ first = true
22
+ each_slice_words(desc, line_len - indent).each_with_object([]) do |line, lines|
23
+ lines << (first ? "#{top_line}#{line}" : "#{" " * indent}#{line}")
24
+ first = false
25
+ end.join("\n")
26
+ end
27
+
28
+ def each_slice_words(str, max_len = 100)
29
+ liner = ""
30
+ str.to_s.scan(/[^\s]+|\s+/).each_with_object([]) do |part, out|
31
+ if "#{liner}#{part}".length <= max_len
32
+ liner << part
33
+ else
34
+ yield(liner) if block_given?
35
+ out << liner
36
+ liner = part.strip
37
+ end
38
+ end.tap do |out|
39
+ if out.empty? || !liner.empty?
40
+ yield(liner) if block_given?
41
+ out << liner if liner
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+ end
48
+ end
49
+ end
@@ -2,18 +2,34 @@ module Eco
2
2
  class CLI
3
3
  class Config
4
4
  class OptionsSet
5
+ include Eco::CLI::Config::Help
5
6
  attr_reader :core_config
6
7
 
7
8
  def initialize(core_config:)
8
9
  @core_config = core_config
9
10
  @options_set = {}
11
+ @description = {}
10
12
  end
11
13
 
12
- def add(option)
14
+ # @return [String] summary of the options.
15
+ def help
16
+ ["The following are the available options:"].yield_self do |lines|
17
+ max_len = keys_max_len(@options_set.keys)
18
+ @options_set.keys.each do |key|
19
+ lines << help_line(key, @description[key], max_len)
20
+ end
21
+ lines
22
+ end.join("\n")
23
+ end
24
+
25
+ # @param option [String] the command line option.
26
+ # @param desc [String] description of the option.
27
+ def add(option, desc = nil)
13
28
  raise "Missing block to define the options builder" unless block_given?
14
29
  callback = Proc.new
15
30
  [option].flatten.compact.each do |opt|
16
31
  @options_set[opt] = callback
32
+ @description[opt] = desc
17
33
  end
18
34
  self
19
35
  end
@@ -2,100 +2,126 @@ module Eco
2
2
  class CLI
3
3
  class Config
4
4
  class UseCases
5
-
5
+ include Eco::CLI::Config::Help
6
6
  attr_reader :core_config
7
7
 
8
8
  def initialize(core_config:)
9
9
  @core_config = core_config
10
- @linked_opt_cases = {}
10
+ @linked_cases = {}
11
+ @description = {}
12
+ end
13
+
14
+ # @return [String] summary of the use cases.
15
+ def help
16
+ ["The following are the available use cases:"].yield_self do |lines|
17
+ max_len = keys_max_len(@linked_cases.keys)
18
+ @linked_cases.keys.sort.each do |key|
19
+ lines << help_line(key, @description[key], max_len)
20
+ end
21
+ lines
22
+ end.join("\n")
11
23
  end
12
24
 
13
- # Integrates a usecase to the command line
14
- # @param option_case [String] the command line option to invoke the usecase
15
- # @param type [Symbol] the type of usecase
16
- # @param case_name [String, nil] the name of the usecase as defined
17
- def add(option_case, type, case_name: nil)
25
+ # Integrates a usecase to the command line.
26
+ # @param option_case [String] the command line option to invoke the usecase.
27
+ # @param type [Symbol] the type of usecase.
28
+ # @param desc [String] description of the case.
29
+ # @param case_name [String, nil] the name of the usecase as defined.
30
+ def add(option_case, type, desc = nil, case_name: nil)
18
31
  Eco::API::UseCases::UseCase.validate_type(type)
19
32
  unless callback = block_given?? Proc.new : nil
20
33
  raise "You must specify a valid 'case_name' when no block is provided" unless case_name
21
34
  raise "'case_name' expected to be a String. Given: #{case_name.class}" unless case_name.is_a?(String)
22
35
  end
23
36
 
24
- @linked_opt_cases[option_case] = {
37
+ @linked_cases[option_case] = {
25
38
  type => {
39
+ option: option_case,
40
+ type: type,
26
41
  casename: case_name,
27
42
  callback: callback
28
43
  }
29
44
  }
45
+ @description[option_case] = desc
30
46
 
31
47
  self
32
48
  end
33
49
 
34
50
  # Scopes/identifies which usecases are being invoked from the command line
51
+ # @note
52
+ # - this method will sort the active usecases by the position they hold in the command line
35
53
  # @param io [Eco::API::UseCases::BaseIO] the input/output object
36
54
  # @return [Hash] where keys are cases and values a `Hash` with `option` String and `callback`
37
55
  def active(io:)
56
+ validate_io!(io)
38
57
  return @active_cases unless !@active_cases
39
- @active_cases = {}.tap do |opt_cases|
40
- @linked_opt_cases.each do |option_case, types|
41
- types.each do |type, data|
42
- if SCR.get_arg(option_case)
43
- usecase = nil
44
- if case_name = data[:casename]
45
- usecase = io.session.usecases.case(case_name, type: type)
46
- end
47
-
48
- if callback = data[:callback]
49
- unless usecase
50
- # identify usecase
51
- params = io.params(keyed: true).merge(type: type)
52
- io = io.new(**params, validate: false)
53
-
54
- usecase = callback.call(*io.params)
55
- unless usecase.is_a?(Eco::API::UseCases::UseCase)
56
- msg = "When adding a usecase, without specifying 'case_name:', "
57
- msg += "the block that integrates usecase for cli option '#{option_case}'"
58
- msg += " must return an Eco::API::UseCases::UseCase object. It returns #{usecase.class}"
59
- raise msg
60
- end
61
- end
62
- end
63
-
64
- if usecase
65
- opt_cases[usecase] = {
66
- option: option_case,
67
- callback: data[:callback]
68
- }
69
- end
70
-
71
- end
58
+ active_cases = {}
59
+ @linked_cases.each do |option_case, types|
60
+ next nil unless SCR.get_arg(option_case)
61
+ types.each do |type, data|
62
+ if usecase = get_usecase(io: io, data: data)
63
+ active_cases[usecase] = {
64
+ index: SCR.get_arg_index(option_case),
65
+ option: option_case,
66
+ callback: data[:callback]
67
+ }
72
68
  end
73
69
  end
74
-
75
70
  end
71
+ @active_cases = active_cases.sort_by {|c, d| d[:index]}.to_h
76
72
  end
77
73
 
78
74
  def process(io:)
79
- unless io && io.is_a?(Eco::API::UseCases::BaseIO)
80
- raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
81
- end
82
-
75
+ validate_io!(io)
83
76
  processed = false
84
77
  active(io: io).each do |usecase, data|
85
78
  raise "Something went wrong when scoping active cases" unless data
86
-
87
79
  processed = true
80
+ io = case_io(io: io, usecase: usecase)
81
+ # some usecases have a callback to collect the parameters
82
+ data[:callback]&.call(*io.params)
83
+ io = usecase.launch(io: io)
84
+ end
85
+ processed
86
+ end
88
87
 
89
- params = io.params(keyed: true).merge(type: usecase.type)
90
- io = io.new(**params)
88
+ private
89
+
90
+ # Gets a `UseCaseIO`
91
+ def case_io(io:, usecase:)
92
+ validate_io!(io)
93
+ case io
94
+ when Eco::API::UseCases::UseCaseIO
95
+ io.chain(usecase: usecase)
96
+ when Eco::API::UseCases::BaseIO
97
+ params = io.params(keyed: true, all: true).merge(usecase: usecase)
98
+ Eco::API::UseCases::UseCaseIO.new(**params)
99
+ end
100
+ end
91
101
 
92
- if callback = data[:callback]
93
- callback.call(*io.params)
102
+ def get_usecase(io:, data:)
103
+ usecase = if case_name = data[:casename]
104
+ io.session.usecases.case(case_name, type: data[:type])
105
+ end
106
+ usecase ||= if callback = data[:callback]
107
+ # identify/retrieve usecase via callback
108
+ params = io.params(keyed: true).merge(type: data[:type])
109
+ io = io.new(**params, validate: false)
110
+ callback.call(*io.params).tap do |usecase|
111
+ unless usecase.is_a?(Eco::API::UseCases::UseCase)
112
+ msg = "When adding a usecase, without specifying 'case_name:', "
113
+ msg += "the block that integrates usecase for cli option '#{data[:option]}'"
114
+ msg += " must return an Eco::API::UseCases::UseCase object. It returns #{usecase.class}"
115
+ raise msg
116
+ end
94
117
  end
118
+ end
119
+ end
95
120
 
96
- usecase.launch(io: io)
121
+ def validate_io!(io)
122
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
123
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
97
124
  end
98
- processed
99
125
  end
100
126
 
101
127
  end