class Ecoportal::API::V1::PersonDetails
Public Instance Methods
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<String>, Boolean, Array<Boolean>, Date, Array<Date>, Numberic, Array<Numeric>] the value of field or `nil` if missing.
# File lib/ecoportal/api/v1/person_details.rb, line 41 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]
# File lib/ecoportal/api/v1/person_details.rb, line 48 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
Ecoportal::API::Common::BaseModel#as_json
# File lib/ecoportal/api/v1/person_details.rb, line 9 def as_json super.merge "fields" => fields.map(&:as_json) end
Gets all the fields of the PersonDetails
. @return [Array<SchemaFieldValue>] the array of fields of the schema.
# File lib/ecoportal/api/v1/person_details.rb, line 23 def fields return @fields if defined?(@fields) @fields = (doc["fields"] || []).each_with_index.map do |field, i| schema_field_value_class.new(field, parent: self, key: ["fields", i]) 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.
# File lib/ecoportal/api/v1/person_details.rb, line 33 def get_field(id) @fields_by_id or index_fields @fields_by_id[id] || @fields_by_alt_id[id] 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.
# File lib/ecoportal/api/v1/person_details.rb, line 16 def schema_id=(value) @fields = [] if value.nil? doc["schema_id"] = value end
Protected Instance Methods
Rebuilds the internal `id` and `alt_id` references to the fields.
# File lib/ecoportal/api/v1/person_details.rb, line 59 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