eco-helpers 2.6.4 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +95 -0
  4. data/CHANGELOG.md +139 -2
  5. data/Rakefile +13 -7
  6. data/eco-helpers.gemspec +2 -2
  7. data/lib/eco/api/common/loaders/base.rb +2 -2
  8. data/lib/eco/api/common/loaders/case_base.rb +1 -1
  9. data/lib/eco/api/common/loaders/config/workflow/mailer.rb +5 -5
  10. data/lib/eco/api/common/loaders/error_handler.rb +8 -5
  11. data/lib/eco/api/common/loaders/parser.rb +44 -22
  12. data/lib/eco/api/common/loaders/policy.rb +6 -4
  13. data/lib/eco/api/common/loaders/use_case.rb +13 -7
  14. data/lib/eco/api/common/people/base_parser.rb +0 -2
  15. data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +0 -1
  16. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +1 -1
  17. data/lib/eco/api/common/people/default_parsers/date_parser.rb +64 -12
  18. data/lib/eco/api/common/people/default_parsers/freemium_parser.rb +0 -1
  19. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +13 -5
  20. data/lib/eco/api/common/people/default_parsers/multi_parser.rb +0 -1
  21. data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +18 -5
  22. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +8 -8
  23. data/lib/eco/api/common/people/default_parsers/select_parser.rb +50 -26
  24. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +6 -6
  25. data/lib/eco/api/common/people/default_parsers/xls_parser.rb +9 -12
  26. data/lib/eco/api/common/people/default_parsers.rb +1 -12
  27. data/lib/eco/api/common/people/entries.rb +13 -13
  28. data/lib/eco/api/common/people/entry_factory.rb +76 -45
  29. data/lib/eco/api/common/people/person_attribute_parser.rb +8 -12
  30. data/lib/eco/api/common/people/person_entry.rb +86 -75
  31. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +60 -44
  32. data/lib/eco/api/common/people/person_factory.rb +30 -22
  33. data/lib/eco/api/common/people/person_modifier.rb +11 -13
  34. data/lib/eco/api/common/people/person_parser.rb +101 -39
  35. data/lib/eco/api/common/people/supervisor_helpers.rb +25 -26
  36. data/lib/eco/api/common/session/base_session.rb +9 -9
  37. data/lib/eco/api/common/session/environment.rb +7 -5
  38. data/lib/eco/api/common/session/sftp.rb +59 -32
  39. data/lib/eco/api/common/version_patches/exception.rb +11 -13
  40. data/lib/eco/api/error.rb +32 -20
  41. data/lib/eco/api/organization/node_classifications.rb +82 -0
  42. data/lib/eco/api/organization/policy_groups.rb +4 -6
  43. data/lib/eco/api/organization/tag_tree.rb +169 -93
  44. data/lib/eco/api/organization.rb +1 -0
  45. data/lib/eco/api/session/batch/job.rb +1 -1
  46. data/lib/eco/api/session/config/tagtree.rb +41 -23
  47. data/lib/eco/api/session/config/workflow.rb +113 -88
  48. data/lib/eco/api/session/config.rb +6 -0
  49. data/lib/eco/api/session.rb +51 -29
  50. data/lib/eco/api/usecases/base_io.rb +28 -25
  51. data/lib/eco/api/usecases/default/locations/cli/tagtree_extract_cli.rb +7 -2
  52. data/lib/eco/api/usecases/default/locations/cli/tagtree_upload_cli.rb +21 -0
  53. data/lib/eco/api/usecases/default/locations/csv_to_tree_case.rb +3 -3
  54. data/lib/eco/api/usecases/default/locations/tagtree_extract_case.rb +54 -23
  55. data/lib/eco/api/usecases/default/locations/tagtree_upload_case.rb +87 -0
  56. data/lib/eco/api/usecases/default/locations.rb +1 -0
  57. data/lib/eco/api/usecases/default/people/analyse_people_case.rb +60 -56
  58. data/lib/eco/api/usecases/default/people/change_email_case.rb +8 -9
  59. data/lib/eco/api/usecases/default/people/clean_unknown_tags_case.rb +13 -11
  60. data/lib/eco/api/usecases/default/people/clear_abilities_case.rb +2 -2
  61. data/lib/eco/api/usecases/default/people/org_data_convert_case.rb +25 -27
  62. data/lib/eco/api/usecases/default/people/refresh_case.rb +2 -2
  63. data/lib/eco/api/usecases/default/people/reinvite_trans_case.rb +1 -1
  64. data/lib/eco/api/usecases/default/people/reinvite_trans_cli.rb +0 -1
  65. data/lib/eco/api/usecases/default/people/restore_db_case.rb +39 -34
  66. data/lib/eco/api/usecases/default/people/set_default_tag_case.rb +19 -15
  67. data/lib/eco/api/usecases/default/people/supers_cyclic_identify_case.rb +16 -12
  68. data/lib/eco/api/usecases/default_cases/hris_case.rb +17 -15
  69. data/lib/eco/api/usecases/default_cases/samples/sftp_case.rb +30 -16
  70. data/lib/eco/api/usecases/graphql/base.rb +5 -3
  71. data/lib/eco/api/usecases/graphql/helpers/base/case_env.rb +4 -1
  72. data/lib/eco/api/usecases/graphql/helpers/base/graphql_env.rb +14 -0
  73. data/lib/eco/api/usecases/graphql/helpers/base.rb +5 -4
  74. data/lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb +60 -0
  75. data/lib/eco/api/usecases/graphql/helpers/location/base/tree_tracking.rb +72 -0
  76. data/lib/eco/api/usecases/graphql/helpers/location/base.rb +25 -59
  77. data/lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb +59 -0
  78. data/lib/eco/api/usecases/graphql/helpers/location/command/diff/compare.rb +49 -0
  79. data/lib/eco/api/usecases/graphql/helpers/location/command/diff.rb +11 -0
  80. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/commandable.rb +46 -0
  81. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/diff_sortable/for_archive.rb +23 -0
  82. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/diff_sortable/for_unarchive.rb +65 -0
  83. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/diff_sortable.rb +49 -0
  84. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/sortable/relation_safe_sort.rb +119 -0
  85. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/sortable.rb +59 -0
  86. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages.rb +82 -0
  87. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs.rb +20 -0
  88. data/lib/eco/api/usecases/graphql/helpers/location/command/optimizations.rb +84 -0
  89. data/lib/eco/api/usecases/graphql/helpers/location/command/result.rb +4 -4
  90. data/lib/eco/api/usecases/graphql/helpers/location/command/results.rb +24 -12
  91. data/lib/eco/api/usecases/graphql/helpers/location/command.rb +21 -24
  92. data/lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb +1 -1
  93. data/lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_set.rb +10 -11
  94. data/lib/eco/api/usecases/graphql/helpers/location/tags_remap.rb +8 -9
  95. data/lib/eco/api/usecases/graphql/samples/location/command/dsl.rb +41 -12
  96. data/lib/eco/api/usecases/graphql/samples/location/command/results.rb +11 -80
  97. data/lib/eco/api/usecases/graphql/samples/location/command/service/tree_update.rb +89 -0
  98. data/lib/eco/api/usecases/graphql/samples/location/command/service.rb +6 -0
  99. data/lib/eco/api/usecases/graphql/samples/location/command/track_changed_ids.rb +89 -0
  100. data/lib/eco/api/usecases/graphql/samples/location/command.rb +3 -0
  101. data/lib/eco/api/usecases/graphql/samples/location/service/base.rb +9 -0
  102. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/heading.rb +18 -0
  103. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/inputable.rb +53 -0
  104. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/parsing/classifications.rb +34 -0
  105. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/parsing/helpers.rb +28 -0
  106. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/parsing.rb +46 -0
  107. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible.rb +38 -0
  108. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff.rb +105 -0
  109. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter/discarded.rb +16 -0
  110. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter/input.rb +15 -0
  111. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter/node_attr_maps.rb +22 -0
  112. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter/parser.rb +45 -0
  113. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter.rb +36 -0
  114. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/output.rb +56 -0
  115. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list.rb +41 -0
  116. data/lib/eco/api/usecases/graphql/samples/location/service.rb +8 -0
  117. data/lib/eco/api/usecases/graphql/samples/location.rb +1 -0
  118. data/lib/eco/api/usecases/graphql/utils/sftp.rb +96 -36
  119. data/lib/eco/api/usecases/ooze_cases/export_register_case.rb +8 -6
  120. data/lib/eco/api/usecases/ooze_samples/helpers/creatable.rb +4 -3
  121. data/lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb +39 -25
  122. data/lib/eco/api/usecases/ooze_samples/helpers/exportable_register.rb +13 -15
  123. data/lib/eco/api/usecases/ooze_samples/helpers/filters.rb +50 -21
  124. data/lib/eco/api/usecases/ooze_samples/helpers/ooze_handlers.rb +21 -11
  125. data/lib/eco/api/usecases/ooze_samples/helpers/rescuable.rb +2 -0
  126. data/lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb +49 -43
  127. data/lib/eco/api/usecases/ooze_samples/ooze_base_case.rb +17 -19
  128. data/lib/eco/api/usecases/ooze_samples/register_export_case.rb +48 -43
  129. data/lib/eco/api/usecases/ooze_samples/register_update_case.rb +33 -34
  130. data/lib/eco/api/usecases/ooze_samples/target_oozes_update_case.rb +8 -10
  131. data/lib/eco/api/usecases.rb +0 -1
  132. data/lib/eco/cli/config/use_cases.rb +31 -29
  133. data/lib/eco/cli_default/workflow.rb +13 -14
  134. data/lib/eco/csv/table.rb +34 -25
  135. data/lib/eco/data/hashes/array_diff.rb +24 -35
  136. data/lib/eco/data/hashes/diff_result/meta.rb +131 -0
  137. data/lib/eco/data/hashes/diff_result.rb +65 -57
  138. data/lib/eco/data/hashes/sanke_camel_indifferent_access.rb +278 -0
  139. data/lib/eco/data/hashes.rb +1 -1
  140. data/lib/eco/data/locations/convert.rb +1 -1
  141. data/lib/eco/data/locations/node_base/csv_convert.rb +19 -9
  142. data/lib/eco/data/locations/node_base/parsing.rb +4 -2
  143. data/lib/eco/data/locations/node_base/treeify.rb +149 -132
  144. data/lib/eco/data/locations/node_base.rb +15 -4
  145. data/lib/eco/data/locations/node_diff/accessors.rb +13 -5
  146. data/lib/eco/data/locations/node_diff/nodes_diff/clustered_treeify.rb +101 -0
  147. data/lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb +99 -0
  148. data/lib/eco/data/locations/node_diff/{selectors.rb → nodes_diff/selectors.rb} +1 -1
  149. data/lib/eco/data/locations/node_diff/nodes_diff.rb +50 -35
  150. data/lib/eco/data/locations/node_diff.rb +45 -17
  151. data/lib/eco/data/locations/node_level/parsing.rb +15 -21
  152. data/lib/eco/data/locations/node_level.rb +66 -22
  153. data/lib/eco/data/locations/node_plain/parsing.rb +1 -1
  154. data/lib/eco/data/locations/node_plain.rb +60 -7
  155. data/lib/eco/data/strings/camel_case.rb +35 -0
  156. data/lib/eco/data/strings/snake_case.rb +18 -0
  157. data/lib/eco/data/strings.rb +8 -0
  158. data/lib/eco/data.rb +1 -0
  159. data/lib/eco/language/auxiliar_logger.rb +7 -5
  160. data/lib/eco/language/methods/call_detector.rb +11 -0
  161. data/lib/eco/language/methods/dsl_able.rb +7 -1
  162. data/lib/eco/language/methods.rb +2 -1
  163. data/lib/eco/language/models/collection.rb +23 -25
  164. data/lib/eco/language/models/parser_serializer.rb +24 -5
  165. data/lib/eco/version.rb +1 -1
  166. data/lib/eco-helpers.rb +0 -1
  167. metadata +52 -7
  168. data/lib/eco/data/hashes/diff_meta.rb +0 -52
@@ -2,25 +2,39 @@ module Eco
2
2
  module API
3
3
  module Common
4
4
  module People
5
- # TODO: EntryFactory should suppport multiple schemas itself (rather that being done on `Session`)
6
- # => currently, it's through session.entry_factory(schema: id), but this is wrong
7
- # => This way, Entries and PersonEntry will be able to refer to attr_map and person_parser linked to schema_id
8
- # => "schema_id" should be an optional column in the input file, or parsable via a custom parser to scope the schema
5
+ # TODO: EntryFactory should suppport multiple schemas itself
6
+ # (rather that being done on `Session`)
7
+ # => currently, it's through session.entry_factory(schema: id),
8
+ # but this is wrong
9
+ # => This way, Entries and PersonEntry will be able to refer to attr_map
10
+ # and person_parser linked to schema_id
11
+ # => "schema_id" should be an optional column in the input file,
12
+ # or parsable via a custom parser to scope the schema
9
13
  # Helper factory class to generate entries (input entries).
10
- # @attr_reader schema [Ecoportal::API::V1::PersonSchema] person schema to be used in this entry factory
14
+ # @attr_reader schema [Ecoportal::API::V1::PersonSchema] person schema to
15
+ # be used in this entry factory
11
16
  class EntryFactory < Eco::API::Common::Session::BaseSession
12
17
  include Eco::Data::Files
13
18
 
14
- attr_reader :schema, :person_parser
19
+ attr_reader :schema
15
20
 
16
- # @param e [Eco::API::Common::Session::Environment] requires a session environment, as any child of `Eco::API::Common::Session::BaseSession`
17
- # @param schema [Ecoportal::API::V1::PersonSchema] schema of person details that the parser will be based upon.
18
- # @param person_parser [nil, Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
19
- # @param attr_map [nil, Eco::Data::Mapper] attribute names mapper to translate external names into internal ones and _vice versa_.
21
+ # @param e [Eco::API::Common::Session::Environment] requires a session environment,
22
+ # as any child of `Eco::API::Common::Session::BaseSession`
23
+ # @param schema [Ecoportal::API::V1::PersonSchema] schema of person details that
24
+ # the parser will be based upon.
25
+ # @param person_parser [nil, Eco::API::Common::People::PersonParser]
26
+ # set of attribute, type and format parsers/serializers.
27
+ # @param attr_map [nil, Eco::Data::Mapper] attribute names mapper
28
+ # to translate external names into internal ones and _vice versa_.
20
29
  def initialize(e, schema:, person_parser: nil, default_parser: nil, attr_map: nil)
21
- fatal "Constructor needs a PersonSchema. Given: #{schema}" if !schema.is_a?(Ecoportal::API::V1::PersonSchema)
22
- fatal "Expecting PersonParser. Given: #{person_parser}" if person_parser && !person_parser.is_a?(Eco::API::Common::People::PersonParser)
23
- fatal "Expecting Mapper object. Given: #{fields_mapper}" if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
30
+ msg = "Constructor needs a PersonSchema. Given: #{schema.class}"
31
+ fatal msg unless schema.is_a?(Ecoportal::API::V1::PersonSchema)
32
+
33
+ msg = "Expecting PersonParser. Given: #{person_parser.class}"
34
+ fatal msg if person_parser && !person_parser.is_a?(Eco::API::Common::People::PersonParser)
35
+
36
+ msg = "Expecting Mapper object. Given: #{attr_map.class}"
37
+ fatal msg if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
24
38
  super(e)
25
39
 
26
40
  @schema = Ecoportal::API::V1::PersonSchema.new(JSON.parse(schema.doc.to_json))
@@ -35,17 +49,16 @@ module Eco
35
49
  @attr_map = attr_map
36
50
  end
37
51
 
38
- def newFactory(schema: nil)
52
+ def newFactory(schema: nil) # rubocop:disable Naming/MethodName
39
53
  self.class.new(
40
54
  environment,
41
- schema: schema,
42
- person_parser: @source_person_parser,
55
+ schema: schema,
56
+ person_parser: @source_person_parser,
43
57
  default_parser: @default_parser,
44
- attr_map: @attr_map
58
+ attr_map: @attr_map
45
59
  )
46
60
  end
47
61
 
48
-
49
62
  # provides with a Eco::API::Common::People::PersonParser object (collection of attribute parsers)
50
63
  # @note if the custom person parser has changed, it updates the copy of this EntryFactory instance
51
64
  # @return [Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
@@ -57,9 +70,12 @@ module Eco
57
70
  @person_parser
58
71
  end
59
72
 
60
- # key method to generate objects of `PersonEntry` that share dependencies via this `EntryFactory` environment.
61
- # @note this method is necessary to make the factory object work as a if it was a class `PersonEntry` you can call `new` on.
62
- # @param data [Hash, Person] data to be parsed/serialized. Parsed: the external hashed entry. Serialized: a Person object.
73
+ # key method to generate objects of `PersonEntry` that share dependencies
74
+ # via this `EntryFactory` environment.
75
+ # @note this method is necessary to make the factory object work
76
+ # as a if it was a class `PersonEntry` you can call `new` on.
77
+ # @param data [Hash, Person] data to be parsed/serialized.
78
+ # Parsed: the external hashed entry. Serialized: a Person object.
63
79
  # @return [Eco::API::Common::People::PersonEntry]
64
80
  def new(data, dependencies: {})
65
81
  PersonEntry.new(
@@ -71,7 +87,8 @@ module Eco
71
87
  )
72
88
  end
73
89
 
74
- # Helper that provides a collection of `Entries`, which in turn provides with further helpers to find and exclude entries.
90
+ # Helper that provides a collection of `Entries`, which in turn provides
91
+ # with further helpers to find and exclude entries.
75
92
  # It accepts a `file:` and `format:` or `data:` but not both options together.
76
93
  # @raise Exception
77
94
  # - if you try to provide `data:` and `file:` at the same time.
@@ -85,11 +102,18 @@ module Eco
85
102
  # @option options [String] :encoding optional parameter to read `file:` by expecting certain encoding.
86
103
  # @option options [Boolean] :check_headers signals if the `csv` file headers should be expected.
87
104
  # @return [Eco::API::Common::People::Entries] collection of `Eco::API::Common::People::PersonEntry`.
88
- def entries(data: (no_data = true; nil), file: (no_file = true; nil), format: (no_format = true; nil), **options)
89
- fatal("You should at least use data: or file:, but not both") if no_data == no_file
90
- fatal("You must specify a valid format: (symbol) when you use file.") if file && no_format
91
- fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
92
- fatal("There is no parser/serializer for format ':#{format.to_s}'") unless no_format || @person_parser.defined?(format)
105
+ def entries(data: (no_data = true; nil), file: (no_file = true; nil), format: (no_format = true; nil), **options) # rubocop:disable Style/Semicolon
106
+ msg = "You should at least use data: or file:, but not both"
107
+ fatal msg if no_data == no_file
108
+
109
+ msg = "You must specify a valid format: (symbol) when you use file."
110
+ fatal msg if file && no_format
111
+
112
+ msg = "Format should be a Symbol. Given '#{format}'"
113
+ fatal msg if format && !format.is_a?(Symbol)
114
+
115
+ msg = "There is no parser/serializer for format ':#{format}'"
116
+ fatal msg unless no_format || @person_parser.defined?(format)
93
117
 
94
118
  options.merge!(content: data) unless no_data
95
119
  options.merge!(file: file) unless no_file
@@ -98,8 +122,7 @@ module Eco
98
122
  Entries.new(to_array_of_hashes(**options), klass: PersonEntry, factory: self)
99
123
  end
100
124
 
101
- def to_array_of_hashes(**kargs)
102
- data = []
125
+ def to_array_of_hashes(**kargs) # rubocop:disable Metrics/AbcSize
103
126
  content, file, encoding, format = kargs.values_at(:content, :file, :encoding, :format)
104
127
 
105
128
  # Support for multiple file
@@ -112,7 +135,7 @@ module Eco
112
135
  end
113
136
  # Get content only when it's not :xls
114
137
  # note: even if content was provided, file takes precedence
115
- if (format != :xls) && file
138
+ if (format != :xls) && file # rubocop:disable Style/IfUnlessModifier
116
139
  content = get_file_content(file, encoding: encoding)
117
140
  end
118
141
 
@@ -156,34 +179,42 @@ module Eco
156
179
  # - if there is no _parser/serializer_ defined for `format:`.
157
180
  # @param data [Eco::API::Organization::People] data to be parsed.
158
181
  # @param file [String] absolute or relative path to the ouput file.
159
- # @param format [Symbol] it specifies the format of the output `file:` (i.e. `:xml`, `:csv`). There must be a parser/serializer defined for it.
182
+ # @param format [Symbol] it specifies the format of the output `file:` (i.e. `:xml`, `:csv`).
183
+ # There must be a parser/serializer defined for it.
160
184
  # @param encoding [String] optional parameter to geneate `file:` content by unsing certain encoding.
161
185
  # @return [Void].
162
186
  def export(data:, file: "export", format: :csv, encoding: "utf-8", internal_names: false)
163
- fatal("data: Expected Eco::API::Organization::People object. Given: #{data.class}") unless data.is_a?(Eco::API::Organization::People)
164
- fatal("A file should be specified.") unless !file.to_s.strip.empty?
165
- fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
166
- fatal("There is no parser/serializer for format ':#{format.to_s}'") unless @person_parser.defined?(format)
187
+ msg = "data: Expected Eco::API::Organization::People object. Given: #{data.class}"
188
+ fatal msg unless data.is_a?(Eco::API::Organization::People)
189
+
190
+ fatal "A file should be specified." if file.to_s.strip.empty?
191
+ fatal "Format should be a Symbol. Given '#{format}'" if format && !format.is_a?(Symbol)
192
+
193
+ msg = "There is no parser/serializer for format ':#{format}'"
194
+ fatal msg unless @person_parser.defined?(format)
167
195
 
168
196
  run = true
169
197
  if self.class.file_exists?(file)
170
- prompt_user("Do you want to overwrite it? (Y/n):", explanation: "The file '#{file}' already exists.", default: "Y") do |response|
198
+ prompt_user(
199
+ "Do you want to overwrite it? (Y/n):",
200
+ explanation: "The file '#{file}' already exists.",
201
+ default: "Y"
202
+ ) do |response|
171
203
  run = (response == "") || response.upcase.start_with?("Y")
172
204
  end
173
205
  end
174
206
 
175
- if run
176
- deps = {"supervisor_id" => {people: data}}
207
+ return unless run
177
208
 
178
- data_entries = data.map do |person|
179
- self.new(person, dependencies: deps).yield_self do |entry|
180
- internal_names ? entry.mapped_entry : entry.external_entry
181
- end
209
+ deps = {"supervisor_id" => {people: data}}
210
+ data_entries = data.map do |person|
211
+ new(person, dependencies: deps).then do |entry|
212
+ internal_names ? entry.mapped_entry : entry.external_entry
182
213
  end
214
+ end
183
215
 
184
- File.open(file, "w", enconding: encoding) do |fd|
185
- fd.write(person_parser.serialize(format, data_entries))
186
- end
216
+ File.open(file, "w", enconding: encoding) do |fd|
217
+ fd.write(person_parser.serialize(format, data_entries))
187
218
  end
188
219
  end
189
220
 
@@ -2,10 +2,8 @@ module Eco
2
2
  module API
3
3
  module Common
4
4
  module People
5
-
6
5
  # Class to define a parser/serializer.
7
6
  class PersonAttributeParser < Eco::Language::Models::ParserSerializer
8
-
9
7
  # @note
10
8
  # - This was introduced at a later stage and might not be available for certain org-parsers configs
11
9
  # @return [RequiredAttrs]
@@ -51,10 +49,9 @@ module Eco
51
49
  (phase == :any) || serializer_category?(phase)
52
50
  end
53
51
 
54
-
55
52
  # Helper to build the `active_when` condition.
56
53
  def active_when_any?(*attrs)
57
- Proc.new do |source_data|
54
+ proc do |source_data|
58
55
  keys = data_keys(source_data)
59
56
  attrs.any? {|key| keys.include?(key)}
60
57
  end
@@ -62,7 +59,7 @@ module Eco
62
59
 
63
60
  # Helper to build the `active_when` condition.
64
61
  def active_when_all?(*attrs)
65
- Proc.new do |source_data|
62
+ proc do |source_data|
66
63
  keys = data_keys(source_data)
67
64
  attrs.all? {|key| keys.include?(key)}
68
65
  end
@@ -73,9 +70,9 @@ module Eco
73
70
  # By default, an attribute paraser is active if in the entry to parse
74
71
  # the internal attribute is present
75
72
  def attribute_present(active_when)
76
- Proc.new do |source_data|
77
- data_keys(source_data).include?(self.attr) ||
78
- (active_when && active_when.call(source_data))
73
+ proc do |source_data|
74
+ data_keys(source_data).include?(self.attr) || # rubocop:disable Style/RedundantSelf
75
+ active_when&.call(source_data)
79
76
  end
80
77
  end
81
78
 
@@ -85,14 +82,13 @@ module Eco
85
82
  def data_keys(source_data)
86
83
  case source_data
87
84
  when Array
88
- keys = source_data
85
+ source_data
89
86
  when Hash
90
- keys = source_data.keys
87
+ source_data.keys
91
88
  else
92
- keys = []
89
+ []
93
90
  end
94
91
  end
95
-
96
92
  end
97
93
  end
98
94
  end
@@ -10,21 +10,34 @@ module Eco
10
10
  # - if `data` is a `Person` object, its behaviour is `serialise`.
11
11
  # - if `data` is **not** a `Person` object, it does a `parse`.
12
12
  # - currently **in rework**, so there may be subtle differences that make it temporarily unstable (yet it is reliable).
13
- # @param data [Hash, Ecoportal::API::V1::Person] `Person` object to be serialized or hashed entry (`CSV::Row` is accepted).
14
- # @param person_parser [Common::People::PersonParser] parser/serializer of person attributes (it contains a set of attribute parsers).
15
- # @param attr_map [Eco::Data::Mapper] mapper to translate attribute names from _external_ to _internal_ names and _vice versa_.
16
- # @param dependencies [Hash] hash where _keys_ are internal attribute names. It is mostly used to deliver final dependencies to attribute parsers/serializers.
17
- # @param logger [Common::Session::Logger, ::Logger] object to manage logs.
13
+ # @param data [Hash, Ecoportal::API::V1::Person] `Person` object
14
+ # to be serialized or hashed entry (`CSV::Row` is accepted).
15
+ # @param person_parser [Common::People::PersonParser] parser/serializer
16
+ # of person attributes (it contains a set of attribute parsers).
17
+ # @param attr_map [Eco::Data::Mapper] mapper to translate attribute
18
+ # names from _external_ to _internal_ names and _vice versa_.
19
+ # @param dependencies [Hash] hash where _keys_ are internal attribute
20
+ # names. It is mostly used to deliver final dependencies
21
+ # to attribute parsers/serializers.
22
+ # @param logger [Common::Session::Logger, ::Logger] object to managelogs.
18
23
  def initialize(data, person_parser:, attr_map:, dependencies: {}, logger: ::Logger.new(IO::NULL))
19
- raise "Constructor needs a PersonParser. Given: #{parser}" if !person_parser.is_a?(Eco::API::Common::People::PersonParser)
20
- raise "Expecting Mapper object. Given: #{attr_map}" if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
21
-
22
- @source = data
23
- @person_parser = person_parser
24
- @deps = dependencies
25
- @logger = logger
26
- @attr_map = attr_map
27
- @emap = PersonEntryAttributeMapper.new(@source, person_parser: @person_parser, attr_map: @attr_map, logger: @logger)
24
+ msg = "Constructor needs a PersonParser. Given: #{person_parser.class}"
25
+ raise msg unless person_parser.is_a?(Eco::API::Common::People::PersonParser)
26
+
27
+ msg = "Expecting Mapper object. Given: #{attr_map.class}"
28
+ raise msg if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
29
+
30
+ @source = data
31
+ @person_parser = person_parser
32
+ @deps = dependencies
33
+ @logger = logger
34
+ @attr_map = attr_map
35
+ @emap = PersonEntryAttributeMapper.new(
36
+ @source,
37
+ person_parser: @person_parser,
38
+ attr_map: @attr_map,
39
+ logger: @logger
40
+ )
28
41
 
29
42
  if parsing?
30
43
  @external_entry = __external_entry(data)
@@ -39,35 +52,41 @@ module Eco
39
52
  @external_entry = __external_entry(@mapped_entry)
40
53
  end
41
54
 
42
- (print_models; exit(1)) if DEBUG
55
+ (print_models; exit(1)) if DEBUG # rubocop:disable Style/Semicolon
43
56
  end
44
57
 
45
58
  # Generates a new entry
46
59
  # @return [PersonEntry]
47
60
  def new(data)
48
- self.class.new(data, person_parser: @person_parser, attr_map: @attr_map, dependencies: @deps, logger: @logger)
61
+ self.class.new(
62
+ data,
63
+ person_parser: @person_parser,
64
+ attr_map: @attr_map,
65
+ dependencies: @deps,
66
+ logger: @logger
67
+ )
49
68
  end
50
69
 
51
70
  # @note completely serialized entry.
52
71
  # @return [Hash] entry `Hash` with **external** attribute names, and values and types thereof.
53
- def external_entry
72
+ def external_entry # rubocop:disable Style/TrivialAccessors
54
73
  @external_entry
55
74
  end
56
75
 
57
76
  # @note just one step away from being completely parsed (only types parsing pending).
58
77
  # @return [Hash] entry `Hash` with **internal** attribute names and values, but **external** types.
59
- def internal_entry
78
+ def internal_entry # rubocop:disable Style/TrivialAccessors
60
79
  @internal_entry
61
80
  end
62
81
 
63
82
  # @return [Hash] entry `Hash` with **internal** attribute names, but **external** types and values.
64
- def mapped_entry
83
+ def mapped_entry # rubocop:disable Style/TrivialAccessors
65
84
  @mapped_entry
66
85
  end
67
86
 
68
87
  # @note values ready to be set to a person.
69
88
  # @return [Hash] entry `Hash` with **internal** attribute names, values and types.
70
- def final_entry
89
+ def final_entry # rubocop:disable Style/TrivialAccessors
71
90
  @final_entry
72
91
  end
73
92
 
@@ -173,8 +192,7 @@ module Eco
173
192
  # @return [String] string summary of this person identity.
174
193
  def to_s(options)
175
194
  options = into_a(options)
176
- case
177
- when options.include?(:identify)
195
+ if options.include?(:identify)
178
196
  identify
179
197
  else
180
198
  final_entry.each.map do |k, v|
@@ -193,16 +211,11 @@ module Eco
193
211
  def set_core(person, exclude: nil)
194
212
  scoped_attrs = @emap.core_attrs(@final_entry) - into_a(exclude)
195
213
  @final_entry.slice(*scoped_attrs).each do |attr, value|
196
- begin
197
- set_part(person, attr, value)
198
- rescue Exception => e
199
- if attr == "email"
200
- logger.error(e.to_s + " - setting blank email instead.")
201
- set_part(person, attr, nil)
202
- else
203
- raise
204
- end
205
- end
214
+ set_part(person, attr, value)
215
+ rescue StandardError => e
216
+ raise unless attr == "email"
217
+ logger.error("#{e} - setting blank email instead.")
218
+ set_part(person, attr, nil)
206
219
  end
207
220
  end
208
221
 
@@ -212,7 +225,7 @@ module Eco
212
225
  # @param person [Ecoportal::API::Internal::Person] the person we want to set the account values to.
213
226
  # @param exclude [String, Array<String>] account properties that should not be set/changed to the person.
214
227
  def set_account(person, exclude: nil)
215
- person.account = {} if !person.account
228
+ person.account = {} unless person.account
216
229
  scoped_attrs = @emap.account_attrs(@final_entry) - into_a(exclude)
217
230
  @final_entry.slice(*scoped_attrs).each do |attr, value|
218
231
  set_part(person.account, attr, value)
@@ -316,7 +329,7 @@ module Eco
316
329
  core_account_hash = core_account.reduce({}) do |hash, attr|
317
330
  hash.merge(hash_attr(attr, _serialize_type(attr, final_entry[attr])))
318
331
  end
319
- details_hash = @person_parser.target_attrs_details.reduce({}) do |hash, attr|
332
+ details_hash = @person_parser.target_attrs_details.reduce({}) do |hash, attr|
320
333
  hash.merge(hash_attr(attr, _serialize_type(attr, final_entry[attr], schema: @person_parser.schema)))
321
334
  end
322
335
  merging(core_account_hash, details_hash) do |internal_entry|
@@ -326,12 +339,13 @@ module Eco
326
339
 
327
340
  # Parsing helper where attributes with custom parsers are already parsed, but
328
341
  # it finishes to parse the types (i.e. to `Array` if `multiple`)
329
- # @param internal_entry [Hash] the entry with the **internal** _attribute_ names and values but the **external** types.
342
+ # @param internal_entry [Hash] the entry with the **internal** _attribute_ names and values
343
+ # but the **external** types.
330
344
  # @return [Hash] the `parsed entry` with the **internal** final attributes names, values and types.
331
345
  def _final_parsing(internal_entry)
332
346
  core_account_attrs = @emap.account_attrs(internal_entry) + @emap.core_attrs(internal_entry)
333
347
  core_account_hash = internal_entry.slice(*core_account_attrs).each_with_object({}) do |(attr, value), hash|
334
- hash[attr] = _parse_type(attr, value)
348
+ hash[attr] = _parse_type(attr, value)
335
349
  end
336
350
 
337
351
  details_attrs = @emap.details_attrs(internal_entry)
@@ -354,7 +368,7 @@ module Eco
354
368
  # @param person [Ecoportal::API::V1::Person] the `Person` object to transform into a _parsed entry_.
355
369
  # @return [Hash] the `parsed entry` with the **internal** attributes names and internal typed values.
356
370
  def _final_serializing(person)
357
- core_hash = @person_parser.target_attrs_core.reduce({}) do |hash, attr|
371
+ core_hash = @person_parser.target_attrs_core.reduce({}) do |hash, attr|
358
372
  hash.merge(hash_attr(attr, get_part(person, attr)))
359
373
  end
360
374
  account_hash = @person_parser.target_attrs_account.reduce({}) do |hash, attr|
@@ -364,7 +378,7 @@ module Eco
364
378
  hash.merge(hash_attr(attr, get_part(person.details, attr)))
365
379
  end
366
380
  merging(core_hash, account_hash, details_hash) do |final_entry|
367
- final_entry["Has account?"] = !!person.account
381
+ final_entry["Has account?"] = !person.account.nil?
368
382
  final_entry.merge(_serialize_values(person, :person))
369
383
  end
370
384
  end
@@ -387,9 +401,8 @@ module Eco
387
401
 
388
402
  # Transforms each **typed** value into its `String` version
389
403
  def _serialize_type(attr, value, schema: nil)
390
- case
391
- when !!schema
392
- unless field = schema[attr]
404
+ if !!schema
405
+ unless (field = schema[attr])
393
406
  fatal("Field '#{attr}' does not exist in details of schema: '#{schema.name}'")
394
407
  end
395
408
  value = @person_parser.serialize(:multiple, value) if field.multiple
@@ -397,11 +410,11 @@ module Eco
397
410
  value = @person_parser.serialize(field.type.to_sym, value, deps: {"attr" => attr})
398
411
  end
399
412
  value
400
- when ["policy_group_ids", "filter_tags", "login_provider_ids", "starred_ids"].include?(attr)
413
+ elsif %w[policy_group_ids filter_tags login_provider_ids starred_ids].include?(attr)
401
414
  @person_parser.serialize(:multiple, value)
402
- when ["freemium", "accept_eula"].include?(attr)
415
+ elsif %w[freemium accept_eula].include?(attr)
403
416
  @person_parser.serialize(:boolean, value)
404
- when ["subordinates"].include?(attr)
417
+ elsif ["subordinates"].include?(attr)
405
418
  @person_parser.serialize(:number, value)
406
419
  else
407
420
  value
@@ -409,12 +422,11 @@ module Eco
409
422
  end
410
423
 
411
424
  # Transforms each `String` value into its **typed** version
412
- def _parse_type(attr, value, schema: nil)
425
+ def _parse_type(attr, value, schema: nil) # rubocop:disable Metrics/AbcSize
413
426
  value = value.strip if value.is_a?(String)
414
427
  value = nil if value.to_s.strip.empty?
415
- case
416
- when !!schema
417
- unless field = schema[attr]
428
+ if !!schema
429
+ unless (field = schema[attr])
418
430
  fatal("Field '#{attr}' does not exist in details of schema: '#{schema.name}'")
419
431
  end
420
432
  value = @person_parser.parse(:multiple, value) if field.multiple
@@ -423,16 +435,16 @@ module Eco
423
435
  value = @person_parser.parse(field.type.to_sym, value, deps: {"attr" => attr})
424
436
  end
425
437
  value
426
- when attr == "email"
438
+ elsif attr == "email"
427
439
  value = value.strip.downcase if value
428
440
  value
429
- when ["policy_group_ids", "filter_tags", "login_provider_ids", "starred_ids"].include?(attr)
441
+ elsif %w[policy_group_ids filter_tags login_provider_ids starred_ids].include?(attr)
430
442
  value = @person_parser.parse(:multiple, value)
431
- value = (attr == "filter_tags")? value.compact.map(&:upcase) : value
443
+ value = value.compact.map(&:upcase) if attr == "filter_tags"
432
444
  value
433
- when ["freemium", "accept_eula"].include?(attr)
445
+ elsif %w[freemium accept_eula].include?(attr)
434
446
  @person_parser.parse(:boolean, value)
435
- when ["subordinates"].include?(attr)
447
+ elsif ["subordinates"].include?(attr)
436
448
  @person_parser.parse(:number, value)
437
449
  else
438
450
  value
@@ -442,12 +454,12 @@ module Eco
442
454
  # Merges multiple hashes giving overriding perference to the first ones.
443
455
  # @return [Hash] with well sorted keys, as they came in the order of the input hashes.
444
456
  def merging(*hashes)
445
- sorted_keys = hashes.map {|h| h.keys}.flatten.uniq
457
+ sorted_keys = hashes.map(&:keys).flatten.uniq
446
458
  rev_hash = hashes.reverse.each_with_object({}) {|h, out| out.merge!(h)}
447
459
  merged = sorted_keys.each_with_object({}) do |k, h|
448
460
  h[k] = rev_hash[k]
449
461
  end
450
- merged = yield(merged) if block_given?
462
+ merged = yield(merged) if block_given?
451
463
  merged
452
464
  end
453
465
 
@@ -458,7 +470,7 @@ module Eco
458
470
  end
459
471
 
460
472
  def into_a(value)
461
- value = [] if value == nil
473
+ value = [] if value.nil?
462
474
  value = [].push(value) unless value.is_a?(Array)
463
475
  value
464
476
  end
@@ -478,20 +490,19 @@ module Eco
478
490
 
479
491
  def set_part(obj, attr, value)
480
492
  return unless obj
481
- begin
482
- case obj
483
- when Ecoportal::API::V1::PersonDetails
484
- unless field = obj.get_field(attr)
485
- fatal("Field '#{attr}' does not exist in details of schema: '#{obj.schema_id}'")
486
- end
487
- obj[attr] = value
488
- else
489
- obj.send("#{attr}=", value)
490
- end
491
- rescue Exception => e
492
- # add more info to the error
493
- raise e.append_message " -- Entry #{to_s(:identify)}"
493
+
494
+ case obj
495
+ when Ecoportal::API::V1::PersonDetails
496
+ msg = "Field '#{attr}' does not exist in details of schema: '#{obj.schema_id}'"
497
+ fatal msg unless obj.get_field(attr)
498
+
499
+ obj[attr] = value
500
+ else
501
+ obj.send("#{attr}=", value)
494
502
  end
503
+ rescue StandardError => e
504
+ # add more info to the error
505
+ raise e.append_message " -- Entry #{to_s(:identify)}"
495
506
  end
496
507
 
497
508
  # @return [Hash] `value` if it was a `Hash`, and `{ attr => value}` otherwise
@@ -512,19 +523,19 @@ module Eco
512
523
 
513
524
  # Function to debug faste
514
525
  def print_models
515
- print_it = Proc.new do |name, model|
526
+ print_it = proc do |name, model|
516
527
  puts "#{name}:"
517
528
  pp model
518
529
  puts "*" * 30
519
530
  end
520
531
 
521
- fin = Proc.new {|x_| print_it.call("final_entry", @final_entry) }
522
- int = Proc.new {|x_| print_it.call("internal_entry", @internal_entry) }
523
- mad = Proc.new {|x_| print_it.call("mapped_entry", @mapped_entry) }
524
- ext = Proc.new {|x_| print_it.call("external_entry", @external_entry) }
532
+ fin = proc { print_it.call("final_entry", @final_entry) }
533
+ int = proc { print_it.call("internal_entry", @internal_entry) }
534
+ mad = proc { print_it.call("mapped_entry", @mapped_entry) }
535
+ ext = proc { print_it.call("external_entry", @external_entry) }
525
536
 
526
537
  call_order = parsing? ? [ext, mad, int, fin] : [fin, int, mad, ext]
527
- call_order.each {|proc| proc.call}
538
+ call_order.each(&:call)
528
539
  end
529
540
  end
530
541
  end