institutions 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/.travis.yml +9 -0
- data/Gemfile +2 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +53 -0
- data/institutions.gemspec +24 -0
- data/lib/institutions/institution/auth.rb +6 -0
- data/lib/institutions/institution/core.rb +52 -0
- data/lib/institutions/institution/ip_addresses.rb +39 -0
- data/lib/institutions/institution/merge.rb +85 -0
- data/lib/institutions/institution/mvc.rb +6 -0
- data/lib/institutions/institution/parents.rb +15 -0
- data/lib/institutions/institution/services.rb +6 -0
- data/lib/institutions/institution/util.rb +65 -0
- data/lib/institutions/institution.rb +29 -0
- data/lib/institutions/version.rb +3 -0
- data/lib/institutions.rb +99 -0
- data/test/config/institutions.yml +89 -0
- data/test/config/overwrite.yml +4 -0
- data/test/institution/auth_test.rb +7 -0
- data/test/institution/core_test.rb +34 -0
- data/test/institution/ip_addresses_test.rb +21 -0
- data/test/institution/merge_test.rb +34 -0
- data/test/institution/mvc_test.rb +7 -0
- data/test/institution/parents_test.rb +13 -0
- data/test/institution/services_test.rb +7 -0
- data/test/institution/util_test.rb +13 -0
- data/test/institution_test.rb +7 -0
- data/test/institutions_test.rb +36 -0
- data/test/test_helper.rb +3 -0
- metadata +175 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 Scot Dalton
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
= Institutions
|
2
|
+
{<img src="https://secure.travis-ci.org/scotdalton/institutions.png?branch=master" alt="Build Status" />}[https://travis-ci.org/scotdalton/institutions]
|
3
|
+
{<img src="https://gemnasium.com/scotdalton/institutions.png" alt="Dependency Status" />}[https://gemnasium.com/scotdalton/institutions]
|
4
|
+
{<img src="https://codeclimate.com/badge.png" alt="Code Climage" />}[https://codeclimate.com/github/scotdalton/institutions]
|
5
|
+
|
6
|
+
This gem provides a mechanism for creating Institutions from a hash of Institutional data elements.
|
7
|
+
|
8
|
+
== Institutions Basics
|
9
|
+
=== Getting institutions
|
10
|
+
Institutions reads in YAML files and creates a Hash of Institution instances
|
11
|
+
with the Institution#code as the Hash key.
|
12
|
+
require 'institutions'
|
13
|
+
institutions = Institutions.institutions
|
14
|
+
|
15
|
+
=== Setting load paths
|
16
|
+
Specify additional directories to search for institutional YAMLs.
|
17
|
+
require 'institutions'
|
18
|
+
Institutions.loadpaths << File.join("path", "to", "new", "load", "directory")
|
19
|
+
|
20
|
+
=== Setting file name
|
21
|
+
Specify additional YAML file names that contain institutions.
|
22
|
+
require 'institutions'
|
23
|
+
Institutions.filenames << "my_institution.yml"
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'rake/testtask'
|
16
|
+
require 'git'
|
17
|
+
require 'fileutils'
|
18
|
+
|
19
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'Institutions'
|
22
|
+
rdoc.options << '--line-numbers'
|
23
|
+
rdoc.main = "README.rdoc"
|
24
|
+
rdoc.rdoc_files.include('README.rdoc')
|
25
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
26
|
+
end
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
Rake::TestTask.new(:test) do |t|
|
32
|
+
t.libs << 'lib'
|
33
|
+
t.libs << 'test'
|
34
|
+
t.pattern = 'test/**/*_test.rb'
|
35
|
+
t.verbose = false
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "Push latest RDocs to gh-pages."
|
39
|
+
task :ghpages => [:rerdoc] do
|
40
|
+
g = Git.open(Dir.pwd)
|
41
|
+
g.checkout(g.branch('gh-pages'))
|
42
|
+
g.pull("origin", "gh-pages")
|
43
|
+
FileUtils.rm_r("rdocs", :force => true)
|
44
|
+
Dir.mkdir("rdocs")
|
45
|
+
FileUtils.mv(Dir.glob("html/*"), "./rdocs", :force => true)
|
46
|
+
FileUtils.rm_r("html", :force => true)
|
47
|
+
g.add(".")
|
48
|
+
g.commit_all("Update gh-pages.")
|
49
|
+
g.push("origin", "gh-pages")
|
50
|
+
g.checkout(g.branch('master'))
|
51
|
+
end
|
52
|
+
|
53
|
+
task :default => :test
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require "institutions/version"
|
5
|
+
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "institutions"
|
9
|
+
s.version = Institutions::VERSION
|
10
|
+
s.authors = ["Scot Dalton"]
|
11
|
+
s.email = ["scotdalton@gmail.com"]
|
12
|
+
s.homepage = "https://github.com/scotdalton/institutions"
|
13
|
+
s.summary = "Abstract mechanism for setting up Institutions (whatever that means)."
|
14
|
+
s.description = "Abstract mechanism for setting up Institutions with arbitrary data elements."
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split($\)
|
17
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
18
|
+
|
19
|
+
s.add_dependency "require_all", "~> 1.2.1"
|
20
|
+
s.add_dependency "ipaddr_range_set", "~> 0.9.1"
|
21
|
+
s.add_dependency "git", "~> 1.2.5"
|
22
|
+
s.add_dependency "rdoc"
|
23
|
+
s.add_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Institutions#:no_doc
|
2
|
+
module Core#:no_doc
|
3
|
+
# Required attributes
|
4
|
+
attr_reader :code, :name
|
5
|
+
alias :display_name :name
|
6
|
+
|
7
|
+
# Optional core attributes
|
8
|
+
attr_reader :default
|
9
|
+
alias :default? :default
|
10
|
+
|
11
|
+
#
|
12
|
+
# Creates a new Institution object from the given code, name and hash.
|
13
|
+
#
|
14
|
+
# The optional +hash+, if given, will generate additional attributes and values.
|
15
|
+
#
|
16
|
+
#
|
17
|
+
# For example:
|
18
|
+
#
|
19
|
+
# require 'institutions'
|
20
|
+
# hash = { "attribute1" => "My first attribute.", :array_attribute => [1, 2] }
|
21
|
+
# institution = Institutions::Institution.new("my_inst", "My Institution", hash)
|
22
|
+
#
|
23
|
+
# p institution # -> <Institutions::Institution @code=:my_inst @name="My Institution" @attribute1=My first attribute." @array_attribute=[1, 2] @default=false>
|
24
|
+
#
|
25
|
+
def initialize(code, name, h={})
|
26
|
+
# Set the required attributes
|
27
|
+
set_required_attributes code, name
|
28
|
+
# Merge in the optional hash attribute
|
29
|
+
merge h unless h.nil?
|
30
|
+
# If the institution is named default, take that as an
|
31
|
+
# indication that it's the default institution
|
32
|
+
@default = true if (name.eql? "default" or name.eql? "DEFAULT")
|
33
|
+
# If default was never set, explicitly set default as false.
|
34
|
+
@default = false if default.nil?
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# Sets the required attributes.
|
39
|
+
# Raises an ArgumentError specifying the missing arguments if they are nil.
|
40
|
+
#
|
41
|
+
def set_required_attributes(code, name)
|
42
|
+
missing_arguments = []
|
43
|
+
missing_arguments << :code if code.nil?
|
44
|
+
missing_arguments << :name if name.nil?
|
45
|
+
raise ArgumentError.new("Cannot create the Institution based on the given arguments (:code => #{code.inspect}, :name => #{name.inspect}).\n"+
|
46
|
+
"The following arguments cannot be nil: #{missing_arguments.inspect}") unless missing_arguments.empty?
|
47
|
+
# Set the instance variables
|
48
|
+
@code, @name = code.to_sym, name
|
49
|
+
end
|
50
|
+
private :set_required_attributes
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Institutions#:no_doc
|
2
|
+
module IpAddresses#:no_doc
|
3
|
+
require 'ipaddr_range_set'
|
4
|
+
attr_reader :ip_addresses
|
5
|
+
|
6
|
+
def ip_addresses=(args)
|
7
|
+
args.collect! do |arg|
|
8
|
+
arg.match(/-/) ? convert_to_range(arg) : arg
|
9
|
+
end
|
10
|
+
@ip_addresses = IPAddrRangeSet.new(*args)
|
11
|
+
end
|
12
|
+
protected :ip_addresses=
|
13
|
+
|
14
|
+
def ip_addresses_add(arg1, arg2)
|
15
|
+
arg1.add(arg2)
|
16
|
+
end
|
17
|
+
protected :ip_addresses_add
|
18
|
+
|
19
|
+
def convert_to_range(s)
|
20
|
+
s.split("-")[0]...s.split("-")[1]
|
21
|
+
end
|
22
|
+
private :convert_to_range
|
23
|
+
|
24
|
+
#
|
25
|
+
# Returns a +boolean+ indicating whether the candidate IP
|
26
|
+
# address is in the Institution's IP range.
|
27
|
+
# Example:
|
28
|
+
#
|
29
|
+
# require 'institutions'
|
30
|
+
# institution = Institution.new("my_inst", "My Institution", "ip_addresses" => ["127.0.0.1", 127.0.0.2"])
|
31
|
+
# data.includes_ip?("127.0.0.1") # => true
|
32
|
+
# data.includes_ip?("127.0.0.3") # => false
|
33
|
+
#
|
34
|
+
def includes_ip?(candidate)
|
35
|
+
return false if ip_addresses.nil?
|
36
|
+
return ip_addresses.include? candidate
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Institutions#:no_doc
|
2
|
+
module Merge#:no_doc
|
3
|
+
#
|
4
|
+
# Merges the given arguments into the Institution.
|
5
|
+
# Internally uses a "recursive merge" algorithm to
|
6
|
+
# preserve as much of the original Institution as possible.
|
7
|
+
# Assumes the argument has a to_hash method
|
8
|
+
# Example.
|
9
|
+
#
|
10
|
+
# require 'institutions'
|
11
|
+
# hash1 = { "string_attribute" => "first string",
|
12
|
+
# :nested_hash_attribute => {
|
13
|
+
# :h1 => {:h1_2 => "first12"},
|
14
|
+
# :h2 => {:h2_1 => "first21", :h2_2 => "first22"}},
|
15
|
+
# :array_attribute => [1, 2, 4] }
|
16
|
+
# institution1 = Institutions::Institution.new("first_inst", "First Institution", hash1)
|
17
|
+
# p institution1 # -> #<Institutions::Institution @code=:first_inst, @name="First Institution", @array_attribute=[1, 2, 4], @default=false, @string_attribute="first string", @nested_hash_attribute={:h1=>{:h1_2=>"first12"}, :h2=>{:h2_1=>"first21", :h2_2=>"first22"}}>
|
18
|
+
# hash2 = { "string_attribute" => "second string",
|
19
|
+
# :nested_hash_attribute => {:h1 => {
|
20
|
+
# :h1_2 => "second12"},
|
21
|
+
# :h2 => {:h2_2 => "second22"}},
|
22
|
+
# :array_attribute => [1, 2, 3], :default => true }
|
23
|
+
# institution2 = Institutions::Institution.new("second_inst", "Second Institution", hash2)
|
24
|
+
# p institution2 # -> <Institutions::Institution @code=:second_inst, @name="Second Institution", @array_attribute=[1, 2, 3], @default=true, @string_attribute="second string", @nested_hash_attribute={:h1=>{:h1_2=>"second12"}, :h2=>{:h2_2=>"second22"}}>
|
25
|
+
# institution1.merge(institution2)
|
26
|
+
# p institution1 # -> #<Institutions::Institution @code=:first_inst, @name="First Institution", @array_attribute=[1, 2, 4, 3], @default=true, @string_attribute="second string", @nested_hash_attribute={:h1=>{:h1_2=>"second12"}, :h2=>{:h2_1=>"first21", :h2_2=>"second22"}}>
|
27
|
+
#
|
28
|
+
def merge(arg={})
|
29
|
+
arg.to_hash.each do |key, value|
|
30
|
+
next unless valid_instance_variable? key
|
31
|
+
instance_variable = instance_variablize(key)
|
32
|
+
if instance_variable_defined? instance_variable
|
33
|
+
instance_variable_set(instance_variable, deep_merge(instance_variable_get(instance_variable), value, key))
|
34
|
+
else
|
35
|
+
writer_method = "#{key}=".to_sym
|
36
|
+
if respond_to? writer_method
|
37
|
+
send writer_method, value
|
38
|
+
else
|
39
|
+
next if [:code, :name].include? key.to_sym
|
40
|
+
# Set each hash value as an instance variable, but don't overwrite code or name values.
|
41
|
+
instance_variable_set(instance_variable, value)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Recursively merges Hash (or any Object that can be converted to a Hash),
|
49
|
+
# Array and attributes that have implemented an :attribute_add method.
|
50
|
+
#
|
51
|
+
def deep_merge(arg1, arg2, key=nil)
|
52
|
+
# Attempt deep merge if the two arguments are
|
53
|
+
# instances of the same class.
|
54
|
+
if arg1.class == arg2.class
|
55
|
+
# Preserve arg1's :code and :name.
|
56
|
+
if (not key.nil?) and [:code, :name].include? key.to_sym
|
57
|
+
return arg1
|
58
|
+
# Recursively call deep merge for Hash objects.
|
59
|
+
elsif arg1.respond_to? :to_hash
|
60
|
+
r = {}
|
61
|
+
return arg1.to_hash.merge(arg2.to_hash) do |key, oldval, newval|
|
62
|
+
r[key] = deep_merge(oldval, newval, key)
|
63
|
+
end
|
64
|
+
# Concatenate Arrays and return uniq elements.
|
65
|
+
elsif arg1.instance_of? Array
|
66
|
+
return arg1.concat(arg2).uniq
|
67
|
+
# If Institutions responds to the :key_add method,
|
68
|
+
# go ahead and use that to add the Objects together.
|
69
|
+
elsif respond_to? "#{key}_add".to_sym
|
70
|
+
return send "#{key}_add".to_sym, arg1, arg2
|
71
|
+
end
|
72
|
+
end
|
73
|
+
# NOTE: Commenting this out since I'm not sure this is desirable functionality.
|
74
|
+
# If the two args aren't the same class, but arg1 is
|
75
|
+
# Array, append arg2 to arg1.
|
76
|
+
# if arg1.instance_of? Array
|
77
|
+
# return arg1.dup<<arg2
|
78
|
+
# end
|
79
|
+
# We tried.
|
80
|
+
# If all else fails just replace arg1 with arg2.
|
81
|
+
return arg2
|
82
|
+
end
|
83
|
+
protected :deep_merge
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Institutions#:no_doc
|
2
|
+
module Parents#:no_doc
|
3
|
+
# Parents attributes
|
4
|
+
attr_reader :parent_code
|
5
|
+
|
6
|
+
#
|
7
|
+
# Merges the given parent into the Institution.
|
8
|
+
# Assumes the parent has a to_hash method
|
9
|
+
#
|
10
|
+
def merge_parent(parent={})
|
11
|
+
# Use the parent as the base and merge in the current institution
|
12
|
+
merge(deep_merge(parent.to_hash, to_hash))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Institutions#:no_doc
|
2
|
+
module Util#:no_doc
|
3
|
+
#
|
4
|
+
# Converts the Institution to a hash with keys representing
|
5
|
+
# each Institutional attribute (as symbols) and their corresponding values.
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# require 'institutions'
|
9
|
+
# institution = Institution.new("my_inst", "My Institution")
|
10
|
+
# data.to_hash # => {:code => "my_inst", :name => "My Institution", :default => false }
|
11
|
+
#
|
12
|
+
def to_hash
|
13
|
+
hash = {}
|
14
|
+
instance_variables.each do |inst_var|
|
15
|
+
hash[hash_keyize(inst_var)] = instance_variable_get(inst_var)
|
16
|
+
end
|
17
|
+
hash
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Dynamically sets attr_readers for elements
|
22
|
+
#
|
23
|
+
def method_missing(method, *args, &block)
|
24
|
+
instance_variable = instance_variablize(method)
|
25
|
+
if instance_variable_defined? instance_variable
|
26
|
+
self.class.send :attr_reader, method.to_sym
|
27
|
+
instance_variable_get instance_variable
|
28
|
+
else
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Tells users that we respond to missing methods
|
35
|
+
# if they are instance variables.
|
36
|
+
#
|
37
|
+
def respond_to_missing?(method, include_private = false)
|
38
|
+
# Short circuit if we have invalid instance variable name,
|
39
|
+
# otherwise we get an exception that we don't need.
|
40
|
+
return super unless valid_instance_variable? method
|
41
|
+
if instance_variable_defined? instance_variablize(method)
|
42
|
+
true
|
43
|
+
else
|
44
|
+
super
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def valid_instance_variable?(id)
|
49
|
+
id.to_sym.id2name.match(/[\@\=\?+]/).nil?
|
50
|
+
end
|
51
|
+
|
52
|
+
# Convert s to an instance variable
|
53
|
+
def instance_variablize(s)
|
54
|
+
"@#{s.to_sym.id2name.downcase}".to_sym
|
55
|
+
end
|
56
|
+
private :instance_variablize
|
57
|
+
|
58
|
+
# Convert s to a hash key
|
59
|
+
def hash_keyize(s)
|
60
|
+
# Remove beginning '@' and convert to symbol for hash keys.
|
61
|
+
s.to_s.sub(/^@/,'').to_sym
|
62
|
+
end
|
63
|
+
private :instance_variablize
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Institutions#:no_doc
|
2
|
+
#
|
3
|
+
# = institution.rb: Institution
|
4
|
+
#
|
5
|
+
# Author:: Scot Dalton
|
6
|
+
#
|
7
|
+
# An Institution represents an administrative unit and its unique configuration data.
|
8
|
+
# Institutions are necessarily abstract and fairly flexible to accomodate the myriad
|
9
|
+
# needs of applications. An institution only requires a code and name to be instantiated.
|
10
|
+
#
|
11
|
+
# == Examples:
|
12
|
+
#
|
13
|
+
# require 'institutions'
|
14
|
+
# hash = { "attribute1" => "My first attribute.", :array_attribute => [1, 2] }
|
15
|
+
# institution = Institutions::Institution.new("my_inst", "My Institution", hash)
|
16
|
+
#
|
17
|
+
# p institution # -> #<Institutions::Institution @code=:my_inst, @name="My Institution", @attribute1="My first attribute.", @array_attribute=[1, 2], @default=false>
|
18
|
+
#
|
19
|
+
class Institution
|
20
|
+
include Core
|
21
|
+
include Auth
|
22
|
+
include IpAddresses
|
23
|
+
include Merge
|
24
|
+
include Mvc
|
25
|
+
include Parents
|
26
|
+
include Services
|
27
|
+
include Util
|
28
|
+
end
|
29
|
+
end
|
data/lib/institutions.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# Order of paths, then order of files dictates overrides.
|
2
|
+
|
3
|
+
module Institutions
|
4
|
+
require 'require_all'
|
5
|
+
require_rel 'institutions'
|
6
|
+
|
7
|
+
# Default paths/files for the module.
|
8
|
+
DEFAULT_LOADPATHS = [
|
9
|
+
(defined?(Rails) and Rails.root) ?
|
10
|
+
"#{Rails.root}/config" : "./config" ]
|
11
|
+
DEFAULT_FILENAMES = ["institutions.yml"]
|
12
|
+
|
13
|
+
def self.loadpaths
|
14
|
+
@loadpaths ||= DEFAULT_LOADPATHS.dup
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.filenames
|
18
|
+
@filenames ||= DEFAULT_FILENAMES.dup
|
19
|
+
end
|
20
|
+
|
21
|
+
# Intended for internal use only.
|
22
|
+
def self.loadfiles
|
23
|
+
loadfiles = []
|
24
|
+
if loadfiles.empty?
|
25
|
+
loadpaths.each do |loadpath|
|
26
|
+
filenames.each do |filename|
|
27
|
+
loadfile = File.join(loadpath, filename)
|
28
|
+
loadfiles<< loadfile if File.exists?(loadfile)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
loadfiles
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.reload
|
36
|
+
@institutions = nil
|
37
|
+
institutions
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns an Array of Institutions
|
41
|
+
def self.defaults
|
42
|
+
return institutions.values.find_all { |institution| institution.default? }
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns an Array of Institutions that contain the given IP.
|
46
|
+
def self.with_ip(ip)
|
47
|
+
return institutions.values.find_all { |institution| institution.includes_ip?(ip) }
|
48
|
+
end
|
49
|
+
|
50
|
+
#
|
51
|
+
# Returns a Hash of Institution instances with the Institution#code as the Hash key.
|
52
|
+
# Load file order can be change by the calling application by using Array methods.
|
53
|
+
# The default load file in Rails apps is
|
54
|
+
# "#{Rails.root}/config/institutions.yml"
|
55
|
+
# and if not Rails
|
56
|
+
# "./config/institutions.yml"
|
57
|
+
# To manipulate load path order and/or add directories to the path
|
58
|
+
# Institutions.loadpaths << File.join("path", "to", "new", "load", "directory")
|
59
|
+
# To manipulate file name order and/or add file names
|
60
|
+
# Institutions.filenames << "newfile.yml"
|
61
|
+
#
|
62
|
+
def self.institutions
|
63
|
+
unless @institutions
|
64
|
+
raise NameError.new("No load path was specified.") if loadpaths.nil?
|
65
|
+
raise NameError.new("No files named #{filenames} exist to load in the configured load paths, #{loadpaths}. ") if filenames.empty?
|
66
|
+
@institutions = {}
|
67
|
+
loadfiles.each do |loadfile|
|
68
|
+
# Loop through institutions in the yaml
|
69
|
+
YAML.load_file(loadfile).each_pair do |code, elements|
|
70
|
+
code = code.to_sym
|
71
|
+
# Merge the new elements or add a new Institution
|
72
|
+
@institutions.has_key?(code) ?
|
73
|
+
@institutions[code].merge(elements) :
|
74
|
+
@institutions[code] =
|
75
|
+
Institution.new(code, elements["name"] ? elements["name"] : code, elements)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
# Handle inheritance for institutions
|
79
|
+
merge_parents
|
80
|
+
end
|
81
|
+
@institutions
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.empty?
|
85
|
+
institutions.empty?
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.institutions?
|
89
|
+
(not empty?)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Handle inheritance for institutions
|
93
|
+
def self.merge_parents
|
94
|
+
@institutions.each do |key, institution|
|
95
|
+
parent_code = institution.parent_code
|
96
|
+
institution.merge_parent(@institutions[parent_code]) if parent_code
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# Configure what service plugins are used by Umlaut. This skeleton file
|
2
|
+
# has been generated into your app to help you get started.
|
3
|
+
#
|
4
|
+
# If a service has "disabled:true", it's currently turned off.
|
5
|
+
#
|
6
|
+
# Some services require local api key or connection details as config.
|
7
|
+
# Most services take other options for custom configuration too, not
|
8
|
+
# all options are neccesarily listed as examples here, see source
|
9
|
+
# or source-generated docs for more info.
|
10
|
+
default:
|
11
|
+
name: NYU Libraries
|
12
|
+
auth:
|
13
|
+
link_code: NYU
|
14
|
+
views:
|
15
|
+
dir: nyu
|
16
|
+
sfx_base_url: http://sfx.library.nyu.edu/sfxlcl41?
|
17
|
+
NYU:
|
18
|
+
parent_institution: default
|
19
|
+
ip_addresses:
|
20
|
+
- 128.122.0.0-128.122.149.239
|
21
|
+
- 172.26.*.*
|
22
|
+
- 172.27.*.*
|
23
|
+
- 172.22.88.*
|
24
|
+
- 216.165.*.*
|
25
|
+
- 128.238.*.*
|
26
|
+
NYUAD:
|
27
|
+
name: NYU Abu Dhabi Library
|
28
|
+
parent_institution: NYU
|
29
|
+
views:
|
30
|
+
dir: nyuad
|
31
|
+
sfx_base_url: http://sfx.library.nyu.edu/sfxlcl41?
|
32
|
+
ip_addresses:
|
33
|
+
- 192.168.224.0/23
|
34
|
+
- 192.168.226.0/24
|
35
|
+
- 192.168.227.0/25
|
36
|
+
- 192.168.227.128/26
|
37
|
+
- 172.25.79.0/26
|
38
|
+
- 172.26.240.0/22
|
39
|
+
- 172.30.60.0/24
|
40
|
+
- 172.27.240.0/22
|
41
|
+
- 172.29.252.0/24
|
42
|
+
- 172.29.120.0/23
|
43
|
+
- 192.168.192.0/23
|
44
|
+
- 192.168.195.0/25
|
45
|
+
- 172.25.76.0/23
|
46
|
+
- 172.26.232.0/22
|
47
|
+
- 172.30.58.0/24
|
48
|
+
- 172.27.232.0/22
|
49
|
+
- 172.29.250.0/24
|
50
|
+
- 172.29.116.0/23
|
51
|
+
- 192.168.194.0/24
|
52
|
+
- 172.25.78.0/26
|
53
|
+
- 172.26.236.0/22
|
54
|
+
- 172.30.59.0/24
|
55
|
+
- 172.27.236.0/22
|
56
|
+
- 172.29.251.0/24
|
57
|
+
- 172.29.118.0/23
|
58
|
+
CU:
|
59
|
+
name: The Cooper Union Library
|
60
|
+
login:
|
61
|
+
link_code: CU
|
62
|
+
views:
|
63
|
+
dir: cu
|
64
|
+
sfx_base_url: http://sfx.library.nyu.edu/sfxcooper?
|
65
|
+
controllers:
|
66
|
+
searcher: SearchMethods::Sfx4Solr::CU
|
67
|
+
ip_addresses:
|
68
|
+
- 199.98.16.0-199.98.31.255
|
69
|
+
NS:
|
70
|
+
name: New School Libraries
|
71
|
+
login:
|
72
|
+
link_code: NS
|
73
|
+
views:
|
74
|
+
dir: ns
|
75
|
+
sfx_base_url: http://sfx4.library.newschool.edu/ns?
|
76
|
+
controllers:
|
77
|
+
searcher: SearchMethods::Sfx4Solr::NS
|
78
|
+
ip_addresses:
|
79
|
+
- 149.31.0.0-149.31.255.255
|
80
|
+
- 69.64.210.46
|
81
|
+
- 69.64.210.50
|
82
|
+
- 69.64.210.42
|
83
|
+
- 69.193.198.126
|
84
|
+
NYSID:
|
85
|
+
login:
|
86
|
+
link_code: NYSID
|
87
|
+
name: New York School of Interior Design Library
|
88
|
+
ip_addresses:
|
89
|
+
- 128.122.0.1
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CoreTest < Test::Unit::TestCase
|
4
|
+
def test_new
|
5
|
+
hash = { "first" => "My first attribute.", :array_attribute => [1, 2] }
|
6
|
+
assert_nothing_raised {
|
7
|
+
institution = Institutions::Institution.new("my_inst", "My Institution", hash)
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_new_no_hash
|
12
|
+
assert_nothing_raised {
|
13
|
+
institution = Institutions::Institution.new("my_inst", "My Institution")
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_new_nil_hash
|
18
|
+
assert_nothing_raised {
|
19
|
+
institution = Institutions::Institution.new("my_inst", "My Institution", nil)
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_new_nil_code
|
24
|
+
assert_raise(ArgumentError) {
|
25
|
+
institution = Institutions::Institution.new(nil, "My Institution")
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_new_nil_name
|
30
|
+
assert_raise(ArgumentError) {
|
31
|
+
institution = Institutions::Institution.new("my_inst", nil)
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class IpAddressesTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@hash = { "ip_addresses" => ["134.122.0.0-134.122.149.239",
|
6
|
+
"134.22.88.*", "134.165.*.*", "134.238.*.*", "134.168.224.0/23"] }
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_ip_addresses
|
10
|
+
assert_nothing_raised {
|
11
|
+
institution = Institutions::Institution.new("my_inst", "My Institution", @hash)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_include_ip?
|
16
|
+
institution = Institutions::Institution.new("my_inst", "My Institution", @hash)
|
17
|
+
assert institution.includes_ip? "134.165.4.1"
|
18
|
+
assert institution.includes_ip? "134.168.224.255"
|
19
|
+
assert institution.includes_ip? "134.122.21.12"
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class MergeTest < Test::Unit::TestCase
|
4
|
+
def test_merge
|
5
|
+
hash1 = { "string_attribute" => "first string", :nested_hash_attribute => {:h1 => {:h1_2 => "first12"},
|
6
|
+
:h2 => {:h2_1 => "first21", :h2_2 => "first22"}}, :array_attribute => [1, 2, 4] }
|
7
|
+
institution1 = Institutions::Institution.new("first_inst", "First Institution", hash1)
|
8
|
+
assert_equal(:first_inst, institution1.code)
|
9
|
+
assert_equal("First Institution", institution1.name)
|
10
|
+
assert_equal({:h1 => {:h1_2 => "first12"}, :h2 => {:h2_1 => "first21", :h2_2 => "first22"}},
|
11
|
+
institution1.nested_hash_attribute)
|
12
|
+
assert_equal([1,2,4], institution1.array_attribute)
|
13
|
+
assert_equal("first string", institution1.string_attribute)
|
14
|
+
assert_equal(false, institution1.default)
|
15
|
+
hash2 = { "string_attribute" => "second string", :nested_hash_attribute => {:h1 => {:h1_2 => "second12"},
|
16
|
+
:h2 => {:h2_2 => "second22"}}, :array_attribute => [1, 2, 3], :default => true }
|
17
|
+
institution2 = Institutions::Institution.new("second_inst", "Second Institution", hash2)
|
18
|
+
assert_equal(:second_inst, institution2.code)
|
19
|
+
assert_equal("Second Institution", institution2.name)
|
20
|
+
assert_equal({:h1 => {:h1_2 => "second12"}, :h2 => {:h2_2 => "second22"}},
|
21
|
+
institution2.nested_hash_attribute)
|
22
|
+
assert_equal([1,2,3], institution2.array_attribute)
|
23
|
+
assert_equal("second string", institution2.string_attribute)
|
24
|
+
assert_equal(true, institution2.default)
|
25
|
+
institution1.merge(institution2)
|
26
|
+
assert_equal(:first_inst, institution1.code)
|
27
|
+
assert_equal("First Institution", institution1.name)
|
28
|
+
assert_equal({:h1 => {:h1_2 => "second12"}, :h2 => {:h2_1 => "first21", :h2_2 => "second22"}},
|
29
|
+
institution1.nested_hash_attribute)
|
30
|
+
assert_equal([1,2,3,4], institution1.array_attribute.sort)
|
31
|
+
assert_equal("second string", institution1.string_attribute)
|
32
|
+
assert_equal(true, institution1.default)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ParentsTest < Test::Unit::TestCase
|
4
|
+
def test_parent
|
5
|
+
hash = { "first" => "My first attribute.", :array_attribute => [1, 2] }
|
6
|
+
institution = Institutions::Institution.new("my_inst", "My Institution", hash)
|
7
|
+
parent_hash = { "first" => "parent first", :array_attribute => [1, 2, 3], :inherited_trait => "inherited" }
|
8
|
+
parent_institution = Institutions::Institution.new("parent", "My Parent Institution", parent_hash)
|
9
|
+
institution.merge_parent(parent_institution)
|
10
|
+
assert_equal("My first attribute.", institution.first)
|
11
|
+
assert_equal("inherited", institution.inherited_trait)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class UtilTest < Test::Unit::TestCase
|
4
|
+
def test_missing_method
|
5
|
+
hash = { "an_attr" => "My first attribute.", :some_numbers => [1, 2] }
|
6
|
+
institution = Institutions::Institution.new("my_inst", "My Institution", hash)
|
7
|
+
assert_nothing_raised {
|
8
|
+
institution.an_attr
|
9
|
+
}
|
10
|
+
assert_equal("My first attribute.", institution.an_attr)
|
11
|
+
assert_equal([1, 2], institution.some_numbers)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class InstitutionsTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
# Undo any instance variable settings.
|
6
|
+
Institutions.send(:instance_variable_set, :@loadpaths, nil)
|
7
|
+
assert_equal(Institutions::DEFAULT_LOADPATHS, Institutions.loadpaths)
|
8
|
+
Institutions.send(:instance_variable_set, :@filenames, nil)
|
9
|
+
assert_equal(Institutions::DEFAULT_FILENAMES, Institutions.filenames)
|
10
|
+
Institutions.send(:instance_variable_set, :@institutions, nil)
|
11
|
+
assert_nil(Institutions.send(:instance_variable_get, :@institutions))
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_module
|
15
|
+
assert_kind_of Module, Institutions
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_path
|
19
|
+
assert_equal Institutions::DEFAULT_LOADPATHS, Institutions.loadpaths
|
20
|
+
Institutions.loadpaths << File.join("../config")
|
21
|
+
assert_equal Institutions::DEFAULT_LOADPATHS.concat(["../config"]), Institutions.loadpaths
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_institutions
|
25
|
+
Institutions.loadpaths << File.join("test", "config")
|
26
|
+
institutions = Institutions.institutions
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_institutions_overwrite
|
30
|
+
Institutions.loadpaths << File.join("test", "config")
|
31
|
+
Institutions.filenames << "overwrite.yml"
|
32
|
+
institutions = Institutions.institutions
|
33
|
+
assert_equal "ns", institutions[:NS].views["dir"]
|
34
|
+
assert_equal "ns tag", institutions[:NS].views["tag"]
|
35
|
+
end
|
36
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: institutions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Scot Dalton
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: require_all
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.2.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.2.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: ipaddr_range_set
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.9.1
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.9.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: git
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.2.5
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.2.5
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rdoc
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Abstract mechanism for setting up Institutions with arbitrary data elements.
|
95
|
+
email:
|
96
|
+
- scotdalton@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .travis.yml
|
103
|
+
- Gemfile
|
104
|
+
- MIT-LICENSE
|
105
|
+
- README.rdoc
|
106
|
+
- Rakefile
|
107
|
+
- institutions.gemspec
|
108
|
+
- lib/institutions.rb
|
109
|
+
- lib/institutions/institution.rb
|
110
|
+
- lib/institutions/institution/auth.rb
|
111
|
+
- lib/institutions/institution/core.rb
|
112
|
+
- lib/institutions/institution/ip_addresses.rb
|
113
|
+
- lib/institutions/institution/merge.rb
|
114
|
+
- lib/institutions/institution/mvc.rb
|
115
|
+
- lib/institutions/institution/parents.rb
|
116
|
+
- lib/institutions/institution/services.rb
|
117
|
+
- lib/institutions/institution/util.rb
|
118
|
+
- lib/institutions/version.rb
|
119
|
+
- test/config/institutions.yml
|
120
|
+
- test/config/overwrite.yml
|
121
|
+
- test/institution/auth_test.rb
|
122
|
+
- test/institution/core_test.rb
|
123
|
+
- test/institution/ip_addresses_test.rb
|
124
|
+
- test/institution/merge_test.rb
|
125
|
+
- test/institution/mvc_test.rb
|
126
|
+
- test/institution/parents_test.rb
|
127
|
+
- test/institution/services_test.rb
|
128
|
+
- test/institution/util_test.rb
|
129
|
+
- test/institution_test.rb
|
130
|
+
- test/institutions_test.rb
|
131
|
+
- test/test_helper.rb
|
132
|
+
homepage: https://github.com/scotdalton/institutions
|
133
|
+
licenses: []
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ! '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
segments:
|
145
|
+
- 0
|
146
|
+
hash: -78613404009586632
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
segments:
|
154
|
+
- 0
|
155
|
+
hash: -78613404009586632
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 1.8.24
|
159
|
+
signing_key:
|
160
|
+
specification_version: 3
|
161
|
+
summary: Abstract mechanism for setting up Institutions (whatever that means).
|
162
|
+
test_files:
|
163
|
+
- test/config/institutions.yml
|
164
|
+
- test/config/overwrite.yml
|
165
|
+
- test/institution/auth_test.rb
|
166
|
+
- test/institution/core_test.rb
|
167
|
+
- test/institution/ip_addresses_test.rb
|
168
|
+
- test/institution/merge_test.rb
|
169
|
+
- test/institution/mvc_test.rb
|
170
|
+
- test/institution/parents_test.rb
|
171
|
+
- test/institution/services_test.rb
|
172
|
+
- test/institution/util_test.rb
|
173
|
+
- test/institution_test.rb
|
174
|
+
- test/institutions_test.rb
|
175
|
+
- test/test_helper.rb
|