Sha256: 2130e383b362d32e1bbfe376f0970dae29853ca653b3de8d97aded10dda00c7f

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

module Institutions#:no_doc
  module Core#:no_doc
    # Required attributes
    attr_reader :code, :name
    alias :display_name :name

    # Optional core attributes
    attr_reader :default
    alias :default? :default

    #
    # Creates a new Institution object from the given code, name and hash.
    #
    # The optional +hash+, if given, will generate additional attributes and values.
    # 
    # 
    # For example:
    #
    #   require 'institutions'
    #   hash = { "attribute1" => "My first attribute.", :array_attribute => [1, 2] }
    #   institution = Institutions::Institution.new("my_inst", "My Institution", hash)
    #
    #   p institution        # -> <Institutions::Institution @code=:my_inst @name="My Institution" @attribute1=My first attribute." @array_attribute=[1, 2] @default=false>
    #
    def initialize(code, name, h={})
      # Set the required attributes
      set_required_attributes code, name
      # Merge in the optional hash attribute
      merge h unless h.nil?
      # If the institution is named default, take that as an
      # indication that it's the default institution
      @default = true if (name.eql? "default" or name.eql? "DEFAULT") 
      # If default was never set, explicitly set default as false.
      @default = false if default.nil?
    end

    # 
    # Sets the required attributes.
    # Raises an ArgumentError specifying the missing arguments if they are nil.
    # 
    def set_required_attributes(code, name)
      missing_arguments = []
      missing_arguments << :code if code.nil?
      missing_arguments << :name if name.nil?
      raise ArgumentError.new("Cannot create the Institution based on the given arguments (:code => #{code.inspect}, :name => #{name.inspect}).\n"+
        "The following arguments cannot be nil: #{missing_arguments.inspect}") unless missing_arguments.empty?
      # Set the instance variables
      @code, @name = code.to_sym, name
    end
    private :set_required_attributes
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
institutions-0.0.3 lib/institutions/institution/core.rb
institutions-0.0.2 lib/institutions/institution/core.rb
institutions-0.0.1 lib/institutions/institution/core.rb