module Ecoportal module API class V1 class PersonDetails < Common::BaseModel passthrough :schema_id, to: :doc def as_json super.merge "fields" => fields.map(&:as_json) end def schema_id=(value) @fields = [] if value.nil? doc["schema_id"] = value end def fields return @fields if defined?(@fields) @fields = (doc["fields"] || []).map do |field| schema_field_value_class.new(field) end end 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 def get_field(id) @fields_by_id or index_fields @fields_by_id[id] || @fields_by_alt_id[id] end def [](id) get_field(id)&.value end def []=(id, value) if field = get_field(id) field.value = value end end private def schema_field_value_class V1::SchemaFieldValue end end end end end