lib/ecoportal/api/v1/person.rb in ecoportal-api-0.9.7 vs lib/ecoportal/api/v1/person.rb in ecoportal-api-0.10.0

- old
+ new

@@ -7,18 +7,20 @@ # @attr supervisor_id [String] internal or external id of the supervisor of this person. # @attr contractor_organization_id [String] internal id of the contractor entity of this person. # @attr details [PersonDetails, nil] the details of the person or `nil` if missing. class Person < Common::BaseModel passthrough :id, :external_id, :name, :email, :filter_tags + passthrough :archived passthrough :supervisor_id, :contractor_organization_id + passthrough :brand_id passthrough :freemium 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_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**: @@ -45,30 +47,30 @@ end # Sets the email of a person. # @param value [String, nil] the email of this person. def email=(value) - unless !value || value.match(VALID_EMAIL_REGEX) - raise "Invalid email #{value.inspect}" - end + raise "Invalid email #{value.inspect}" if value && !value.match(VALID_EMAIL_REGEX) + 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 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}" - end + msg = "filter_tags= needs to be passed an Array, got #{value.class}" + raise ArgumentError, msg unless value.is_a?(Array) + end_tags = value.compact.map do |tag| - unless tag.match(VALID_TAG_REGEX) - raise "Invalid filter tag #{tag.inspect}" - end + msg = "Invalid filter tag #{tag.inspect}" + raise ArgumentError, msg unless tag.match(VALID_TAG_REGEX) + tag.upcase end + set_uniq_array_keep_order("filter_tags", end_tags) end # @return [Array<String>] the filter tags of this person. def filter_tags @@ -103,12 +105,14 @@ end remove_instance_variable("@details") if defined?(@details) end # Sets the PersonDetails to the person, depending on the parameter received: - # - `PersonSchema`: initializes the `PersonDetails` as per the schema specified (`schema_id` and `fields`). - # - `String`: it just sets the `schema_id` on the `PersonDetails` (as `fields` is not include, `details[key]=` will throw error). + # - `PersonSchema`: initializes the `PersonDetails` as per the schema specified + # (`schema_id` and `fields`). + # - `String`: it just sets the `schema_id` on the `PersonDetails` + # (as `fields` is not include, `details[key]=` will throw error). # (see #details=) # @note # - this method alone only sets the internal structure of the details. # - you will not be able to `reset!` after using this method. # @param schema_or_id [PersonSchema, String, nil, PersonDetails, Hash] value to be set. @@ -128,10 +132,9 @@ original_doc["details"] = { "fields" => JSON.parse(doc["details"]["fields"].to_json) } end end - end end end end