module Eco module API module Organization class PersonSchemas < Eco::Language::Models::Collection # build the shortcuts of Collection attr_collection :id, :name def initialize(schemas = [], klass: Ecoportal::API::Internal::PersonSchema) @klass = Ecoportal::API::Internal::PersonSchema @caches_init = false super(schemas, klass: @klass) init_caches end def to_id(name) case name when Enumerable name.map do |n| schema(n)&.id end.compact else schema(name)&.id end end def to_name(id) schema(id)&.name end def schema(id_name) self[id_name] end def [](id_name) @by_id[schema_id(id_name)] end private def schema_name(id_name) (@by_id[id_name] || @by_name[id_name&.downcase])&.name&.downcase end def schema_id(id_name) (@by_name[id_name&.downcase] || @by_id[id_name])&.id end def init_caches return if @caches_init @by_id = self.map { |pg| [pg.id, pg] }.to_h @by_name = self.map { |pg| [pg.name&.downcase, pg] }.to_h @caches_init = true end end end end end