lib/ecoportal/api/v1/person.rb in ecoportal-api-0.7.1 vs lib/ecoportal/api/v1/person.rb in ecoportal-api-0.7.2

- old
+ new

@@ -13,10 +13,11 @@ class_resolver :person_schema_class, "Ecoportal::API::V1::PersonSchema" class_resolver :person_details_class, "Ecoportal::API::V1::PersonDetails" embeds_one :details, nullable: true, klass: :person_details_class VALID_TAG_REGEX = /^[A-Za-z0-9 &_'\/-]+$/ + VALID_EMAIL_REGEX = /^[^@\s]+@[^@\s]+\.[^@\s]+$/ # Gets the supervisor (`Person`) of this person, with given his `supervisor_id`. # # **Example Usage**: # ```ruby @@ -39,12 +40,21 @@ # @param person [Person, nil] the supervisor of this person. def supervisor=(person) self.supervisor_id = person&.id || person&.external_id end + # Sets the email of a person. + # @param email [String, nil] the email of this person. + def email=(value) + unless !value || value.match(VALID_EMAIL_REGEX) + raise "Invalid email #{email.inspect}" + end + doc["email"] = value&.downcase + end + # Validates the string tags of the array, and sets the `filter_tags` property of the account. - # @note all is set in upper case. + # @note all is set in upper case and preserves the original order. # @raise [Exception] if there was any invalid string tag. # @param value [Array<String>] array of tags. def filter_tags=(value) unless value.is_a?(Array) raise "filter_tags= needs to be passed an Array, got #{value.class}" @@ -52,14 +62,19 @@ end_tags = value.map do |tag| unless tag.match(VALID_TAG_REGEX) raise "Invalid filter tag #{tag.inspect}" end tag.upcase - end + end.uniq ini_tags = (original_doc && original_doc["filter_tags"]) || [] # preserve original order to avoid false updates doc["filter_tags"] = (ini_tags & end_tags) + (end_tags - ini_tags) + end + + # @return [Array<String>] the filter tags of this person. + def filter_tags + doc["filter_tags"] ||= [] end def as_json super.merge "details" => details&.as_json end