Module: Institutions

Defined in:
lib/institutions.rb,
lib/institutions/version.rb,
lib/institutions/institution.rb,
lib/institutions/institution/mvc.rb,
lib/institutions/institution/auth.rb,
lib/institutions/institution/core.rb,
lib/institutions/institution/util.rb,
lib/institutions/institution/merge.rb,
lib/institutions/institution/parents.rb,
lib/institutions/institution/services.rb,
lib/institutions/institution/ip_addresses.rb

Overview

:no_doc

Defined Under Namespace

Modules: Auth, Core, IpAddresses, Merge, Mvc, Parents, Services, Util Classes: Institution

Constant Summary collapse

DEFAULT_LOADPATH =

Default paths/files for the module.

"./config"
DEFAULT_FILENAME =
"institutions.yml"
VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.defaultsObject

Returns an Array of Institutions



48
49
50
# File 'lib/institutions.rb', line 48

def self.defaults
  return institutions.values.find_all { |institution| institution.default? }
end

.empty?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/institutions.rb', line 91

def self.empty?
  institutions.empty?
end

.filenamesObject



24
25
26
# File 'lib/institutions.rb', line 24

def self.filenames
  @filenames ||= [DEFAULT_FILENAME]
end

.institutionsObject

Returns a Hash of Institution instances with the Institution#code as the Hash key. Load file order can be change by the calling application by using Array methods. The default load file in Rails apps is

"#{Rails.root}/config/institutions.yml"

and if not Rails

"./config/institutions.yml"

To manipulate load path order and/or add directories to the path

Institutions.loadpaths << File.join("path", "to", "new", "load", "directory")

To manipulate file name order and/or add file names

Institutions.filenames << "newfile.yml"


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/institutions.rb', line 69

def self.institutions
  unless @institutions
    raise NameError.new("No load path was specified.") if loadpaths.nil?
    raise NameError.new("No files named #{filenames} exist to load in the configured load paths, #{loadpaths}. ") if filenames.empty?
    @institutions = {}
    loadfiles.each do |loadfile|
      # Loop through institutions in the yaml
      YAML.load(ERB.new(File.read(loadfile)).result).each_pair do |code, elements|
        code = code.to_sym
        # Merge the new elements or add a new Institution
        @institutions.has_key?(code) ?
          @institutions[code].merge(elements) :
            @institutions[code] =
              Institution.new(code, elements["name"] ? elements["name"] : code.to_s, elements)
      end
    end
    # Handle inheritance for institutions
    merge_parents
  end
  @institutions
end

.institutions?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/institutions.rb', line 95

def self.institutions?
  (not empty?)
end

.loadfilesObject

Intended for internal use only.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/institutions.rb', line 29

def self.loadfiles
  loadfiles = []
  if loadfiles.empty?
    loadpaths.each do |loadpath|
      filenames.each do |filename|
        loadfile = File.join((loadpath.is_a? Proc) ? loadpath.call : loadpath, filename)
        loadfiles<< loadfile if File.exists?(loadfile)
      end
    end
  end
  loadfiles
end

.loadpathsObject



13
14
15
16
17
# File 'lib/institutions.rb', line 13

def self.loadpaths
  @loadpaths ||=
    [(defined?(::Rails) and ::Rails.version >= '3.0.1' ) ?
      self.rails_loadpath : DEFAULT_LOADPATH]
end

.merge_parentsObject

Handle inheritance for institutions



100
101
102
103
104
105
# File 'lib/institutions.rb', line 100

def self.merge_parents
  @institutions.each do |key, institution|
    parent_code = institution.parent_code
    institution.merge_parent(@institutions[parent_code]) if parent_code
  end
end

.rails_loadpathObject

Necessary to use a proc to generate the rails root a bit later.



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

def self.rails_loadpath
  lambda {return "#{Rails.root}/config"}
end

.reloadObject



42
43
44
45
# File 'lib/institutions.rb', line 42

def self.reload
  @institutions = nil
  institutions
end

.with_ip(ip) ⇒ Object

Returns an Array of Institutions that contain the given IP.



53
54
55
# File 'lib/institutions.rb', line 53

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