Class: Eco::API::Common::People::PersonEntry
- Includes:
- Language::AuxiliarLogger
- Defined in:
- lib/eco/api/common/people/person_entry.rb
Constant Summary collapse
- DEBUG =
false
Instance Attribute Summary
Attributes included from Language::AuxiliarLogger
Instance Method Summary collapse
- #default_tag ⇒ Object
- #default_tag? ⇒ Boolean
-
#email ⇒ String?
The email of this person if defined.
- #email? ⇒ Boolean
-
#external_entry ⇒ Hash
Entry
Hash
with external attribute names, and values and types thereof. -
#external_id ⇒ String?
The external id of this person if defined.
- #external_id? ⇒ Boolean
- #filter_tags ⇒ Object
- #filter_tags? ⇒ Boolean
-
#final_entry ⇒ Hash
Entry
Hash
with internal attribute names, values and types. -
#id ⇒ String?
The internal id of this person if defined.
- #id? ⇒ Boolean
-
#identify ⇒ String
Provides a reference to this person entry.
-
#idx ⇒ Integer
The entry number in the input file.
-
#initialize(data, person_parser:, attr_map:, dependencies: {}, logger: ::Logger.new(IO::NULL)) ⇒ PersonEntry
constructor
This class is meant to provide a common interface to access entries of source data that come in different formats.
-
#internal_entry ⇒ Hash
Entry
Hash
with internal attribute names and values, but external types. -
#mapped_entry ⇒ Hash
Entry
Hash
with internal attribute names, but external types and values. -
#name ⇒ String?
The name of this person if defined.
- #name? ⇒ Boolean
-
#new(data) ⇒ PersonEntry
Generates a new entry.
-
#parsing? ⇒ Boolean
To know if currently the object is in parse or serialize mode.
- #policy_group_ids ⇒ Object
- #policy_group_ids? ⇒ Boolean
-
#serializing? ⇒ Boolean
To know if currently the object is in parse or serialize mode.
-
#set_account(person, exclude: nil) ⇒ Object
Setter to fill in the
account
properties of thePerson
that are present in theEntry
. -
#set_core(person, exclude: nil) ⇒ Object
Setter to fill in all the
core
properties of thePerson
that are present in theEntry
. -
#set_details(person, exclude: nil) ⇒ Object
TO DO: use person.details.schema_id to switch @emap and @person_parser (or just crash if they don't match?) Setter to fill in all the schema
details
fields of thePerson
that are present in theEntry
. -
#supervisor_id ⇒ String?
The supervisor id of this person if defined.
- #supervisor_id=(value) ⇒ Object
- #supervisor_id? ⇒ Boolean
-
#to_s(options) ⇒ String
Provides a reference of this person entry.
Methods included from Language::AuxiliarLogger
Constructor Details
#initialize(data, person_parser:, attr_map:, dependencies: {}, logger: ::Logger.new(IO::NULL)) ⇒ PersonEntry
- if
data
is aPerson
object, its behaviour isserialise
. - if
data
is not aPerson
object, it does aparse
. - currently in rework, so there may be subtle differences that make it temporarily unstable (yet it is reliable).
This class is meant to provide a common interface to access entries of source data that come in different formats.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/eco/api/common/people/person_entry.rb', line 26 def initialize(data, person_parser:, attr_map:, dependencies: {}, logger: ::Logger.new(IO::NULL)) msg = "Constructor needs a PersonParser. Given: #{person_parser.class}" raise msg unless person_parser.is_a?(Eco::API::Common::People::PersonParser) msg = "Expecting Mapper object. Given: #{attr_map.class}" raise msg if attr_map && !attr_map.is_a?(Eco::Data::Mapper) @source = data @person_parser = person_parser @deps = dependencies @logger = logger @attr_map = attr_map @emap = PersonEntryAttributeMapper.new( @source, person_parser: @person_parser, attr_map: @attr_map, logger: @logger ) if parsing? @external_entry = __external_entry(data) @mapped_entry = __mapped_entry(@external_entry) @internal_entry = __internal_entry(@mapped_entry) @final_entry = __final_entry(@internal_entry) else # SERIALIZING @person = data @final_entry = __final_entry(@person) @internal_entry = __internal_entry(@final_entry) @mapped_entry = __mapped_entry(@internal_entry) @external_entry = __external_entry(@mapped_entry) end (print_models; exit(1)) if DEBUG # rubocop:disable Style/Semicolon end |
Instance Method Details
#default_tag ⇒ Object
183 184 185 |
# File 'lib/eco/api/common/people/person_entry.rb', line 183 def default_tag final_entry["default_tag"] end |
#default_tag? ⇒ Boolean
179 180 181 |
# File 'lib/eco/api/common/people/person_entry.rb', line 179 def default_tag? final_entry.key?("default_tag") end |
#email ⇒ String?
Returns the email of this person if defined.
142 143 144 |
# File 'lib/eco/api/common/people/person_entry.rb', line 142 def email final_entry["email"] end |
#email? ⇒ Boolean
146 147 148 |
# File 'lib/eco/api/common/people/person_entry.rb', line 146 def email? final_entry.key?("email") end |
#external_entry ⇒ Hash
completely serialized entry.
Returns entry Hash
with external attribute names, and values and types thereof.
75 76 77 |
# File 'lib/eco/api/common/people/person_entry.rb', line 75 def external_entry # rubocop:disable Style/TrivialAccessors @external_entry end |
#external_id ⇒ String?
Returns the external id of this person if defined.
124 125 126 |
# File 'lib/eco/api/common/people/person_entry.rb', line 124 def external_id final_entry["external_id"] end |
#external_id? ⇒ Boolean
128 129 130 |
# File 'lib/eco/api/common/people/person_entry.rb', line 128 def external_id? final_entry.key?("external_id") end |
#filter_tags ⇒ Object
163 164 165 |
# File 'lib/eco/api/common/people/person_entry.rb', line 163 def final_entry["filter_tags"] || [] end |
#filter_tags? ⇒ Boolean
167 168 169 |
# File 'lib/eco/api/common/people/person_entry.rb', line 167 def final_entry.key?("filter_tags") end |
#final_entry ⇒ Hash
values ready to be set to a person.
Returns entry Hash
with internal attribute names, values and types.
92 93 94 |
# File 'lib/eco/api/common/people/person_entry.rb', line 92 def final_entry # rubocop:disable Style/TrivialAccessors @final_entry end |
#id ⇒ String?
Returns the internal id of this person if defined.
115 116 117 |
# File 'lib/eco/api/common/people/person_entry.rb', line 115 def id final_entry["id"] end |
#id? ⇒ Boolean
119 120 121 |
# File 'lib/eco/api/common/people/person_entry.rb', line 119 def id? final_entry.key?("id") end |
#identify ⇒ String
Provides a reference to this person entry.
189 190 191 192 |
# File 'lib/eco/api/common/people/person_entry.rb', line 189 def identify str_id = id ? "id: '#{id}'; " : "" "(row: #{idx}) '#{name}' (#{str_id}ext_id: '#{external_id}'; email: '#{email}')" end |
#idx ⇒ Integer
Eco::API::Common::People::EntryFactory#entries
adds this idx
(i.e. row number)
Returns the entry number in the input file.
110 111 112 |
# File 'lib/eco/api/common/people/person_entry.rb', line 110 def idx final_entry["idx"] end |
#internal_entry ⇒ Hash
just one step away from being completely parsed (only types parsing pending).
Returns entry Hash
with internal attribute names and values, but external types.
81 82 83 |
# File 'lib/eco/api/common/people/person_entry.rb', line 81 def internal_entry # rubocop:disable Style/TrivialAccessors @internal_entry end |
#mapped_entry ⇒ Hash
Returns entry Hash
with internal attribute names, but external types and values.
86 87 88 |
# File 'lib/eco/api/common/people/person_entry.rb', line 86 def mapped_entry # rubocop:disable Style/TrivialAccessors @mapped_entry end |
#name ⇒ String?
Returns the name of this person if defined.
133 134 135 |
# File 'lib/eco/api/common/people/person_entry.rb', line 133 def name final_entry["name"] end |
#name? ⇒ Boolean
137 138 139 |
# File 'lib/eco/api/common/people/person_entry.rb', line 137 def name? final_entry.key?("name") end |
#new(data) ⇒ PersonEntry
Generates a new entry
63 64 65 66 67 68 69 70 71 |
# File 'lib/eco/api/common/people/person_entry.rb', line 63 def new(data) self.class.new( data, person_parser: @person_parser, attr_map: @attr_map, dependencies: @deps, logger: @logger ) end |
#parsing? ⇒ Boolean
To know if currently the object is in parse or serialize mode.
98 99 100 |
# File 'lib/eco/api/common/people/person_entry.rb', line 98 def parsing? !@source.is_a?(Ecoportal::API::Internal::Person) end |
#policy_group_ids ⇒ Object
171 172 173 |
# File 'lib/eco/api/common/people/person_entry.rb', line 171 def policy_group_ids final_entry["policy_group_ids"] || [] end |
#policy_group_ids? ⇒ Boolean
175 176 177 |
# File 'lib/eco/api/common/people/person_entry.rb', line 175 def policy_group_ids? final_entry.key?("policy_group_ids") end |
#serializing? ⇒ Boolean
To know if currently the object is in parse or serialize mode.
104 105 106 |
# File 'lib/eco/api/common/people/person_entry.rb', line 104 def serializing? !parsing? end |
#set_account(person, exclude: nil) ⇒ Object
it only sets those account properties defined in the entry. Meaning that if an account property is not present in the entry, this will not be set on the target person.
Setter to fill in the account
properties of the Person
that are present in the Entry
.
232 233 234 235 236 237 238 |
# File 'lib/eco/api/common/people/person_entry.rb', line 232 def set_account(person, exclude: nil) person.account = {} unless person.account scoped_attrs = @emap.account_attrs(@final_entry) - into_a(exclude) @final_entry.slice(*scoped_attrs).each do |attr, value| set_part(person.account, attr, value) end end |
#set_core(person, exclude: nil) ⇒ Object
- it only sets those core properties defined in the entry. Meaning that if an core property is not present in the entry, this will not be set on the target person.
- if there's an incorrect email exception, it blanks the email and logs a warning message
Setter to fill in all the core
properties of the Person
that are present in the Entry
.
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/eco/api/common/people/person_entry.rb', line 214 def set_core(person, exclude: nil) scoped_attrs = @emap.core_attrs(@final_entry) - into_a(exclude) scoped_attrs -= ['id'] @final_entry.slice(*scoped_attrs).each do |attr, value| set_part(person, attr, value) rescue StandardError => e raise unless attr == "email" log(:error) { "#{e} - setting blank email instead." } set_part(person, attr, nil) end end |
#set_details(person, exclude: nil) ⇒ Object
it only sets those details properties defined in the entry. Meaning that if an details property is not present in the entry, this will not be set on the target person.
TO DO: use person.details.schema_id to switch @emap and @person_parser (or just crash if they don't match?)
Setter to fill in all the schema details
fields of the Person
that are present in the Entry
.
246 247 248 249 250 251 252 |
# File 'lib/eco/api/common/people/person_entry.rb', line 246 def set_details(person, exclude: nil) person.add_details(@person_parser.schema) if !person.details || !person.details.schema_id scoped_attrs = @emap.details_attrs(@final_entry) - into_a(exclude) @final_entry.slice(*scoped_attrs).each do |attr, value| set_part(person.details, attr, value) end end |
#supervisor_id ⇒ String?
Returns the supervisor id of this person if defined.
151 152 153 |
# File 'lib/eco/api/common/people/person_entry.rb', line 151 def supervisor_id final_entry["supervisor_id"] end |
#supervisor_id=(value) ⇒ Object
155 156 157 |
# File 'lib/eco/api/common/people/person_entry.rb', line 155 def supervisor_id=(value) final_entry["supervisor_id"] = value end |
#supervisor_id? ⇒ Boolean
159 160 161 |
# File 'lib/eco/api/common/people/person_entry.rb', line 159 def supervisor_id? final_entry.key?("supervisor_id") end |
#to_s(options) ⇒ String
Provides a reference of this person entry.
196 197 198 199 200 201 202 203 204 205 |
# File 'lib/eco/api/common/people/person_entry.rb', line 196 def to_s() = into_a() if .include?(:identify) identify else final_entry.each.map do |k, v| "'#{k}': '#{v.to_json}'" end.join(" | ") end end |