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, 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
# 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     = data
    @serialized_entry   = _mapped_entry(@external_entry)
    @internal_entry     = _internal_entry(@serialized_entry)
  else  # SERIALIZING

    @person             = data
    @internal_entry     = _internal_entry(@person)
    @serialized_entry   = _mapped_entry(@internal_entry)
    #@external_entry     = external_entry

  end
end

Instance Method Details

#docObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/eco/api/common/people/person_entry.rb', line 185

def doc
  return @person.doc if instance_variable_defined?(:@person) && @person

  core_attrs    = @emap.core_attrs
  details_attrs = @emap.details_attrs
   = @emap.

  internal_entry.slice(*core_attrs).tap do |core_hash|
    unless details_attrs.empty?
      schema_id      = @person_parser.schema.id
      details_fields = @person_parser.schema.doc["fields"].each_with_object([]) do |fld, flds|
        if details_attrs.include?(fld.alt_id)
          flds << fld.merge("value" => internal_entry[fld.alt_id]).slice("id", "alt_id", "type", "name", "shared", "multiple", "value")
        end
      end
      core_hash.merge!({
        "details" => {
          "schema_id" => schema_id,
          "fields"    => details_fields
        }
      })
    end

    unless .empty?
       = internal_entry.slice(*)
      core_hash.merge!({
        "account" => 
      })
    end
  end
end

#emailString?

Returns the email of this person if defined.

Returns:

  • (String, nil)

    the email of this person if defined.



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

def email
  @internal_entry["email"]
end

#email?Boolean

Returns:

  • (Boolean)


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

def email?
  @internal_entry.key?("email")
end

#external_entryHash

Note:

normally used to obtain a serialized entry.

Entry represented in a Hash with external attribute names and values thereof.

Returns:

  • (Hash)

    with external names and values.



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

def external_entry
  @emap.all_attrs.each_with_object({}) do |attr, hash|
    unless hash.key?(ext_attr = @emap.to_external(attr))
      hash[ext_attr] = @serialized_entry[attr]
    end
  end
end

#external_idString?

Returns the external id of this person if defined.

Returns:

  • (String, nil)

    the external id of this person if defined.



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

def external_id
  @internal_entry["external_id"]
end

#external_id?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/eco/api/common/people/person_entry.rb', line 66

def external_id?
  @internal_entry.key?("external_id")
end

#filter_tagsObject



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

def filter_tags
  @internal_entry["filter_tags"]
end

#filter_tags?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/eco/api/common/people/person_entry.rb', line 105

def filter_tags?
  @internal_entry.key?("filter_tags")
end

#idString?

Returns the internal id of this person if defined.

Returns:

  • (String, nil)

    the internal id of this person if defined.



53
54
55
# File 'lib/eco/api/common/people/person_entry.rb', line 53

def id
  @internal_entry["id"]
end

#id?Boolean

Returns:

  • (Boolean)


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

def id?
  @internal_entry.key?("id")
end

#internal_entryObject



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

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.



71
72
73
# File 'lib/eco/api/common/people/person_entry.rb', line 71

def name
  @internal_entry["name"]
end

#name?Boolean

Returns:

  • (Boolean)


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

def name?
  @internal_entry.key?("name")
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.



42
43
44
# File 'lib/eco/api/common/people/person_entry.rb', line 42

def parsing?
  !@source.is_a?(Ecoportal::API::Internal::Person)
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.



48
49
50
# File 'lib/eco/api/common/people/person_entry.rb', line 48

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 (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.



154
155
156
157
158
159
160
161
# File 'lib/eco/api/common/people/person_entry.rb', line 154

def (person, exclude: nil)
  person. = {} if !person.
  person..permissions_preset = nil unless person..permissions_preset = "custom"
  scoped_attrs = @emap. - into_a(exclude)
  @internal_entry.slice(*scoped_attrs).each do |attr, value|
    (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 (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.



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

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



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

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)
  @internal_entry.slice(*scoped_attrs).each do |attr, value|
    _set_to_details(person, 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.



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

def supervisor_id
  @internal_entry["supervisor_id"]
end

#supervisor_id=(value) ⇒ Object



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

def supervisor_id=(value)
  @internal_entry["supervisor_id"] = value
end

#supervisor_id?Boolean

Returns:

  • (Boolean)


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

def supervisor_id?
  @internal_entry.key?("supervisor_id")
end

#to_hashHash

Note:

normally used to obtain a serialized entry.

Entry represented in a Hash with external attribute names and values thereof.

Returns:

  • (Hash)

    with external names and values.



166
167
168
# File 'lib/eco/api/common/people/person_entry.rb', line 166

def to_hash
  external_entry
end

#to_s(options) ⇒ String

Provides a reference of this person.

Returns:

  • (String)

    string summary of this person identity.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/eco/api/common/people/person_entry.rb', line 112

def to_s(options)
  options = into_a(options)
  case
  when options.include?(:identify)
    "'#{name}' ('#{external_id}': '#{email}')"
  else
    @internal_entry.each.map do |k, v|
      "'#{k}': '#{v.to_json}'"
    end.join(" | ")
  end
end