Class: InstitutionList

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/authpds/institution_list.rb

Constant Summary collapse

@@institutions_yaml_path =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstitutionList

Returns a new instance of InstitutionList.



5
6
7
# File 'lib/authpds/institution_list.rb', line 5

def initialize
  @institutions = nil
end

Class Method Details

.institutions_defined?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/authpds/institution_list.rb', line 15

def self.institutions_defined?
  return !@@institutions_yaml_path.nil?
end

.yaml_path=(path) ⇒ Object

Used for initialization and testing



10
11
12
13
# File 'lib/authpds/institution_list.rb', line 10

def self.yaml_path=(path)
  @@institutions_yaml_path = path
  self.instance.reload
end

Instance Method Details

#defaultsObject

Returns an array of Institutions



25
26
27
# File 'lib/authpds/institution_list.rb', line 25

def defaults
  return institutions.values.find_all {|institution| institution.default === true}
end

#get(name) ⇒ Object

Returns an Institution



20
21
22
# File 'lib/authpds/institution_list.rb', line 20

def get(name)
  return institutions[name]
end

#institutionsObject

Load institutions from the YAML file and return as a hash.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/authpds/institution_list.rb', line 42

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)
    institutions_hash = YAML.load_file( @@institutions_yaml_path )
    institutions_with_parents = {}
    # Prepare institution definitions
    institutions_hash.each do |name, definition|
      definition["name"] = name
      definition["default"] = false unless definition.key?("default")
      institutions_with_parents[name] = definition if definition.key?("parent_institution")
    end
    # Handle inheritance for institutions
    institutions_with_parents.each do |name, definition|
      institutions_hash[name] =  merge_with_parent(institutions_hash, definition)
    end
    # Turn the institution definitions to Institutions
    @institutions = {}
    institutions_hash.each do |name, definition|
      @institutions[name] = Institution.new(definition)
    end
  end
  return @institutions
end

#institutions_with_ip(ip) ⇒ Object

Returns an array of Institutions



30
31
32
# File 'lib/authpds/institution_list.rb', line 30

def institutions_with_ip(ip)
  return institutions.values.find_all { |institution| institution.includes_ip?(ip) }
end

#reloadObject

Reload institutions from the YAML file.



35
36
37
38
39
# File 'lib/authpds/institution_list.rb', line 35

def reload
  @institutions = nil
  institutions
  true
end