Sha256: a188658d7c83d0f7b9fbbe36d7cebc348864d5bc78aa7328b6322d92863d91be

Contents?: true

Size: 1.93 KB

Versions: 8

Compression:

Stored size: 1.93 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.downcase.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

8 entries across 8 versions & 1 rubygems

Version Path
institutions-0.1.3 lib/institutions/institution/core.rb
institutions-0.1.2 lib/institutions/institution/core.rb
institutions-0.1.1 lib/institutions/institution/core.rb
institutions-0.1.0 lib/institutions/institution/core.rb
institutions-0.0.7 lib/institutions/institution/core.rb
institutions-0.0.6 lib/institutions/institution/core.rb
institutions-0.0.5 lib/institutions/institution/core.rb
institutions-0.0.4 lib/institutions/institution/core.rb