module Ecoportal module API class V1 class PersonDetails < Common::BaseModel passthrough :schema_id class_resolver :schema_field_value_class, "Ecoportal::API::V1::SchemaFieldValue" def as_json super.merge "fields" => fields.map(&:as_json) end # Sets the `id` of the PersonDetails. # @note unless the new `id` is `nil`, this does not reset the `fields`. # @param value [nil, String] the id of the schema. def schema_id=(value) @fields = [] if value.nil? doc["schema_id"] = value end # Gets all the fields of the PersonDetails. # @return [Array] the array of fields of the schema. def fields return @fields if defined?(@fields) @fields = (doc["fields"] || []).map do |field| schema_field_value_class.new(field) end end # Gets one specific field of the PersonDetails. # @param id [String] the `id` or the `alt_id` of the target field. # @return [nil, SchemaFieldValue] the field or `nil` if missing. def get_field(id) @fields_by_id or index_fields @fields_by_id[id] || @fields_by_alt_id[id] end # Gets the value of one specific field of the PersonDetails. # @param id [String] the `id` or the `alt_id` of the target field. # @return [String, Array, Boolean, Array, Date, Array, Numberic, Array] the value of field or `nil` if missing. def [](id) get_field(id)&.value end # Sets the value to one specific field of the PersonDetails. # @param id [String] the `id` or the `alt_id` of the target field. # @return [void] def []=(id, value) if field = get_field(id) field.value = value else raise "details[#{id.inspect}] is missing. Did you forget to load the schema?" end end protected # Rebuilds the internal `id` and `alt_id` references to the fields. def index_fields @fields_by_id = {} @fields_by_alt_id = {} fields.each do |wrapped| @fields_by_id[wrapped.id] = wrapped @fields_by_alt_id[wrapped.alt_id] = wrapped end end end end end end