Sha256: aa00510589108562604e1f4692f518073969896507afe5d69c1d65a54191af23

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

class Institution < Struct.new(:display_name, :name, :default,
  :layouts, :ip_addresses, :parent_institution, :views, :login)
  require 'ipaddr'

  # Better initializer than Struct gives us, take a hash instead
  # of an ordered array. :services=>[] is an array of service ids,
  # not actual Services!
  def initialize(h={})
    members.each {|m| self.send( ("#{m}=").to_sym , (h.delete("#{m}".to_sym) || h.delete("#{m}"))) }
    # If the institution is named default, take that as an
    # indication that it's the default institution
    self.default= true if name == "default" or name == "DEFAULT"
    self.default= false unless default
    # Log the fact that there are left overs in the hash
    # Rails.logger.warn("The following institution settings were ignored: #{h.inspect}.") unless h.empty?
  end

  # Instantiates a new copy of all services included in this institution,
  # returns an array. 
  def instantiate_services!
    services.collect {|s|  }
  end
  
  # Check the list of IP addresses for the given IP
  def includes_ip?(prospective_ip_address)
    return false if ip_addresses.nil?
    ip_prospect = IPAddr.new(prospective_ip_address)
    ip_addresses.each do |ip_address|
      ip_range = (ip_address.match(/[\-\*]/)) ? 
        (ip_address.match(/\-/)) ? 
          (IPAddr.new(ip_address.split("-")[0])..IPAddr.new(ip_address.split("-")[1])) :
            (IPAddr.new(ip_address.gsub(/\*/, "0"))..IPAddr.new(ip_address.gsub(/\*/, "255"))) :
              IPAddr.new(ip_address).to_range
      return true if ip_range === ip_prospect unless ip_range.nil?
    end
    return false;
  end
  
  def to_h
    h = {}
    members.each {|m| h[m.to_sym] = self.send(m)}
    h
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
authpds-0.1.1 lib/authpds/institution.rb
authpds-0.1.0 lib/authpds/institution.rb
authpds-0.0.23 lib/authpds/institution.rb