module Eco module API module Common module People class Entries < Eco::Language::Models::Collection # build the shortcuts of Collection attr_collection :id, :external_id, :email, :name, :supervisor_id alias_method :entries, :to_a include Common::People def initialize(data = [], klass:, factory:) super(data, klass: klass, factory: factory) @caches_init = false end def [](id_or_ext) self.id(id_or_ext) || self.external_id(id_or_ext) end #def []=(id, value) # if self.id?(id) #end def id(*args) attr('id', *args).first end def external_id(*args) attr('external_id', *args).first end def entry(id: nil, external_id: nil, email: nil) init_caches pers = @by_id[id]&.first if id pers = @by_external_id[external_id&.strip]&.first if !pers && !external_id.to_s.strip.empty? pers = @by_email[email&.downcase.strip]&.first if !pers && !email.to_s.strip.empty? pers = @by_external_id[email&.downcase.strip]&.first if !pers && !email.to_s.strip.empty? pers end def find(object) id = object.respond_to?("id")? object.send("id") : nil external_id = object.respond_to?("external_id")? object.send("external_id") : nil email = object.respond_to?("email")? object.send("email") : nil entry(id: id, external_id: external_id, email: email) end def exclude(object) exclude_people(into_a(object)) end def exclude_people(list) discarded = list.map do |person| find(person) end.compact newFrom self.to_a - discarded end def email_id_maps self.email_present.group_by(:email).transform_values { |person| person.id } end def group_by_supervisor self.to_h(:supervisor_id) end def to_h(attr) super(attr || "id") end protected def on_change @caches_init = false end private def init_caches return if @caches_init @by_id = self.to_h('id') @by_external_id = self.to_h('external_id') @by_email = self.to_h('email') @caches_init = true end end end end end end