module StudyEngine class StudyID class Bank < ActiveRecord::Base self.table_name = "study_engine_study_ids" def self.find_or_create_from_full_study_id study_id study_id_study, study_id_site, study_id_id = study_id.split("-") find_or_create_by study_id_study: study_id_study, study_id_site: study_id_site, study_id_id: study_id_id end def self.studies pluck(:study_id_study).uniq.sort end def self.sites pluck(:study_id_site).uniq.sort end def self.ids_grouped_by_study_and_site all.reduce(Hash.new([])) do |hash, study_id| hash[study_id.study_id_study + study_id.study_id_site] += [study_id.study_id_id] hash end end def self.studies_grouped_by_site all.reduce(Hash.new(Set.new)) do |hash, study_id| hash[study_id.study_id_site] += [study_id.study_id_study] hash end end validates :study_id_study, :study_id_site, format: /\A[A-Z]{3}\Z/ validates :study_id_id, format: /\A[0-9]{4}\Z/ end end end