module Ecoportal module API class V1 class Person < Common::BaseModel passthrough :id, :external_id, :name, :email, :supervisor_id, to: :doc def supervisor(client) return @supervisor if defined?(@supervisor) return @supervisor = nil if supervisor_id.nil? @supervisor = client.people.get(supervisor_id).result end def supervisor=(person) self.supervisor_id = person.id end def as_json super.merge "details" => details&.as_json end def details return @details if defined?(@details) return @details = nil if doc["details"].nil? @details = person_details_class.new(doc["details"]) end def details=(value) case value when NilClass doc["details"] = nil when person_details_class doc["details"] = value.as_json when Hash doc["details"] = value.slice("schema_id", "fields") else raise "Invalid set on details: Need nil, PersonDetails or Hash; got #{value.class}" end remove_instance_variable("@details") if defined?(@details) return details end def add_details(schema_id) self.details = person_details_class.new self.details.schema_id = schema_id end private def person_details_class V1::PersonDetails end end end end end