Class: Eco::API::Common::People::PersonEntry
- Inherits:
-
Object
- Object
- Eco::API::Common::People::PersonEntry
- Defined in:
- lib/eco/api/common/people/person_entry.rb
Instance Method Summary collapse
- #doc ⇒ Object
-
#email ⇒ String?
The email of this person if defined.
- #email? ⇒ Boolean
-
#external_entry ⇒ Hash
Entry represented in a
Hashwith external attribute names and values thereof. -
#external_id ⇒ String?
The external id of this person if defined.
- #external_id? ⇒ Boolean
- #filter_tags ⇒ Object
- #filter_tags? ⇒ Boolean
-
#id ⇒ String?
The internal id of this person if defined.
- #id? ⇒ Boolean
-
#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 ⇒ Object
-
#name ⇒ String?
The name of this person if defined.
- #name? ⇒ Boolean
-
#parsing? ⇒ Boolean
To know if currently the object is in parse or serialize mode.
-
#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
accountproperties of thePersonthat are present in theEntry. -
#set_core(person, exclude: nil) ⇒ Object
Setter to fill in all the
coreproperties of thePersonthat are present in theEntry. -
#set_details(person, exclude: nil) ⇒ Object
Setter to fill in all the schema
detailsfields of thePersonthat are present in theEntry. -
#supervisor_id ⇒ String?
The supervisor id of this person if defined.
- #supervisor_id=(value) ⇒ Object
- #supervisor_id? ⇒ Boolean
-
#to_hash ⇒ Hash
Entry represented in a
Hashwith external attribute names and values thereof. -
#to_s(options) ⇒ String
Provides a reference of this person.
Constructor Details
#initialize(data, person_parser:, attr_map:, dependencies: {}, logger: ::Logger.new(IO::NULL)) ⇒ PersonEntry
- if
datais aPersonobject, its behaviour isserialise. - if
datais not aPersonobject, 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.
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
#doc ⇒ Object
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 account_attrs = @emap.account_attrs 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 account_attrs.empty? account_hash = internal_entry.slice(*account_attrs) core_hash.merge!({ "account" => account_hash }) end end end |
#email ⇒ String?
Returns 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
84 85 86 |
# File 'lib/eco/api/common/people/person_entry.rb', line 84 def email? @internal_entry.key?("email") end |
#external_entry ⇒ Hash
normally used to obtain a serialized entry.
Entry represented in a Hash with external attribute names and values thereof.
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_id ⇒ String?
Returns 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
66 67 68 |
# File 'lib/eco/api/common/people/person_entry.rb', line 66 def external_id? @internal_entry.key?("external_id") end |
#filter_tags ⇒ Object
101 102 103 |
# File 'lib/eco/api/common/people/person_entry.rb', line 101 def @internal_entry["filter_tags"] end |
#filter_tags? ⇒ Boolean
105 106 107 |
# File 'lib/eco/api/common/people/person_entry.rb', line 105 def @internal_entry.key?("filter_tags") end |
#id ⇒ String?
Returns 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
57 58 59 |
# File 'lib/eco/api/common/people/person_entry.rb', line 57 def id? @internal_entry.key?("id") end |
#internal_entry ⇒ Object
181 182 183 |
# File 'lib/eco/api/common/people/person_entry.rb', line 181 def internal_entry @internal_entry end |
#name ⇒ String?
Returns 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
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.
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.
48 49 50 |
# File 'lib/eco/api/common/people/person_entry.rb', line 48 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.
154 155 156 157 158 159 160 161 |
# File 'lib/eco/api/common/people/person_entry.rb', line 154 def set_account(person, exclude: nil) person.account = {} if !person.account person.account. = nil unless person.account. = "custom" scoped_attrs = @emap.account_attrs - into_a(exclude) @internal_entry.slice(*scoped_attrs).each do |attr, value| _set_to_account(person, 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.
Setter to fill in all the core properties of the Person that are present in the Entry.
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
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.
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_id ⇒ String?
Returns 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
97 98 99 |
# File 'lib/eco/api/common/people/person_entry.rb', line 97 def supervisor_id? @internal_entry.key?("supervisor_id") end |
#to_hash ⇒ Hash
normally used to obtain a serialized entry.
Entry represented in a Hash with external attribute names and values thereof.
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.
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() = into_a() case when .include?(:identify) "'#{name}' ('#{external_id}': '#{email}')" else @internal_entry.each.map do |k, v| "'#{k}': '#{v.to_json}'" end.join(" | ") end end |