You are here:   ArielOrtiz.com > Software Design and Architecture > Lab 9: Meta-programming Pattern

Lab 9: Meta-programming Pattern

Objectives

During this lab session:

This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.

Activity Description

The lab activities can be developed individually or in pairs.

The lab report must be developed individually.

  1. Create a folder called metaprogramming. Inside this folder, create two files called: attr_init.rb and test_attr_init.rb.

    All Ruby source files must start with a comment containing the lab's title, date, and the authors' personal information. For example:

    # Lab 9: Meta-programming Pattern
    # Date: 26-Oct-2011
    # Authors:
    # 456654  Anthony Stark 
    # 1160611 Thursday Rubinstein
  2. In the attr_init.rb source file, open the Object class and define a new class method called attr_init. This method should work as a class annotation that defines the initialize method in the enclosing class, and also calls the attr_accessor as shown as follows.

    For example, given the class definition:

    class C
      attr_init :w, :x, :y, :z
    end
    

    It should be transformed into something equivalent to this:

    class C
      def initialize(w, x, y, z)
        @w = w
        @x = x
        @y = y
        @z = z
      end
      attr_accessor :w, :x, :y, :z
    end
    

    Use the meta-programming pattern, as explained in page 309 of [OLSEN], to do this.

  3. The following unit tests verify the correct definition of the attr_init class annotation. Place the test class in the test_attr_init.rb source file.

    require 'test/unit'
    require 'attr_init'
    
    class AttrInitTest < Test::Unit::TestCase
      
      class Alpha
        attr_init :beta, :gamma, :delta
      end
      
      class BiologicalClassification
        attr_init :kingdom, :phylum, :klass, :order, :family, :genus, :species
      end    
    
      def test_alpha    
        alpha = Alpha.new('one', 'two', 'three')
        assert_equal 3, alpha.method(:initialize).arity
        assert_respond_to alpha, :beta
        assert_respond_to alpha, :beta=
        assert_respond_to alpha, :gamma
        assert_respond_to alpha, :gamma=
        assert_respond_to alpha, :delta
        assert_respond_to alpha, :delta=
        assert_equal 'one', alpha.beta
        assert_equal 'two', alpha.gamma
        assert_equal 'three', alpha.delta
        assert_equal 1, (alpha.beta = 1)
        assert_equal 2, (alpha.gamma = 2)
        assert_equal 3, (alpha.delta = 3)
        assert_equal 1, alpha.beta
        assert_equal 2, alpha.gamma
        assert_equal 3, alpha.delta
      end
      
      def test_biological_classification
        platapus = BiologicalClassification.new(
          'Animalia',
          'Chordata',
          'Mammalia',
          'Monotremata',
          'Ornithorhynchidae',
          'Ornithorhynchus',
          'Ornithorhynchus Anatinus')
        assert_equal 7, platapus.method(:initialize).arity
        assert_respond_to platapus, :kingdom
        assert_respond_to platapus, :phylum
        assert_respond_to platapus, :klass
        assert_respond_to platapus, :order
        assert_respond_to platapus, :family
        assert_respond_to platapus, :genus
        assert_respond_to platapus, :species
        assert_respond_to platapus, :kingdom=
        assert_respond_to platapus, :phylum=
        assert_respond_to platapus, :klass=
        assert_respond_to platapus, :order=
        assert_respond_to platapus, :family=
        assert_respond_to platapus, :genus=
        assert_respond_to platapus, :species=
        assert_equal 'Animalia', platapus.kingdom
        assert_equal 'Chordata', platapus.phylum
        assert_equal 'Mammalia', platapus.klass
        assert_equal 'Monotremata', platapus.order
        assert_equal 'Ornithorhynchidae', platapus.family
        assert_equal 'Ornithorhynchus', platapus.genus
        assert_equal 'Ornithorhynchus Anatinus', platapus.species
        assert_equal 'Amoebozoa', (platapus.kingdom = 'Amoebozoa')
        assert_equal 'Tubulinea', (platapus.phylum = 'Tubulinea')
        assert_equal 'Tubulinida', (platapus.order = 'Tubulinida')
        assert_equal 'Amoebidae', (platapus.family = 'Amoebidae')
        assert_equal 'Amoeba', (platapus.genus = 'Amoeba')
        assert_equal 'Amoeba Proteus', (platapus.species = 'Amoeba Proteus')
      end
      
      def test_errors
        assert_raise(ArgumentError) do
          alpha = Alpha.new()
        end    
        assert_raise(ArgumentError) do
          alpha = Alpha.new(1, 2, 3, 4)
        end    
        assert_raise(ArgumentError) do
          alpha = BiologicalClassification.new()
        end
        assert_raise(ArgumentError) do
          alpha = BiologicalClassification.new(
            'Plantae',
            'Magnoliophyta',
            'Magnoliopsida',
            'Rosales',
            'Rosaceae')
        end
      end
      
    end

Deliverables

To hand in your individual lab work, follow these instructions.

Due date is Tuesday, November 1.

Evaluation

This activity will be evaluated using the following criteria:

50% Implementation of functional requirements.
50% Lab report.
DA The program and/or report was plagiarized.
© 1996-2011 by Ariel Ortiz (ariel.ortiz@itesm.mx)
Made with Django | Licensed under Creative Commons | Valid HTML5 | Valid CSS