eco-helpers 2.0.9 → 2.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -1
- data/lib/eco/api/common/people/person_entry.rb +1 -1
- data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +8 -8
- data/lib/eco/api/common/people/person_parser.rb +22 -11
- data/lib/eco/api/organization/tag_tree.rb +20 -3
- data/lib/eco/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8ca1a088cf60b7608e9f92da680a631c73228ae92fed33484b5669c480350fb
|
4
|
+
data.tar.gz: 6733d55fbdcf24c2766529c275f86f3132f1c3232b4c8d31f34880f9253dbc82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1cdc2aacd52c590230fe606b079d91e6f3dea01ddbd8857d48170467bbfd089ffa7081c8630aac93ebae4c6e3b8125a446cd7ddc8a6b290c174e906661b2474
|
7
|
+
data.tar.gz: 356ccf7e50d3f0e12f9ad1dde6f1fe7ad4fd13205334f9319cecd45736f110684eb404113512aca4b1d2c49070fe61eb48b1e69c3ae13f423ae8895288ea0d2d
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,24 @@
|
|
1
1
|
# Change Log
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
-
## [2.0.
|
4
|
+
## [2.0.10] - 2021-03-xx
|
5
|
+
|
6
|
+
### Added
|
7
|
+
- `Eco::API::Common::People::PersonParser`
|
8
|
+
- `#defined_attrs`: list of all attribute defined parsers (regardless they belong to the model)
|
9
|
+
- `Eco::API::Organization::TagTree` methods
|
10
|
+
- `#total_depth` to know the total depth of a tree
|
11
|
+
- `#flat?` to know if the tagtree has only 1 level
|
12
|
+
|
13
|
+
### Changed
|
14
|
+
- `Eco::API::Common::People::PersonParser`
|
15
|
+
- Renamed `#all_attrs` to `#all_model_attrs`
|
16
|
+
- Ranamed `#defined_attrs` to `#defined_model_attrs`
|
17
|
+
- Ranamed `#undefined_attrs` to `#undefined_model_attrs`
|
18
|
+
|
19
|
+
### Fixed
|
20
|
+
|
21
|
+
## [2.0.9] - 2021-03-19
|
5
22
|
|
6
23
|
### Added
|
7
24
|
### Changed
|
@@ -256,7 +256,7 @@ module Eco
|
|
256
256
|
# @param mapped_entry [Hash] that with **internal** names but **external** values and types.
|
257
257
|
# @return [Hash] with **external** names, values and types.
|
258
258
|
def _external_serializing(mapped_entry)
|
259
|
-
target_attrs = @emap.
|
259
|
+
target_attrs = @emap.all_model_attrs | @emap.aliased_attrs
|
260
260
|
rest_keys = mapped_entry.keys - target_attrs
|
261
261
|
target_attrs -= ["send_invites"]
|
262
262
|
external_entry = target_attrs.each_with_object({}) do |attr, hash|
|
@@ -6,14 +6,14 @@ module Eco
|
|
6
6
|
# @attr_reader core_attrs [Array<String>] core attributes that are present in the person entry.
|
7
7
|
# @attr_reader details_attrs [Array<String>] schema details attributes that are present in the person entry.
|
8
8
|
# @attr_reader account_attrs [Array<String>] account attributes that are present in the person entry.
|
9
|
-
# @attr_reader
|
9
|
+
# @attr_reader all_model_attrs [Array<String>] all the attrs that are present in the person entry.
|
10
10
|
# @attr_reader internal_attrs [Array<String>] all the internally named attributes that the person entry has.
|
11
11
|
# @attr_reader aliased_attrs [Array<String>] only those internal attributes present in the person entry that have an internal/external name mapping.
|
12
12
|
# @attr_reader direct_attrs [Array<String>] only those internal attributes present in the person entry that do **not** have an internal/external name mapping.
|
13
13
|
class PersonEntryAttributeMapper
|
14
14
|
@@cached_warnings = {}
|
15
15
|
|
16
|
-
attr_reader :core_attrs, :details_attrs, :account_attrs, :
|
16
|
+
attr_reader :core_attrs, :details_attrs, :account_attrs, :all_model_attrs
|
17
17
|
attr_reader :internal_attrs, :aliased_attrs, :direct_attrs
|
18
18
|
|
19
19
|
# Helper class tied to `PersonEntry` that allows to track which attributes of a person entry are present
|
@@ -41,14 +41,14 @@ module Eco
|
|
41
41
|
init_attr_trackers
|
42
42
|
else # SERIALIZING
|
43
43
|
@person = data
|
44
|
-
@internal_attrs = @person_parser.
|
44
|
+
@internal_attrs = @person_parser.all_model_attrs
|
45
45
|
@aliased_attrs = @attr_map.list(:internal)
|
46
46
|
end
|
47
47
|
|
48
48
|
@core_attrs = @person_parser.target_attrs_core(@internal_attrs)
|
49
49
|
@details_attrs = @person_parser.target_attrs_details(@internal_attrs)
|
50
50
|
@account_attrs = @person_parser.target_attrs_account(@internal_attrs)
|
51
|
-
@
|
51
|
+
@all_model_attrs = @core_attrs | @account_attrs | @details_attrs
|
52
52
|
end
|
53
53
|
|
54
54
|
# To know if currently the object is in parse or serialize mode.
|
@@ -98,7 +98,7 @@ module Eco
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
return nil unless @person_parser.
|
101
|
+
return nil unless @person_parser.all_model_attrs.include?(attr)
|
102
102
|
end
|
103
103
|
|
104
104
|
# Serializing helper also used to do a reverse mapping when parsing:
|
@@ -146,9 +146,9 @@ module Eco
|
|
146
146
|
# when parsing:
|
147
147
|
def init_attr_trackers
|
148
148
|
# (def) all internal attributes we can expect
|
149
|
-
def_all_attrs = @person_parser.
|
149
|
+
def_all_attrs = @person_parser.all_model_attrs
|
150
150
|
# (def) internal attrs with no aliasing nor parser definition (expected to be direct)
|
151
|
-
def_unlinked = @person_parser.
|
151
|
+
def_unlinked = @person_parser.undefined_model_attrs.select { |attr| !to_external(attr) }
|
152
152
|
# (def) those with parser or alias:
|
153
153
|
def_linked = def_all_attrs - def_unlinked
|
154
154
|
|
@@ -168,7 +168,7 @@ module Eco
|
|
168
168
|
# (data) virtual internal attrs (the internal names of those virtual attrs)
|
169
169
|
data_vi_int_aliased = data_vi_ext_alias.map do |attr|
|
170
170
|
# to_internal(attr) can't be used here, becauase virtual fields would get filtered out,
|
171
|
-
# as they are not recognized by @parser.
|
171
|
+
# as they are not recognized by @parser.all_model_attrs.include?(attr)
|
172
172
|
@attr_map.to_internal(attr)
|
173
173
|
end.compact
|
174
174
|
|
@@ -7,7 +7,7 @@ module Eco
|
|
7
7
|
#
|
8
8
|
# @attr_reader schema [Ecoportal::API::V1::PersonSchema, nil] schema of person details that this parser will be based upon.
|
9
9
|
# @attr_reader details_attrs [Array<String>] internal names of schema details attributes.
|
10
|
-
# @attr_reader
|
10
|
+
# @attr_reader all_model_attrs [Array<String>] all the internal name attributes, including _core_, _account_ and _details_.
|
11
11
|
class PersonParser
|
12
12
|
extend Eco::API::Common::ClassAutoLoader
|
13
13
|
autoloads_children_of "Eco::API::Common::Loaders::Parser"
|
@@ -19,8 +19,7 @@ module Eco
|
|
19
19
|
FORMAT = [:csv, :xml, :json]
|
20
20
|
|
21
21
|
attr_reader :schema
|
22
|
-
attr_reader :details_attrs, :
|
23
|
-
attr_reader :defined_attrs
|
22
|
+
attr_reader :details_attrs, :all_model_attrs
|
24
23
|
attr_reader :patch_version
|
25
24
|
|
26
25
|
# @example Example of usage:
|
@@ -46,7 +45,7 @@ module Eco
|
|
46
45
|
@details_attrs = @schema&.fields.map { |fld| fld.alt_id }
|
47
46
|
end
|
48
47
|
|
49
|
-
@
|
48
|
+
@all_model_attrs = CORE_ATTRS + ACCOUNT_ATTRS + @details_attrs
|
50
49
|
self.class.autoload_children(self)
|
51
50
|
end
|
52
51
|
|
@@ -60,6 +59,11 @@ module Eco
|
|
60
59
|
|
61
60
|
# @!group Scopping attributes (identifying, presence & active)
|
62
61
|
|
62
|
+
# All the internal name attributes, including _core_, _account_ and _details_.
|
63
|
+
def all_attrs(include_defined_parsers: false)
|
64
|
+
all_model_attrs | defined_model_attrs
|
65
|
+
end
|
66
|
+
|
63
67
|
# Scopes `source_attrs` using the _**core** attributes_.
|
64
68
|
# @note use this helper to know which among your attributes are **core** ones.
|
65
69
|
# @param source_attrs [Array<String>]
|
@@ -93,14 +97,21 @@ module Eco
|
|
93
97
|
@parsers.keys
|
94
98
|
end
|
95
99
|
|
100
|
+
# Returns a list of all the internal attributes that have a parser defined.
|
101
|
+
# @note These attributes do not necessarily belong to the model. They could be virtual attributes
|
102
|
+
# @return [Array<String>] list of all attribute defined parsers.
|
103
|
+
def defined_attrs
|
104
|
+
defined_list - symbol_keys
|
105
|
+
end
|
106
|
+
|
96
107
|
# Returns a list of all the internal attributes of the model that have a parser defined.
|
97
108
|
# @note
|
98
109
|
# - it excludes any parser that is not in the model, such as type parsers (i.e. `:boolean`, `:multiple`)
|
99
110
|
# - the list is sorted according `CORE_ATTRS` + `ACCOUNT_ATTRS` + schema attrs
|
100
|
-
# @return [Array<String>] list of all attribute defined parsers.
|
101
|
-
def
|
111
|
+
# @return [Array<String>] list of all attribute defined parsers in the model.
|
112
|
+
def defined_model_attrs
|
102
113
|
defined = @parsers.keys
|
103
|
-
defined = (
|
114
|
+
defined = (all_model_attrs | defined) & defined
|
104
115
|
defined - symbol_keys
|
105
116
|
end
|
106
117
|
|
@@ -118,7 +129,7 @@ module Eco
|
|
118
129
|
# @param process [Symbol] either `:parse` or `:serialize`, depending if we want to parse or serialize the `attr`.
|
119
130
|
# @return [Array<String>] list of all attribute defined parsers that should be active for the given `source_data`.
|
120
131
|
def active_attrs(source_data, phase = :any, process: :parse)
|
121
|
-
|
132
|
+
defined_model_attrs.select do |attr|
|
122
133
|
if process == :serialize
|
123
134
|
@parsers[attr].serializer_active?(phase)
|
124
135
|
else
|
@@ -130,8 +141,8 @@ module Eco
|
|
130
141
|
# Returns a list of all the internal attributes of the model that do **not** have a parser defined.
|
131
142
|
# @note it excludes any parser that is **not** in the model, such as type parsers (i.e. :boolean, :multiple)
|
132
143
|
# @return [Array<String>] list of all attributes without a defined parser.
|
133
|
-
def
|
134
|
-
|
144
|
+
def undefined_model_attrs
|
145
|
+
all_model_attrs - defined_model_attrs
|
135
146
|
end
|
136
147
|
|
137
148
|
# @param attr [String] internal name of an attribute.
|
@@ -238,7 +249,7 @@ module Eco
|
|
238
249
|
end
|
239
250
|
|
240
251
|
def valid_attr?(attr)
|
241
|
-
attr.is_a?(String) && (!@schema || @
|
252
|
+
attr.is_a?(String) && (!@schema || @all_model_attrs.include?(attr))
|
242
253
|
end
|
243
254
|
|
244
255
|
def valid_type?(attr)
|
@@ -4,7 +4,7 @@ module Eco
|
|
4
4
|
|
5
5
|
# Provides helpers to deal with tagtrees.
|
6
6
|
class TagTree
|
7
|
-
attr_reader :tag, :nodes
|
7
|
+
attr_reader :tag, :nodes, :children_count
|
8
8
|
attr_reader :depth, :path
|
9
9
|
attr_reader :enviro
|
10
10
|
|
@@ -37,6 +37,7 @@ module Eco
|
|
37
37
|
|
38
38
|
nodes = @source.is_a?(Array) ? @source : @source.dig('nodes') || []
|
39
39
|
@nodes = nodes.map {|cnode| TagTree.new(cnode, depth: @depth + 1, path: @path.dup, enviro: @enviro)}
|
40
|
+
@children_count = @nodes.count
|
40
41
|
|
41
42
|
init_hashes
|
42
43
|
end
|
@@ -46,6 +47,21 @@ module Eco
|
|
46
47
|
@has_tags.empty?
|
47
48
|
end
|
48
49
|
|
50
|
+
# @return [Integer] the highest `depth` of all the children.
|
51
|
+
def total_depth
|
52
|
+
@total_depth ||= if children_count > 0
|
53
|
+
deepest_node = nodes.max_by {|node| node.total_depth}
|
54
|
+
deepest_node.depth
|
55
|
+
else
|
56
|
+
depth
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# @return [Integer] if there's only top level.
|
61
|
+
def flat?
|
62
|
+
self.total_depth <= 0
|
63
|
+
end
|
64
|
+
|
49
65
|
# Gets all the tags of the current node tree.
|
50
66
|
# @note
|
51
67
|
# - this will include the upper level tag(s) as well
|
@@ -147,7 +163,8 @@ module Eco
|
|
147
163
|
# * take the deepest tag (the one that is further down in the tree)
|
148
164
|
# * if there are different options (several nodes at the same depth):
|
149
165
|
# * take the common node between them (i.e. you have Hamilton and Auckland -> take New Zealand)
|
150
|
-
# * if there's no common node between them, take the `first
|
166
|
+
# * if there's no common node between them, take the `first`, unless they are at top level of the tree
|
167
|
+
# * to the above, take the `first` also on top level, but only if there's 1 level for the entire tree
|
151
168
|
# @param [Array<String>] values list of tags.
|
152
169
|
# @return [String] default tag.
|
153
170
|
def default_tag(*values)
|
@@ -169,7 +186,7 @@ module Eco
|
|
169
186
|
common = nodes.reduce(self.tags.reverse) {|com, cnode| com & cnode.path.reverse}
|
170
187
|
default_tag = common.first if common.length > 0 && depth > 0
|
171
188
|
end
|
172
|
-
default_tag = nodes.first&.tag
|
189
|
+
default_tag = nodes.first&.tag if !default_tag && ( (depth > 0) || flat?)
|
173
190
|
default_tag
|
174
191
|
end
|
175
192
|
|
data/lib/eco/version.rb
CHANGED