Class: Eco::API::Common::People::PersonEntry

Inherits:
Object
  • Object
show all
Includes:
Language::AuxiliarLogger
Defined in:
lib/eco/api/common/people/person_entry.rb

Constant Summary collapse

DEBUG =
false
BOOLEAN_ATTRS =
%w[
  freemium accept_eula archived
].freeze
MULTI_ATTRS =
%w[
  policy_group_ids filter_tags login_provider_ids starred_ids
].freeze
NUMERIC_ATTRS =
%w[
  subordinates
].freeze

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Constructor Details

#initialize(data, person_parser:, attr_map:, dependencies: {}, logger: ::Logger.new(IO::NULL)) ⇒ PersonEntry

Note:
  • if data is a Person object, its behaviour is serialise.
  • if data is not a Person object, it does a parse.
  • 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.

Parameters:

  • data (Hash, Ecoportal::API::V1::Person)

    Person object to be serialized or hashed entry (CSV::Row is accepted).

  • person_parser (Common::People::PersonParser)

    parser/serializer of person attributes (it contains a set of attribute parsers).

  • attr_map (Eco::Data::Mapper)

    mapper to translate attribute names from external to internal names and vice versa.

  • dependencies (Hash) (defaults to: {})

    hash where keys are internal attribute names. It is mostly used to deliver final dependencies to attribute parsers/serializers.

  • logger (Common::Session::Logger, ::Logger) (defaults to: ::Logger.new(IO::NULL))

    object to managelogs.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eco/api/common/people/person_entry.rb', line 36

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

#[](key) ⇒ Object

Value on the final entry



102
103
104
# File 'lib/eco/api/common/people/person_entry.rb', line 102

def [](key)
  final_entry[key]
end

#archivedObject



168
169
170
# File 'lib/eco/api/common/people/person_entry.rb', line 168

def archived
  final_entry['archived']
end

#archived_flag?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/eco/api/common/people/person_entry.rb', line 172

def archived_flag?
  final_entry.key?('archived')
end

#contractor_organization_idObject



215
216
217
# File 'lib/eco/api/common/people/person_entry.rb', line 215

def contractor_organization_id
  final_entry['contractor_organization_id']
end

#contractor_organization_id?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/eco/api/common/people/person_entry.rb', line 219

def contractor_organization_id?
  final_entry.key?('contractor_organization_id')
end

#default_tagObject



235
236
237
# File 'lib/eco/api/common/people/person_entry.rb', line 235

def default_tag
  final_entry['default_tag']
end

#default_tag?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/eco/api/common/people/person_entry.rb', line 231

def default_tag?
  final_entry.key?('default_tag')
end

#emailString?

Returns the email of this person if defined.

Returns:

  • (String, nil)

    the email of this person if defined.



186
187
188
# File 'lib/eco/api/common/people/person_entry.rb', line 186

def email
  final_entry['email']
end

#email?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/eco/api/common/people/person_entry.rb', line 190

def email?
  final_entry.key?('email')
end

#entry(phase = :external) ⇒ Hash

Returns the data as of at that parsing stage.

Returns:

  • (Hash)

    the data as of at that parsing stage.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/eco/api/common/people/person_entry.rb', line 84

def entry(phase = :external)
  phase = phase.to_sym

  case phase
  when :external
    external_entry
  when :mapped
    mapped_entry
  when :internal
    internal_entry
  when :final
    final_entry
  else
    raise ArgumentError, "Unknown phase '#{phase}'"
  end
end

#external_entryHash

Note:

completely serialized entry.

Returns entry Hash with external attribute names, and values and types thereof.

Returns:

  • (Hash)

    entry Hash with external attribute names, and values and types thereof.



108
109
110
# File 'lib/eco/api/common/people/person_entry.rb', line 108

def external_entry # rubocop:disable Style/TrivialAccessors
  @external_entry
end

#external_idString?

Returns the external id of this person if defined.

Returns:

  • (String, nil)

    the external id of this person if defined.



160
161
162
# File 'lib/eco/api/common/people/person_entry.rb', line 160

def external_id
  final_entry['external_id']
end

#external_id?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/eco/api/common/people/person_entry.rb', line 164

def external_id?
  final_entry.key?('external_id')
end

#filter_tagsObject



207
208
209
# File 'lib/eco/api/common/people/person_entry.rb', line 207

def filter_tags
  final_entry['filter_tags'] || []
end

#filter_tags?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/eco/api/common/people/person_entry.rb', line 211

def filter_tags?
  final_entry.key?('filter_tags')
end

#final_entryHash

Note:

values ready to be set to a person.

Returns entry Hash with internal attribute names, values and types.

Returns:

  • (Hash)

    entry Hash with internal attribute names, values and types.



128
129
130
# File 'lib/eco/api/common/people/person_entry.rb', line 128

def final_entry # rubocop:disable Style/TrivialAccessors
  @final_entry
end

#idString?

Returns the internal id of this person if defined.

Returns:

  • (String, nil)

    the internal id of this person if defined.



151
152
153
# File 'lib/eco/api/common/people/person_entry.rb', line 151

def id
  final_entry['id']
end

#id?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/eco/api/common/people/person_entry.rb', line 155

def id?
  final_entry.key?('id')
end

#identifyString

Provides a reference to this person entry.

Returns:

  • (String)

    string summary of this person identity.



241
242
243
244
245
246
247
# File 'lib/eco/api/common/people/person_entry.rb', line 241

def identify
  str_id = id ? "id: '#{id}'; " : ''
  str  = "(row: #{idx}) '#{name}' "
  str << "(#{str_id}ext_id: '#{external_id}'; "
  str << "email: '#{email}')"
  str
end

#idxInteger

Note:

Eco::API::Common::People::EntryFactory#entries adds this idx (i.e. row number)

Returns the entry number in the input file.

Returns:

  • (Integer)

    the entry number in the input file



146
147
148
# File 'lib/eco/api/common/people/person_entry.rb', line 146

def idx
  final_entry['idx']
end

#internal_entryHash

Note:

just one step away from being completely parsed (only types parsing pending).

Returns entry Hash with internal attribute names and values, but external types.

Returns:

  • (Hash)

    entry Hash with internal attribute names and values, but external types.



115
116
117
# File 'lib/eco/api/common/people/person_entry.rb', line 115

def internal_entry # rubocop:disable Style/TrivialAccessors
  @internal_entry
end

#mapped_entryHash

Note:

at this stage, only the header names have been converted/transformed.

Returns entry Hash with internal attribute names, but external types and values.

Returns:

  • (Hash)

    entry Hash with internal attribute names, but external types and values.



122
123
124
# File 'lib/eco/api/common/people/person_entry.rb', line 122

def mapped_entry # rubocop:disable Style/TrivialAccessors
  @mapped_entry
end

#nameString?

Returns the name of this person if defined.

Returns:

  • (String, nil)

    the name of this person if defined.



177
178
179
# File 'lib/eco/api/common/people/person_entry.rb', line 177

def name
  final_entry['name']
end

#name?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/eco/api/common/people/person_entry.rb', line 181

def name?
  final_entry.key?('name')
end

#new(data) ⇒ PersonEntry

Generates a new entry

Returns:



73
74
75
76
77
78
79
80
81
# File 'lib/eco/api/common/people/person_entry.rb', line 73

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.

Returns:

  • (Boolean)

    returns true if we are parsing, false otherwise.



134
135
136
# File 'lib/eco/api/common/people/person_entry.rb', line 134

def parsing?
  !@source.is_a?(Ecoportal::API::Internal::Person)
end

#policy_group_idsObject



223
224
225
# File 'lib/eco/api/common/people/person_entry.rb', line 223

def policy_group_ids
  final_entry['policy_group_ids'] || []
end

#policy_group_ids?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/eco/api/common/people/person_entry.rb', line 227

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.

Returns:

  • (Boolean)

    returns true if we are serializing, false otherwise.



140
141
142
# File 'lib/eco/api/common/people/person_entry.rb', line 140

def serializing?
  !parsing?
end

#set_account(person, exclude: nil) ⇒ Object

Note:

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.

Parameters:

  • person (Ecoportal::API::Internal::Person)

    the person we want to set the account values to.

  • exclude (String, Array<String>) (defaults to: nil)

    account properties that should not be set/changed to the person.



286
287
288
289
290
291
292
293
# File 'lib/eco/api/common/people/person_entry.rb', line 286

def (person, exclude: nil)
  person. = {} unless person.
  scoped_attrs = @emap.(@final_entry) - into_a(exclude)

  @final_entry.slice(*scoped_attrs).each do |attr, value|
    set_part(person., attr, value)
  end
end

#set_core(person, exclude: nil) ⇒ Object

Note:
  1. 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.
  2. 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.

Parameters:

  • person (Ecoportal::API::V1::Person)

    the person we want to set the core values to.

  • exclude (String, Array<String>) (defaults to: nil)

    core attributes that should not be set/changed to the person.



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/eco/api/common/people/person_entry.rb', line 267

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

Note:

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.

Parameters:

  • person (Ecoportal::API::V1::Person)

    the person we want to set the schema fields' values to.

  • exclude (String, Array<String>) (defaults to: nil)

    schema field attributes that should not be set/changed to the person.



301
302
303
304
305
306
307
308
309
310
# File 'lib/eco/api/common/people/person_entry.rb', line 301

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_idString?

Returns the supervisor id of this person if defined.

Returns:

  • (String, nil)

    the supervisor id of this person if defined.



195
196
197
# File 'lib/eco/api/common/people/person_entry.rb', line 195

def supervisor_id
  final_entry['supervisor_id']
end

#supervisor_id=(value) ⇒ Object



199
200
201
# File 'lib/eco/api/common/people/person_entry.rb', line 199

def supervisor_id=(value)
  final_entry['supervisor_id'] = value
end

#supervisor_id?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/eco/api/common/people/person_entry.rb', line 203

def supervisor_id?
  final_entry.key?('supervisor_id')
end

#to_s(options) ⇒ String

Provides a reference of this person entry.

Returns:

  • (String)

    string summary of this person identity.



251
252
253
254
255
256
257
258
# File 'lib/eco/api/common/people/person_entry.rb', line 251

def to_s(options)
  options = into_a(options)
  return identify if options.include?(:identify)

  final_entry.each.map do |k, v|
    "'#{k}': '#{v.to_json}'"
  end.join(' | ')
end