eco-helpers 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eco/api.rb +1 -0
  3. data/lib/eco/api/common.rb +1 -0
  4. data/lib/eco/api/common/class_helpers.rb +33 -0
  5. data/lib/eco/api/common/people.rb +1 -0
  6. data/lib/eco/api/common/people/person_attribute_parser.rb +52 -0
  7. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +2 -2
  8. data/lib/eco/api/common/people/person_parser.rb +16 -10
  9. data/lib/eco/api/common/version_patches.rb +2 -3
  10. data/lib/eco/api/common/version_patches/ecoportal_api.rb +4 -0
  11. data/lib/eco/api/common/version_patches/{base_model.rb → ecoportal_api/base_model.rb} +0 -0
  12. data/lib/eco/api/common/version_patches/{external_person.rb → ecoportal_api/external_person.rb} +0 -0
  13. data/lib/eco/api/common/version_patches/{internal_person.rb → ecoportal_api/internal_person.rb} +0 -0
  14. data/lib/eco/api/common/version_patches/hash.rb +2 -0
  15. data/lib/eco/api/common/version_patches/hash/deep_merge.rb +34 -0
  16. data/lib/eco/api/error.rb +133 -0
  17. data/lib/eco/api/error/handler.rb +19 -0
  18. data/lib/eco/api/error/handlers.rb +22 -0
  19. data/lib/eco/api/organization/people.rb +11 -11
  20. data/lib/eco/api/organization/policy_groups.rb +8 -0
  21. data/lib/eco/api/policies.rb +26 -3
  22. data/lib/eco/api/policies/policy.rb +4 -5
  23. data/lib/eco/api/session.rb +27 -18
  24. data/lib/eco/api/session/batch.rb +12 -6
  25. data/lib/eco/api/session/batch/errors.rb +134 -0
  26. data/lib/eco/api/session/batch/job.rb +213 -0
  27. data/lib/eco/api/session/batch/jobs.rb +72 -0
  28. data/lib/eco/api/session/batch/jobs_groups.rb +85 -0
  29. data/lib/eco/api/session/batch/status.rb +133 -0
  30. data/lib/eco/api/session/config.rb +36 -14
  31. data/lib/eco/api/session/config/base_config.rb +2 -0
  32. data/lib/eco/api/session/config/people.rb +8 -0
  33. data/lib/eco/api/session/config/post_launch.rb +58 -0
  34. data/lib/eco/api/session/config/workflow.rb +189 -0
  35. data/lib/eco/api/session/task.rb +49 -6
  36. data/lib/eco/api/usecases.rb +137 -2
  37. data/lib/eco/api/usecases/base_case.rb +20 -8
  38. data/lib/eco/api/usecases/base_io.rb +97 -0
  39. data/lib/eco/api/usecases/default_case.rb +19 -0
  40. data/lib/eco/api/usecases/default_cases.rb +2 -2
  41. data/lib/eco/api/usecases/default_cases/change_email_case.rb +2 -2
  42. data/lib/eco/api/usecases/default_cases/create_case.rb +2 -2
  43. data/lib/eco/api/usecases/default_cases/create_details_case.rb +2 -2
  44. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +2 -2
  45. data/lib/eco/api/usecases/default_cases/delete_case.rb +2 -2
  46. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +2 -2
  47. data/lib/eco/api/usecases/default_cases/hris_case.rb +2 -2
  48. data/lib/eco/api/usecases/default_cases/new_email_case.rb +2 -2
  49. data/lib/eco/api/usecases/default_cases/new_id_case.rb +2 -2
  50. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +11 -8
  51. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +2 -2
  52. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +2 -2
  53. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +2 -2
  54. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +2 -2
  55. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +2 -2
  56. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +2 -2
  57. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +2 -2
  58. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +4 -4
  59. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +3 -3
  60. data/lib/eco/api/usecases/default_cases/update_case.rb +2 -2
  61. data/lib/eco/api/usecases/default_cases/update_details_case.rb +2 -2
  62. data/lib/eco/api/usecases/default_cases/upsert_case.rb +2 -2
  63. data/lib/eco/api/usecases/use_case.rb +23 -36
  64. data/lib/eco/api/usecases/use_case_chain.rb +14 -24
  65. data/lib/eco/api/usecases/use_case_io.rb +23 -75
  66. data/lib/eco/assets.rb +11 -11
  67. data/lib/eco/cli.rb +37 -0
  68. data/lib/eco/cli/config.rb +63 -1
  69. data/lib/eco/cli/config/default.rb +15 -0
  70. data/lib/eco/cli/config/default/filters.rb +69 -0
  71. data/lib/eco/cli/config/default/input.rb +21 -0
  72. data/lib/eco/cli/config/default/options.rb +47 -0
  73. data/lib/eco/cli/config/default/people.rb +39 -0
  74. data/lib/eco/cli/config/default/usecases.rb +63 -0
  75. data/lib/eco/cli/config/default/workflow.rb +86 -0
  76. data/lib/eco/cli/config/input.rb +40 -0
  77. data/lib/eco/cli/config/options_set.rb +35 -0
  78. data/lib/eco/cli/config/people_filters.rb +38 -0
  79. data/lib/eco/cli/config/use_cases.rb +87 -0
  80. data/lib/eco/cli/scripting/args_helpers.rb +10 -4
  81. data/lib/eco/cli/scripting/argument.rb +6 -0
  82. data/lib/eco/language/models/collection.rb +1 -0
  83. data/lib/eco/version.rb +1 -1
  84. metadata +32 -12
  85. data/lib/eco/api/policies/base_policy.rb +0 -14
  86. data/lib/eco/api/policies/used_policies.rb +0 -37
  87. data/lib/eco/api/session/batch_job.rb +0 -215
  88. data/lib/eco/api/session/batch_jobs.rb +0 -62
  89. data/lib/eco/api/session/batch_status.rb +0 -205
  90. data/lib/eco/api/session/job_groups.rb +0 -75
  91. data/lib/eco/api/usecases/use_group.rb +0 -124
  92. data/lib/eco/cli/config/options.rb +0 -11
@@ -1,32 +1,14 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
4
- class UseCase
5
- TYPES = [:import, :filter, :transform, :sync, :export]
6
- ALL_PARAMS = [:input, :people, :session, :options]
7
- TYPE_PARAMS = {
8
- import: [:input, :session],
9
- filter: [:people, :session, :options],
10
- transform: [:people, :session],
11
- export: [:people, :session, :options]
12
- }
13
-
14
- class << self
15
- def valid_type?(type)
16
- TYPES.include?(type)
17
- end
18
-
19
- def type_params(type)
20
- raise "Invalid type '#{type.to_s}'" if !valid_type?(type)
21
- TYPE_PARAMS[type]
22
- end
23
- end
3
+ class UseCases
4
+ class UseCase < BaseCase
5
+ @types = [:import, :filter, :transform, :sync, :export]
24
6
 
25
7
  attr_reader :name, :type, :times_launched
26
8
  attr_reader :options
27
9
 
28
10
  def initialize(name, type:, root:, &block)
29
- raise "Undefined usecase type #{type}, when creating '#{name}'. Please, use any of #{TYPES}" unless self.class.valid_type?(type)
11
+ raise InvalidType.new("Invalid type for '#{name}'.", type: type, types: self.class.types) unless self.class.valid_type?(type)
30
12
 
31
13
  self.root = root
32
14
  @callback = block
@@ -36,28 +18,33 @@ module Eco
36
18
  end
37
19
 
38
20
  def chainer
39
- # TODO: root is a UseGroup that will not point to this new case.
21
+ # TODO: root is a Eco::API::UseCases that will not point to this new case.
40
22
  # => Moreover, the name and type will be the same as self
41
23
  Eco::API::UseCases::UseCaseChain.new(usecase: self, root: @root)
42
24
  end
43
25
 
44
26
  def root=(value)
45
- raise "Root should be a UseGroup. Given: #{value}" if !value.is_a?(UseGroup)
27
+ raise "Root should be a Eco::API::UseCases. Given: #{value.class}" if !value.is_a?(Eco::API::UseCases)
46
28
  @root = value
47
29
  end
48
30
 
49
- def launch(input: nil, people: nil, session:, options: {})
50
- @options = options
51
- data = UseCaseIO.new(usecase: self, input: input, people: people, session: session, options: options)
52
-
53
- data.output = @callback.call(data.params)
54
- @times_launched += 1
55
-
56
- data_model = {
57
- self => {
58
- io: data
59
- }
60
- }
31
+ # Actual launch of the usecase
32
+ # @param io [Eco::API::UseCases::BaseIO] an input/output helper with the necessary parameters.
33
+ # @param kargs [Hash] hash with symbol keys.
34
+ # @option kargs [Eco::API::Common::People::Entries, Eco::API::Organization::People] :input the input data of reference.
35
+ # @option kargs [Eco::API::Organization::People] :people object.
36
+ # @option kargs [Eco::API:Session] :session
37
+ # @option kargs [Hash] :options hash with symbol keys (i.e. behaviour modifiers, cli trackers, filters, etc.)
38
+ def launch(io: nil, **kargs)
39
+ params = io&.params(keyed: true) || {}
40
+ kargs = params.merge(kargs).merge(usecase: self)
41
+
42
+ UseCaseIO.new(**kargs).tap do |uio|
43
+ @options = uio.options
44
+ uio.session.logger.debug("#{self.class}: going to process '#{name}'")
45
+ uio.output = @callback.call(*uio.params)
46
+ @times_launched += 1
47
+ end
61
48
  end
62
49
 
63
50
  protected
@@ -1,10 +1,12 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
3
+ class UseCases
4
4
  class UseCaseChain < UseCase
5
5
  MAX_CHAINS = 70
6
6
  @@num_chains = 0
7
7
 
8
+ @types = [:import, :filter, :transform, :sync, :export]
9
+
8
10
  def initialize(name = nil, type: nil, root:, usecase: nil, &block)
9
11
  if usecase
10
12
  raise "Expected Eco::API::UseCases::UseCase. Given #{usecase.class}" if !usecase.is_a?(Eco::API::UseCases::UseCase)
@@ -19,7 +21,7 @@ module Eco
19
21
  end
20
22
 
21
23
  def root=(value)
22
- raise "You cannot change root UseGroup once the chains have been resolved" if @resolved_chains
24
+ raise "You cannot change root Eco::API::UseCases once the chains have been resolved" if @resolved_chains
23
25
  super(value)
24
26
  end
25
27
 
@@ -43,9 +45,15 @@ module Eco
43
45
  end
44
46
  end
45
47
 
46
- def launch(input: nil, people: nil, session:, options: {})
47
- data_model = super(input: input, people: people, session: session, options: options)
48
- launch_chain(data_model)
48
+ # @see Eco::API::UseCases::UseCase#launch
49
+ def launch(io: nil, **kargs)
50
+ super(io: io, **kargs).tap do |uio|
51
+ next if resolved_chains.empty?
52
+
53
+ resolved_chains.each do |usecase|
54
+ uio = usecase.launch(io: uio.chain(usecase: usecase))
55
+ end
56
+ end
49
57
  end
50
58
 
51
59
  def chain(usecase = nil)
@@ -67,7 +75,7 @@ module Eco
67
75
 
68
76
  def resolved_chains(use_group = nil)
69
77
  return @resolved_chains if @resolved_chains
70
- raise "Only UseGroup object can resolve chains. Given: #{use_group} " if use_group && !use_group.is_a?(UseGroup)
78
+ raise "Only Eco::API::UseCases object can contain resolved chains. Given: #{use_group.class} " if use_group && !use_group.is_a?(Eco::API::UseCases)
71
79
 
72
80
  use_group = use_group || @root
73
81
  @resolved_chains = @chains.map do |usecase|
@@ -78,24 +86,6 @@ module Eco
78
86
  end
79
87
  end
80
88
 
81
- private
82
-
83
- def launch_chain(data_model)
84
- return data_model if resolved_chains.empty?
85
-
86
- data_model.tap do |dm|
87
- dm[self][:chains] ||= {}
88
-
89
- resolved_chains.each do |usecase|
90
- # chained cases use same params as parent case (out of simplicity)
91
- data_chain = dm[self][:io].chain(usecase: usecase)
92
- keyed_data = data_chain.params(keyed: true)
93
- keyed_data.delete(:usecase)
94
- dm[self][:chains].merge(usecase.launch(keyed_data))
95
- end
96
- end
97
- end
98
-
99
89
  end
100
90
 
101
91
  end
@@ -1,45 +1,25 @@
1
1
  module Eco
2
2
  module API
3
- module UseCases
4
- class UseCaseIO
5
- TYPES = [:import, :filter, :transform, :sync, :export]
6
- ALL_PARAMS = [:input, :people, :session, :options]
7
- TYPE_PARAMS = {
8
- import: [:input, :session],
9
- filter: [:people, :session, :options],
10
- transform: [:people, :session],
11
- export: [:people, :session, :options]
12
- }
3
+ class UseCases
4
+ class UseCaseIO < BaseIO
5
+ @types = [:import, :filter, :transform, :sync, :export]
13
6
 
14
7
  attr_reader :usecase
15
- attr_reader :input, :people, :session, :options
16
- attr_accessor :output
17
8
 
18
- class << self
19
- def valid_type?(type)
20
- TYPES.include?(type)
21
- end
22
-
23
- def type_params(type)
24
- raise "Invalid type '#{type.to_s}'" if !valid_type?(type)
25
- TYPE_PARAMS[type]
26
- end
27
- end
28
-
29
- def initialize(input: nil, people: nil, session:, options: {}, usecase:)
9
+ def initialize(usecase:, **kargs)
30
10
  self.usecase = usecase
31
- @output = nil
32
-
33
- validate_args(input: input, people: people, session: session, options: options)
11
+ super(**kargs)
12
+ end
34
13
 
35
- @input = input
36
- @people = people
37
- @session = session
38
- @options = options
14
+ # @see Eco::API::UseCases::BaseIO
15
+ # @param usecase [Eco::API::UseCases::UseCase] target usecase
16
+ # @return [Eco::API::UseCases::UseCaseIO]
17
+ def new(usecase:, **kargs)
18
+ super(**kargs.merge(usecase: usecase))
39
19
  end
40
20
 
41
21
  def usecase=(value)
42
- raise "It should be a UseCase. Given: #{value}" if !value.is_a?(UseCase)
22
+ raise "It should be a Eco::API::UseCases::UseCase. Given: #{value}" if !value.is_a?(Eco::API::UseCases::UseCase)
43
23
  @usecase = value
44
24
  end
45
25
 
@@ -47,17 +27,18 @@ module Eco
47
27
  @usecase.type
48
28
  end
49
29
 
50
- def params(input: nil, people: nil, session: nil, options: {}, keyed: false)
51
- opts = @options.merge(options&.dup || {})
52
- input ||= @input
53
- people ||= @people
54
- session ||= @session
30
+ def type=(value)
31
+ raise "Can't modify type depends on the usecase linked to this IO object"
32
+ end
55
33
 
56
- kargs = {}
57
- kargs.merge!(input: input) if input_required?
58
- kargs.merge!(people: people) if people_required?
59
- kargs.merge!(session: session, options: opts, usecase: usecase)
60
- keyed ? kargs : kargs.values
34
+ def params(keyed: false)
35
+ super(keyed: keyed).tap do |res|
36
+ if keyed
37
+ res.merge!(usecase: usecase)
38
+ else
39
+ res.push(usecase)
40
+ end
41
+ end
61
42
  end
62
43
 
63
44
  def chain(usecase:)
@@ -76,39 +57,6 @@ module Eco
76
57
  end
77
58
  self.class.new(kargs)
78
59
  end
79
-
80
- protected
81
-
82
- private
83
-
84
- def validate_args(input:, people:, session:, options:)
85
- case
86
- when !session.is_a?(Eco::API::Session)
87
- raise "A UseCase needs a Session object. Given: #{session}"
88
- when input_required? && !input
89
- raise "UseCase of type '#{type}' requires a valid input. None given"
90
- when people_required? && !people.is_a?(Eco::API::Organization::People)
91
- raise "UseCase of type '#{type}' requires a People object. Given: #{people}"
92
- when !options || (options && !options.is_a?(Hash))
93
- raise "To inject dependencies via ':options' it should be a Hash object. Given: #{options}"
94
- when options_required? && !options
95
- raise "UseCase of type '#{type}' requires a Hash ':options' object."
96
- end
97
- true
98
- end
99
-
100
- def input_required?
101
- [:import, :sync].include?(type)
102
- end
103
-
104
- def people_required?
105
- [:filter, :transform, :sync, :export].include?(type)
106
- end
107
-
108
- def options_required?
109
- [:filter].include?(type)
110
- end
111
-
112
60
  end
113
61
 
114
62
  end
data/lib/eco/assets.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'ecoportal/api'
2
-
3
1
  module Eco
4
2
  class Assets
5
3
 
@@ -7,23 +5,16 @@ module Eco
7
5
 
8
6
  def initialize
9
7
  @active_config = :default
8
+ @cli = Eco::CLI.new
9
+ @cli_init = false
10
10
  end
11
11
 
12
12
  def session(key: active_config, update_active: true)
13
13
  config(key: key, update_active: update_active).session.tap do |session|
14
14
  yield(session) if block_given?
15
15
  end
16
- #sessions[key] ||= Eco::API::Session.new(config(key: key, update_active: update_active))
17
- #sessions[key].tap do |session|
18
- # yield(session) if block_given?
19
- #end
20
16
  end
21
17
 
22
- #def sessions
23
- # @sessions ||= {}
24
- #end
25
-
26
-
27
18
  def config(key: active_config, update_active: true)
28
19
  configs[:default] ||= Eco::API::Session::Config.new(key)
29
20
  unless configs.key?(key)
@@ -39,6 +30,15 @@ module Eco
39
30
  def configs
40
31
  @configs ||= {}
41
32
  end
33
+
34
+ def cli
35
+ unless @cli_init
36
+ @cli_init = true
37
+ #pp "going to load default cli config..."
38
+ require_relative('cli/config/default')
39
+ end
40
+ @cli
41
+ end
42
42
  end
43
43
  end
44
44
 
data/lib/eco/cli.rb CHANGED
@@ -1,5 +1,42 @@
1
1
  module Eco
2
2
  class CLI
3
+
4
+ def initialize
5
+ @config = nil
6
+ end
7
+
8
+ def args
9
+ ARGV
10
+ end
11
+
12
+ def options
13
+ @options ||= {}
14
+ end
15
+
16
+ def config
17
+ @config ||= Eco::CLI::Config.new(cli: self)
18
+ @config.tap do |cnf|
19
+ yield(cnf) if block_given?
20
+ end
21
+ end
22
+
23
+ def workflow
24
+ @workflow ||= Eco::CLI::Workflow.new(cli: self)
25
+ end
26
+
27
+ def run(session:)
28
+ io = Eco::API::UseCases::BaseIO.new(session: session, options: options)
29
+ io = io.new(options: config.options_set.process(io: io))
30
+
31
+ session.workflow(io: io) do |wf, io|
32
+ io = wf.run(:load, io: io)
33
+ io = wf.run(:usecases, io: io)
34
+ io = wf.run(:launch_jobs, io: io)
35
+ io = wf.run(:post_launch, io: io)
36
+ io = wf.run(:end, io: io)
37
+ end
38
+ end
39
+
3
40
  end
4
41
  end
5
42
 
@@ -2,9 +2,71 @@ module Eco
2
2
  class CLI
3
3
  class Config
4
4
 
5
+ attr_reader :cli
6
+
7
+ def initialize(cli:)
8
+ @cli = cli
9
+ end
10
+
11
+ def options
12
+ cli.options
13
+ end
14
+
15
+ def args
16
+ cli.args
17
+ end
18
+
19
+ def options_set
20
+ @opions_set ||= Eco::CLI::Config::OptionsSet.new(core_config: self)
21
+ @opions_set.tap do |opts_set|
22
+ yield(opts_set, options) if block_given?
23
+ end
24
+ end
25
+
26
+ def input(default_option: nil)
27
+ @input ||= Eco::CLI::Config::Input.new(core_config: self, default_option: default_option)
28
+ if block_given?
29
+ @input.define(&Proc.new)
30
+ self
31
+ else
32
+ @input
33
+ end
34
+ end
35
+
36
+ def people(io: nil)
37
+ if block_given?
38
+ @people_load = Proc.new
39
+ self
40
+ else
41
+ raise "There is no definition on how to load people" unless instance_variable_defined?(:@people_load) && @people_load
42
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
43
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
44
+ end
45
+
46
+ io = io.new(type: :import)
47
+ @people_load.call(*io.params)
48
+ end
49
+ end
50
+
51
+ def people_filters
52
+ @people_filters ||= Eco::CLI::Config::PeopleFilters.new(core_config: self)
53
+ @people_filters.tap do |filters|
54
+ yield(filters) if block_given?
55
+ end
56
+ end
57
+
58
+ def usecases
59
+ @usecases ||= Eco::CLI::Config::UseCases.new(core_config: self)
60
+ @usecases.tap do |cases|
61
+ yield(cases) if block_given?
62
+ end
63
+ end
5
64
 
6
65
  end
7
66
  end
8
67
  end
9
68
 
10
- require_relative 'config/options'
69
+ require_relative 'config/options_set'
70
+ require_relative 'config/input'
71
+ require_relative 'config/people_filters'
72
+ require_relative 'config/use_cases'