Sha256: f7572cec4cc774655d0e3338431f9f55edd27ec5168fadf9c1ea953dac988ac6

Contents?: true

Size: 1.93 KB

Versions: 10

Compression:

Stored size: 1.93 KB

Contents

class InstitutionList
  include Singleton # get the instance with InstitutionList.instance
  @@institutions_yaml_path = nil

  def initialize
    @institutions = nil
  end

  # Used for initialization and testing
  def self.yaml_path=(path)
    @@institutions_yaml_path = path
    self.instance.reload
  end
  
  def self.institutions_defined?
    return !@@institutions_yaml_path.nil?
  end

  # Returns an Institution
  def get(name)
    return institutions[name]
  end

  # Returns an array of Institutions
  def default_institutions
    return institutions.values.find_all {|institution| institution.default_institution == true}
  end

  # Returns an array of Institutions
  def institutions_with_ip(ip)
    return institutions.values.find_all { |institution| institution.includes_ip?(ip) }
  end

  # Reload institutions from the YAML file.
  def reload
    @institutions = nil
    institutions
    true
  end

  # Load institutions from the YAML file and return as a hash.
  def institutions
    unless @institutions
      raise ArgumentError.new("institutions_yaml_path was not specified.") if @@institutions_yaml_path.nil?
      raise NameError.new(
        "The file #{@@institutions_yaml_path} does not exist. "+
        "In order to use the institution feature you must create the file."
      ) unless File.exists?(@@institutions_yaml_path)
      institution_list = YAML.load_file( @@institutions_yaml_path )
      @institutions = {}
      # Turn the institution hashes to Institutions
      institution_list.each_pair do |institution_name, institution_hash|
        institution_hash["name"] = institution_name
        # Merge with parent institution
        institution_hash = 
          institution_list[institution_hash["parent_institution"]].
            merge(institution_hash) unless institution_hash["parent_institution"].nil?
        @institutions[institution_name] = Institution.new(institution_hash)
      end
    end
    return @institutions
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
authpds-0.0.14 lib/authpds/institution_list.rb
authpds-0.0.13 lib/authpds/institution_list.rb
authpds-0.0.12 lib/authpds/institution_list.rb
authpds-0.0.11 lib/authpds/institution_list.rb
authpds-0.0.10 lib/authpds/institution_list.rb
authpds-0.0.9 lib/authpds/institution_list.rb
authpds-0.0.8 lib/authpds/institution_list.rb
authpds-0.0.7 lib/authpds/institution_list.rb
authpds-0.0.6 lib/authpds/institution_list.rb
authpds-0.0.5 lib/authpds/institution_list.rb