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.0"
Class Method Summary collapse
-
.defaults ⇒ Object
Returns an Array of Institutions.
- .empty? ⇒ Boolean
- .filenames ⇒ Object
-
.institutions ⇒ Object
Returns a Hash of Institution instances with the Institution#code as the Hash key.
- .institutions? ⇒ Boolean
-
.loadfiles ⇒ Object
Intended for internal use only.
- .loadpaths ⇒ Object
-
.merge_parents ⇒ Object
Handle inheritance for institutions.
-
.rails_loadpath ⇒ Object
Necessary to use a proc to generate the rails root a bit later.
- .reload ⇒ Object
-
.with_ip(ip) ⇒ Object
Returns an Array of Institutions that contain the given IP.
Class Method Details
.defaults ⇒ Object
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
91 92 93 |
# File 'lib/institutions.rb', line 91 def self.empty? institutions.empty? end |
.filenames ⇒ Object
24 25 26 |
# File 'lib/institutions.rb', line 24 def self.filenames @filenames ||= [DEFAULT_FILENAME] end |
.institutions ⇒ Object
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
95 96 97 |
# File 'lib/institutions.rb', line 95 def self.institutions? (not empty?) end |
.loadfiles ⇒ Object
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 |
.loadpaths ⇒ Object
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_parents ⇒ Object
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_loadpath ⇒ Object
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 |
.reload ⇒ Object
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 |