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

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

Instance Method Summary collapse

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 manage logs.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/eco/api/common/people/person_entry.rb', line 17

def initialize(data, person_parser:, attr_map:, dependencies: {}, logger: ::Logger.new(IO::NULL))
  raise "Constructor needs a PersonParser. Given: #{parser}" if !person_parser.is_a?(Eco::API::Common::People::PersonParser)
  raise "Expecting Mapper object. Given: #{attr_map}" 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
end

Instance Method Details

#default_tagObject



153
154
155
# File 'lib/eco/api/common/people/person_entry.rb', line 153

def default_tag
  final_entry["default_tag"]
end

#default_tag?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/eco/api/common/people/person_entry.rb', line 149

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.



112
113
114
# File 'lib/eco/api/common/people/person_entry.rb', line 112

def email
  final_entry["email"]
end

#email?Boolean

Returns:

  • (Boolean)


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

def email?
  final_entry.key?("email")
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.



50
51
52
# File 'lib/eco/api/common/people/person_entry.rb', line 50

def external_entry
  @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.



94
95
96
# File 'lib/eco/api/common/people/person_entry.rb', line 94

def external_id
  final_entry["external_id"]
end

#external_id?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/eco/api/common/people/person_entry.rb', line 98

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

#filter_tagsObject



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

def filter_tags
  final_entry["filter_tags"] || []
end

#filter_tags?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/eco/api/common/people/person_entry.rb', line 137

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.



62
63
64
# File 'lib/eco/api/common/people/person_entry.rb', line 62

def final_entry
  @final_entry
end

#idString?

Returns the internal id of this person if defined.

Returns:

  • (String, nil)

    the internal id of this person if defined.



85
86
87
# File 'lib/eco/api/common/people/person_entry.rb', line 85

def id
  final_entry["id"]
end

#id?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/eco/api/common/people/person_entry.rb', line 89

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

#idxInteger

Note:

Eco::API::Common::People::EntryFactory#entries adds this idx

Returns the entry number in the input file.

Returns:

  • (Integer)

    the entry number in the input file



80
81
82
# File 'lib/eco/api/common/people/person_entry.rb', line 80

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.



56
57
58
# File 'lib/eco/api/common/people/person_entry.rb', line 56

def internal_entry
  @internal_entry
end

#nameString?

Returns the name of this person if defined.

Returns:

  • (String, nil)

    the name of this person if defined.



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

def name
  final_entry["name"]
end

#name?Boolean

Returns:

  • (Boolean)


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

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

#new(data) ⇒ PersonEntry

Generates a new entry

Returns:



44
45
46
# File 'lib/eco/api/common/people/person_entry.rb', line 44

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.



68
69
70
# File 'lib/eco/api/common/people/person_entry.rb', line 68

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

#policy_group_idsObject



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

def policy_group_ids
  final_entry["policy_group_ids"] || []
end

#policy_group_ids?Boolean

Returns:

  • (Boolean)


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

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.



74
75
76
# File 'lib/eco/api/common/people/person_entry.rb', line 74

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.



189
190
191
192
193
194
195
196
# File 'lib/eco/api/common/people/person_entry.rb', line 189

def (person, exclude: nil)
  person. = {} if !person.
  person..permissions_preset = nil unless person..permissions_preset = "custom"
  scoped_attrs = @emap. - 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:

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.

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.



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

def set_core(person, exclude: nil)
  scoped_attrs = @emap.core_attrs - into_a(exclude)
  @final_entry.slice(*scoped_attrs).each do |attr, value|
    set_part(person, attr, value)
  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.

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.



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

def set_details(person, exclude: nil)
  person.add_details(@person_parser.schema) if !person.details || !person.details.schema_id
  scoped_attrs = @emap.details_attrs - 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.



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

def supervisor_id
  final_entry["supervisor_id"]
end

#supervisor_id=(value) ⇒ Object



125
126
127
# File 'lib/eco/api/common/people/person_entry.rb', line 125

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

#supervisor_id?Boolean

Returns:

  • (Boolean)


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

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

#to_s(options) ⇒ String

Provides a reference of this person.

Returns:

  • (String)

    string summary of this person identity.



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/eco/api/common/people/person_entry.rb', line 159

def to_s(options)
  options = into_a(options)
  case
  when options.include?(:identify)
    str_id = id ? "id: '#{id}'; " : ""
    "(row: #{idx}) '#{name}' (#{str_id}ext_id: '#{external_id}'; email: '#{email}')"
  else
    final_entry.each.map do |k, v|
      "'#{k}': '#{v.to_json}'"
    end.join(" | ")
  end
end