eco-helpers 1.4.0 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +150 -2
  3. data/LICENSE +21 -0
  4. data/eco-helpers.gemspec +11 -10
  5. data/lib/eco/api.rb +3 -0
  6. data/lib/eco/api/common.rb +5 -1
  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 +5 -10
  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 +20 -0
  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 +31 -0
  26. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +15 -24
  27. data/lib/eco/api/common/people/entries.rb +54 -24
  28. data/lib/eco/api/common/people/entry_factory.rb +18 -15
  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 +5 -2
  32. data/lib/eco/api/common/people/person_parser.rb +52 -19
  33. data/lib/eco/api/common/session/base_session.rb +3 -6
  34. data/lib/eco/api/common/session/environment.rb +2 -23
  35. data/lib/eco/api/common/session/logger.rb +4 -0
  36. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +2 -0
  37. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +9 -1
  38. data/lib/eco/api/common/version_patches/exception.rb +24 -2
  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 +35 -0
  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 +20 -0
  51. data/lib/eco/api/microcases/fix_default_group.rb +34 -0
  52. data/lib/eco/api/microcases/fix_filter_tags.rb +42 -0
  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 +20 -0
  61. data/lib/eco/api/microcases/set_core.rb +18 -0
  62. data/lib/eco/api/microcases/set_core_with_supervisor.rb +23 -0
  63. data/lib/eco/api/microcases/set_supervisor.rb +30 -0
  64. data/lib/eco/api/microcases/strict_search.rb +19 -0
  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/organization/presets_reference.json +9 -1
  74. data/lib/eco/api/organization/presets_values.json +4 -1
  75. data/lib/eco/api/policies.rb +11 -7
  76. data/lib/eco/api/session.rb +62 -29
  77. data/lib/eco/api/session/batch.rb +2 -45
  78. data/lib/eco/api/session/batch/base_policy.rb +7 -6
  79. data/lib/eco/api/session/batch/errors.rb +28 -4
  80. data/lib/eco/api/session/batch/feedback.rb +7 -1
  81. data/lib/eco/api/session/batch/job.rb +40 -23
  82. data/lib/eco/api/session/batch/jobs.rb +9 -4
  83. data/lib/eco/api/session/batch/jobs_groups.rb +1 -1
  84. data/lib/eco/api/session/batch/request_stats.rb +91 -58
  85. data/lib/eco/api/session/batch/status.rb +35 -31
  86. data/lib/eco/api/session/config.rb +106 -44
  87. data/lib/eco/api/session/config/api.rb +132 -7
  88. data/lib/eco/api/session/config/apis.rb +24 -25
  89. data/lib/eco/api/session/config/logger.rb +2 -2
  90. data/lib/eco/api/session/config/post_launch.rb +1 -1
  91. data/lib/eco/api/session/config/workflow.rb +8 -7
  92. data/lib/eco/api/usecases.rb +47 -33
  93. data/lib/eco/api/usecases/backup/append_usergroups_case.rb +36 -0
  94. data/lib/eco/api/usecases/backup/create_case.rb +104 -0
  95. data/lib/eco/api/usecases/backup/create_details_case.rb +31 -0
  96. data/lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb +48 -0
  97. data/lib/eco/api/usecases/backup/hris_case.rb +124 -0
  98. data/lib/eco/api/usecases/backup/set_default_tag_case.rb +49 -0
  99. data/lib/eco/api/usecases/backup/set_supervisor_case.rb +41 -0
  100. data/lib/eco/api/usecases/backup/transfer_account_case.rb +90 -0
  101. data/lib/eco/api/usecases/backup/update_case.rb +112 -0
  102. data/lib/eco/api/usecases/backup/update_details_case.rb +64 -0
  103. data/lib/eco/api/usecases/backup/upsert_case.rb +114 -0
  104. data/lib/eco/api/usecases/base_case.rb +2 -0
  105. data/lib/eco/api/usecases/base_io.rb +3 -3
  106. data/lib/eco/api/usecases/default_cases.rb +23 -53
  107. data/lib/eco/api/usecases/default_cases/append_usergroups_case.rb +10 -31
  108. data/lib/eco/api/usecases/default_cases/change_email_case.rb +23 -47
  109. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +56 -43
  110. data/lib/eco/api/usecases/default_cases/create_case.rb +15 -101
  111. data/lib/eco/api/usecases/default_cases/create_details_case.rb +11 -26
  112. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +12 -43
  113. data/lib/eco/api/usecases/default_cases/delete_sync_case.rb +11 -0
  114. data/lib/eco/api/usecases/default_cases/delete_trans_case.rb +14 -0
  115. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +10 -21
  116. data/lib/eco/api/usecases/default_cases/hris_case.rb +23 -120
  117. data/lib/eco/api/usecases/default_cases/new_email_case.rb +10 -23
  118. data/lib/eco/api/usecases/default_cases/new_id_case.rb +11 -25
  119. data/lib/eco/api/usecases/default_cases/new_id_case0.rb +14 -0
  120. data/lib/eco/api/usecases/default_cases/org_data_convert_case.rb +83 -0
  121. data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +30 -0
  122. data/lib/eco/api/usecases/default_cases/refresh_case.rb +7 -20
  123. data/lib/eco/api/usecases/default_cases/reinvite_sync_case.rb +11 -0
  124. data/lib/eco/api/usecases/default_cases/reinvite_trans_case.rb +17 -0
  125. data/lib/eco/api/usecases/default_cases/remove_account_sync_case.rb +11 -0
  126. data/lib/eco/api/usecases/default_cases/remove_account_trans_case.rb +17 -0
  127. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +9 -19
  128. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +92 -0
  129. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +32 -40
  130. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +15 -33
  131. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +66 -57
  132. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -44
  133. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +40 -55
  134. data/lib/eco/api/usecases/default_cases/transfer_account_case.rb +264 -84
  135. data/lib/eco/api/usecases/default_cases/update_case.rb +15 -109
  136. data/lib/eco/api/usecases/default_cases/update_details_case.rb +14 -61
  137. data/lib/eco/api/usecases/default_cases/upsert_case.rb +16 -111
  138. data/lib/eco/api/usecases/use_case_io.rb +9 -9
  139. data/lib/eco/cli/config.rb +10 -2
  140. data/lib/eco/cli/config/default.rb +2 -1
  141. data/lib/eco/cli/config/default/input_filters.rb +58 -0
  142. data/lib/eco/cli/config/default/options.rb +60 -25
  143. data/lib/eco/cli/config/default/people.rb +4 -4
  144. data/lib/eco/cli/config/default/people_filters.rb +108 -0
  145. data/lib/eco/cli/config/default/usecases.rb +69 -32
  146. data/lib/eco/cli/config/default/workflow.rb +37 -27
  147. data/lib/eco/cli/config/filters.rb +50 -0
  148. data/lib/eco/cli/config/filters/input_filters.rb +29 -0
  149. data/lib/eco/cli/config/filters/people_filters.rb +29 -0
  150. data/lib/eco/cli/config/help.rb +49 -0
  151. data/lib/eco/cli/config/options_set.rb +17 -1
  152. data/lib/eco/cli/config/use_cases.rb +79 -53
  153. data/lib/eco/cli/scripting.rb +10 -2
  154. data/lib/eco/cli/scripting/args_helpers.rb +25 -15
  155. data/lib/eco/cli/scripting/argument.rb +1 -0
  156. data/lib/eco/cli/scripting/arguments.rb +1 -1
  157. data/lib/eco/csv.rb +8 -3
  158. data/lib/eco/csv/table.rb +1 -1
  159. data/lib/eco/data/crypto/encryption.rb +3 -0
  160. data/lib/eco/data/files/helpers.rb +6 -1
  161. data/lib/eco/language/match.rb +19 -9
  162. data/lib/eco/language/match_modifier.rb +13 -5
  163. data/lib/eco/language/models/collection.rb +77 -56
  164. data/lib/eco/language/models/parser_serializer.rb +39 -15
  165. data/lib/eco/version.rb +1 -1
  166. metadata +149 -63
  167. data/lib/eco/api/session/task.rb +0 -175
  168. data/lib/eco/api/usecases/default_case.rb +0 -19
  169. data/lib/eco/api/usecases/default_cases/delete_case.rb +0 -32
  170. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +0 -98
  171. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +0 -26
  172. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +0 -41
  173. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +0 -38
  174. data/lib/eco/cli/config/default/filters.rb +0 -70
  175. data/lib/eco/cli/config/people_filters.rb +0 -38
@@ -0,0 +1,20 @@
1
+ class Eco::API::Common::People::DefaultParsers::FreemiumParser < Eco::API::Common::Loaders::Parser
2
+ attribute "freemium"
3
+
4
+ def parser(hash, deps)
5
+ value = hash["freemium"]
6
+ value = value.first if value.is_a?(Array)
7
+ truthy(value)
8
+ end
9
+
10
+ def serializer(person, deps)
11
+ person.freemium&.to_s
12
+ end
13
+
14
+ private
15
+
16
+ def truthy (value)
17
+ %w[true y yes x].include?(value.to_s&.downcase)
18
+ end
19
+
20
+ end
@@ -0,0 +1,26 @@
1
+ class Eco::API::Common::People::DefaultParsers::LoginProvidersParser < Eco::API::Common::Loaders::Parser
2
+ attribute "login_provider_ids"
3
+ parsing_phase :final
4
+ serializing_phase :final
5
+
6
+ def parser(hash, deps)
7
+ hash["login_provider_ids"].map do |name|
8
+ login_providers.to_id(name&.downcase.strip)
9
+ end.compact
10
+ end
11
+
12
+ def serializer(hash, deps)
13
+ if ids = hash["login_provider_ids"]
14
+ login_providers.to_name(ids)
15
+ else
16
+ []
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def login_providers
23
+ @login_providers ||= ASSETS.config.login_providers
24
+ end
25
+
26
+ end
@@ -1,33 +1,21 @@
1
- module Eco
2
- module API
3
- module Common
4
- module People
5
- class DefaultParsers
6
- class MultiParser < People::BaseParser
1
+ class Eco::API::Common::People::DefaultParsers::MultiParser < Eco::API::Common::Loaders::Parser
2
+ attribute :multiple
7
3
 
8
- def process
9
- @parsers.define_attribute(:multiple) do |parser|
10
- parser.def_parser do |value|
11
- next value if value.is_a?(Array)
12
- value = value.split("|") if value&.include?("|")
13
- into_a(value)
14
- end.def_serializer do |value|
15
- into_a(value).join("|")
16
- end
17
- end
18
- end
4
+ def parser(value, deps)
5
+ into_a(value)
6
+ end
19
7
 
20
- private
8
+ def serializer(value, deps)
9
+ into_a(value).join("|")
10
+ end
21
11
 
22
- def into_a(value)
23
- value = [] if value == nil
24
- value = [].push(value) unless value.is_a?(Array)
25
- value
26
- end
12
+ private
27
13
 
28
- end
29
- end
30
- end
31
- end
14
+ def into_a(value)
15
+ return [] if value.nil?
16
+ return value if value.is_a?(Array)
17
+ return value.split("|") if value.is_a?(String) && value.include?("|")
18
+ [].push(value)
32
19
  end
20
+
33
21
  end
@@ -1,23 +1,18 @@
1
- module Eco
2
- module API
3
- module Common
4
- module People
5
- class DefaultParsers
6
- class NumericParser < People::BaseParser
1
+ class Eco::API::Common::People::DefaultParsers::NumericParser < Eco::API::Common::Loaders::Parser
2
+ attribute :number
7
3
 
8
- def process
9
- @parsers.define_attribute(:number, dependencies: @options) do |parser|
10
- parser.def_parser do |value, deps|
11
- value = value.to_s.strip.empty? ? nil : (value.to_f rescue nil)
12
- end.def_serializer do |value|
13
- value.is_a?(Array) ? value.map { |v| v.to_s } : value.to_s
14
- end
15
- end
16
- end
4
+ def parser(value, deps)
5
+ value.is_a?(Array) ? value.map { |v| parse_number(v) } : parse_number(value)
6
+ end
17
7
 
18
- end
19
- end
20
- end
21
- end
8
+ def serializer(value, deps)
9
+ value.is_a?(Array) ? value.map { |v| v.to_s } : value.to_s
22
10
  end
11
+
12
+ private
13
+
14
+ def parse_number(value)
15
+ value.to_s.strip.empty? ? nil : (value.to_f rescue nil)
16
+ end
17
+
23
18
  end
@@ -0,0 +1,31 @@
1
+ class Eco::API::Common::People::DefaultParsers::PolicyGroupsParser < Eco::API::Common::Loaders::Parser
2
+ attribute "policy_group_ids"
3
+
4
+ def parser(hash, deps)
5
+ policy_group_ids = []
6
+ if policy_ids = hash["policy_group_ids"]
7
+ policy_group_ids = policy_ids.split("|").map do |name|
8
+ policy_groups.to_id(name&.downcase.strip)
9
+ end.compact
10
+ end
11
+ policy_group_ids.empty?? default_id : policy_group_ids.join("|")
12
+ end
13
+
14
+ def serializer(person, deps)
15
+ ids = person&.account&.policy_group_ids || []
16
+ ids.map do |id|
17
+ policy_groups.to_name(id)
18
+ end.compact.join("|")
19
+ end
20
+
21
+ private
22
+
23
+ def policy_groups
24
+ @policy_groups ||= ASSETS.config.policy_groups
25
+ end
26
+
27
+ def default_id
28
+ @default_id ||= policy_groups.to_id(ASSETS.config.people.default_usergroup)
29
+ end
30
+
31
+ end
@@ -1,29 +1,20 @@
1
- module Eco
2
- module API
3
- module Common
4
- module People
5
- class DefaultParsers
6
- class SendInvitesParser < People::BaseParser
1
+ class Eco::API::Common::People::DefaultParsers::SendInvitesParser < Eco::API::Common::Loaders::Parser
2
+ attribute "send_invites"
7
3
 
8
- def process
9
- @parsers.define_attribute("send_invites") do |parser|
10
- parser.def_parser do |hash|
11
- value = hash["send_invites"]
12
- value.is_a?(Array) ? value.map { |v| truthy(v) } : truthy(value)
13
- end.def_serializer do |person|
14
- person.account && person.account.send_invites&.to_s
15
- end
16
- end
17
- end
4
+ def parser(hash, deps)
5
+ value = hash["send_invites"]
6
+ value = value.first if value.is_a?(Array)
7
+ truthy(value)
8
+ end
18
9
 
19
- private
10
+ def serializer(person, deps)
11
+ person.account && person.account.send_invites&.to_s
12
+ end
13
+
14
+ private
20
15
 
21
- def truthy (value)
22
- %w[true y yes x].include?(value.to_s&.downcase)
23
- end
24
- end
25
- end
26
- end
27
- end
16
+ def truthy (value)
17
+ %w[true y yes x].include?(value.to_s&.downcase)
28
18
  end
19
+
29
20
  end
@@ -3,7 +3,7 @@ module Eco
3
3
  module Common
4
4
  module People
5
5
  # Class meant to offer a _collection_ of entries, normally used to get parsed input data.
6
- # @attr_reader entries [Array<Eco::API::Common::PersonEntry] a pure `Array` object.
6
+ # @attr_reader entries [Array<Eco::API::Common::PeopleEntry] a pure `Array` object.
7
7
  class Entries < Eco::Language::Models::Collection
8
8
  # build the shortcuts of Collection
9
9
  attr_collection :id, :external_id, :email, :name, :supervisor_id
@@ -17,37 +17,41 @@ module Eco
17
17
  @caches_init = false
18
18
  end
19
19
 
20
- # Override `each` to make it work with supervisor hiearchy
21
- def each(params: {}, &block)
22
- return to_enum(:each) unless block
23
- @array_supers = sort_by_supervisors(@items) unless @caches_init
24
- @array_supers.each(&block)
20
+ # @!group Main identifier helpers
21
+
22
+ def id(*args)
23
+ attr('id', *args).first
24
+ end
25
+
26
+ def external_id(*args)
27
+ attr('external_id', *args).first
25
28
  end
26
29
 
27
30
  def [](id_or_ext)
28
31
  id(id_or_ext) || external_id(id_or_ext)
29
32
  end
33
+ # @!endgroup
30
34
 
31
- def id(*args)
32
- attr('id', *args).first
35
+ # @!group Special filters
36
+
37
+ def filter_tags_any(tags)
38
+ attr("filter_tags", tags, default_modifier.any.insensitive)
33
39
  end
34
40
 
35
- def external_id(*args)
36
- attr('external_id', *args).first
41
+ def filter_tags_all(tags)
42
+ attr("filter_tags", tags, default_modifier.all.insensitive)
37
43
  end
38
44
 
39
- # Helper to dump the entries into a CSV
40
- # @param filename [String] the destination file
41
- def export(filename)
42
- CSV.open(filename, "w") do |csv|
43
- entry = self.first
44
- header = entry.internal_entry.keys
45
- csv << header
46
- self.each do |entry|
47
- csv << entry.internal_entry.values
48
- end
49
- end
45
+ def policy_group_ids_any(ids)
46
+ attr("policy_group_ids", tags, default_modifier.any.insensitive)
47
+ end
48
+
49
+ def policy_group_ids_all(ids)
50
+ attr("policy_group_ids", tags, default_modifier.all.insensitive)
50
51
  end
52
+ # @!endgroup
53
+
54
+ # @!group Searchers
51
55
 
52
56
  # Search function to find an `entry` based on one of different options
53
57
  def entry(id: nil, external_id: nil, email: nil, strict: false)
@@ -68,11 +72,21 @@ module Eco
68
72
  # Search function to find an `entry` based on one of different options
69
73
  # see Eco::API::Common::People::Entries#entry
70
74
  def find(object, strict: false)
71
- id = object.respond_to?("id")? object.send("id") : nil
72
- external_id = object.respond_to?("external_id")? object.send("external_id") : nil
73
- email = object.respond_to?("email")? object.send("email") : nil
75
+ id = attr_value(object, "id")
76
+ external_id = attr_value(object, "external_id")
77
+ email = attr_value(object, "email")
74
78
  entry(id: id, external_id: external_id, email: email, strict: strict)
75
79
  end
80
+ # @!endgroup
81
+
82
+ # @!group Basic Collection Methods
83
+
84
+ # Override `each` to make it work with supervisor hiearchy
85
+ def each(&block)
86
+ return to_enum(:each) unless block
87
+ @array_supers = sort_by_supervisors(@items) unless @caches_init
88
+ @array_supers.each(&block)
89
+ end
76
90
 
77
91
  def exclude(object)
78
92
  exclude_people(into_a(object))
@@ -85,6 +99,21 @@ module Eco
85
99
  newFrom to_a - discarded
86
100
  end
87
101
 
102
+ # Helper to dump the entries into a CSV
103
+ # @param filename [String] the destination file
104
+ def export(filename)
105
+ CSV.open(filename, "w") do |csv|
106
+ entry = self.first
107
+ header = entry.internal_entry.keys
108
+ csv << header
109
+ self.each do |entry|
110
+ csv << entry.internal_entry.values
111
+ end
112
+ end
113
+ end
114
+
115
+ # @!group Groupping methods
116
+
88
117
  def email_id_maps
89
118
  email_present.group_by(:email).transform_values { |person| person.id }
90
119
  end
@@ -96,6 +125,7 @@ module Eco
96
125
  def to_h(attr = "id")
97
126
  super(attr || "id")
98
127
  end
128
+ # @!endgroup
99
129
 
100
130
  protected
101
131
 
@@ -4,29 +4,27 @@ module Eco
4
4
  module People
5
5
  # Helper factory class to generate entries (input entries).
6
6
  # @attr_reader schema [Ecoportal::API::V1::PersonSchema] person schema to be used in this entry factory
7
- # @attr_reader person_parser [Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
8
- class EntryFactory
7
+ class EntryFactory < Eco::API::Common::Session::BaseSession
9
8
 
10
9
  attr_reader :schema, :person_parser
11
10
 
11
+ # @param e [Eco::API::Common::Session::Environment] requires a session environment, as any child of `Eco::API::Common::Session::BaseSession`
12
12
  # @param schema [Ecoportal::API::V1::PersonSchema] schema of person details that the parser will be based upon.
13
13
  # @param person_parser [nil, Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
14
14
  # @param attr_map [nil, Eco::Data::Mapper] attribute names mapper to translate external names into internal ones and _vice versa_.
15
- # @param logger [Common::Session::Logger, ::Logger] object to manage logs.
16
- def initialize(schema:, person_parser: nil, attr_map: nil, logger: ::Logger.new(IO::NULL))
15
+ def initialize(e, schema:, person_parser: nil, attr_map: nil)
17
16
  fatal "Constructor needs a PersonSchema. Given: #{schema}" if !schema.is_a?(Ecoportal::API::V1::PersonSchema)
18
17
  fatal "Expecting PersonParser. Given: #{person_parser}" if person_parser && !person_parser.is_a?(Eco::API::Common::People::PersonParser)
19
18
  fatal "Expecting Mapper object. Given: #{fields_mapper}" if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
19
+ super(e)
20
20
 
21
- @logger = logger
22
21
  @schema = Ecoportal::API::V1::PersonSchema.new(JSON.parse(schema.doc.to_json))
23
-
24
22
  @source_person_parser = person_parser
25
23
 
26
- # load default parsers
27
- @person_parser = Eco::API::Common::People::DefaultParsers.new(schema: @schema)
28
- # merge the custom parsers
29
- @person_parser = @person_parser.merge(@source_person_parser)
24
+ # load default parser + custom parsers
25
+ base_parser = Eco::API::Common::People::DefaultParsers.new(schema: @schema).merge(@source_person_parser)
26
+ # new parser with linked schema
27
+ @person_parser = @source_person_parser.new(schema: @schema).merge(base_parser)
30
28
  @person_parser_patch_version = @source_person_parser.patch_version
31
29
 
32
30
  @attr_map = attr_map
@@ -34,6 +32,7 @@ module Eco
34
32
 
35
33
  # provides with a Eco::API::Common::People::PersonParser object (collection of attribute parsers)
36
34
  # @note if the custom person parser has changed, it updates the copy of this EntryFactory instance
35
+ # @return [Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
37
36
  def person_parser
38
37
  if @person_parser_patch_version < @source_person_parser.patch_version
39
38
  @person_parser.merge(@source_person_parser)
@@ -52,7 +51,7 @@ module Eco
52
51
  person_parser: person_parser,
53
52
  attr_map: @attr_map,
54
53
  dependencies: dependencies,
55
- logger: @logger
54
+ logger: logger
56
55
  )
57
56
  end
58
57
 
@@ -78,10 +77,14 @@ module Eco
78
77
  arr_hash = []
79
78
  if Eco::API::Common::Session::FileManager.file_exists?(file)
80
79
  encoding ||= Eco::API::Common::Session::FileManager.encoding(file)
80
+ encoding = (encoding != "utf-8")? "#{encoding}|utf-8": encoding
81
81
  file_content = File.read(file, encoding: encoding)
82
- arr_hash = person_parser.parse(format, file_content)
82
+ arr_hash = person_parser.parse(format, file_content).map.each_with_index do |entry_hash, i|
83
+ j = (format == :csv)? i + 2 : i + 1
84
+ entry_hash.tap {|hash| hash["idx"] = j}
85
+ end
83
86
  else
84
- @logger.warn("File does not exist: #{file}")
87
+ logger.warn("File does not exist: #{file}")
85
88
  end
86
89
 
87
90
  entries(data: arr_hash)
@@ -118,7 +121,7 @@ module Eco
118
121
  deps = {"supervisor_id" => {people: data}}
119
122
 
120
123
  data_entries = data.map do |person|
121
- self.new(person, dependencies: deps).to_hash
124
+ self.new(person, dependencies: deps).external_entry
122
125
  end
123
126
 
124
127
  File.open(file, "w", enconding: encoding) do |fd|
@@ -131,7 +134,7 @@ module Eco
131
134
  private
132
135
 
133
136
  def fatal(msg)
134
- @logger.fatal(msg)
137
+ logger.fatal(msg)
135
138
  raise msg
136
139
  end
137
140
 
@@ -12,21 +12,39 @@ module Eco
12
12
  # parser will be active/used.
13
13
  # - this is important to help avoiding to set values that are not present in the input entry.
14
14
  # - if you have doubts about using it or not, do not use it.
15
- # @param active_when [Proc] that expects a list of internal attributes or the internal entry itself
15
+ # @param phase [Symbol] the phase when this parser should be active.
16
+ # Must be one of [`:internal`, `:final`]
17
+ # @param active_when [Proc] that expects a list of internal attributes or the internal entry itself.
16
18
  # By default, an attribute paraser is active if in the entry to parse the internal attribute is present.
17
- def def_parser(active_when: nil, &block)
19
+ def def_parser(phase = :internal, active_when: nil, &block)
18
20
  @active_when = attribute_present(active_when)
19
- super(&block)
21
+ super(phase, &block)
20
22
  end
21
23
 
22
- # Determines if for a given source data to parse, this parser should be active or not
23
- # @return [Boolean] `true` if there's no callback defined or there is and evaluates `true`
24
- # when for the current data to parse. It returns `false` otherwise.
25
- def parser_active?(source_data)
26
- @active_when.call(source_data)
24
+ # @param phase [Symbol] the phase when this serializer should be active.
25
+ # Must be one of [`:person,` `:final`, `:internal`]
26
+ def def_serializer(phase = :person, &block)
27
+ super(phase, &block)
27
28
  end
28
29
 
29
- # Helper to build the `active_when` condition
30
+ # Determines if for a given source data to parse, this parser should be active or not.
31
+ # @param phase [Symbol] the phase when this parser should be active.
32
+ # Must be one of [`:internal`, `:final`]
33
+ # @return [Boolean] `true` if there's parser defined and `source_data` activates it.
34
+ def parser_active?(source_data, phase = :any)
35
+ (phase == :any || parser_category?(phase)) && @active_when.call(source_data)
36
+ end
37
+
38
+ # Determines a serializer should be active or not.
39
+ # @param phase [Symbol] the phase when this serializer should be active.
40
+ # Must be one of [`:person,` `:final`, `:internal`]
41
+ # @return [Boolean] `true` if there's serializer defined.
42
+ def serializer_active?(phase = :any)
43
+ (phase == :any) || serializer_category?(phase)
44
+ end
45
+
46
+
47
+ # Helper to build the `active_when` condition.
30
48
  def active_when_any?(*attrs)
31
49
  Proc.new do |source_data|
32
50
  keys = data_keys(source_data)
@@ -34,7 +52,7 @@ module Eco
34
52
  end
35
53
  end
36
54
 
37
- # Helper to build the `active_when` condition
55
+ # Helper to build the `active_when` condition.
38
56
  def active_when_all?(*attrs)
39
57
  Proc.new do |source_data|
40
58
  keys = data_keys(source_data)
@@ -44,7 +62,7 @@ module Eco
44
62
 
45
63
  private
46
64
 
47
- # by default, an attribute paraser is active if in the entry to parse
65
+ # By default, an attribute paraser is active if in the entry to parse
48
66
  # the internal attribute is present
49
67
  def attribute_present(active_when)
50
68
  Proc.new do |source_data|
@@ -67,7 +85,6 @@ module Eco
67
85
  end
68
86
  end
69
87
 
70
-
71
88
  end
72
89
  end
73
90
  end