The Rubygame Book Part 3

From Kibabase

Jump to: navigation, search
Home Beginning Back Next End
Back to Table of Content Jump to Beginning Back Next Jump to End(Part 7)

It is time to put our theory into real world usage as we write the dependencity system.

Contents

Initialization

Let get started by writing the initialization of Dependencity.

 file: lib/dependencity/dependencity.rb
  class Dependencity
    attr_reader :directories
    def initialize
      @directories = Array.new() #@directories contain the directories we are loading. 
                                 #@directories will be useful later on as we write our own event system.
      @ignore_list = Array.new() #@ignore_list is self explanatory. It list files that should not be loaded.
      @patterns = Array.new()    #@patterns contain regular expression for figuring out prorities, thus which files to load first.
    end
  end

Adding Stuff

Ignore

The ignore method is next to write. This method will add files for exclusion from loading.

 file: lib/dependencity/dependencity.rb in class Dependencity
    def ignore name
      @ignore_list << name
    end

Directories

Of course, we will need a method to add directories we want to load.

 file: lib/dependencity/dependencity.rb in class Dependencity
    def add_dir name
      @directories << dir
    end

Rules

We will need to add rules so that the program can understand which are the most improtant to load first. The matching code will go through each pattern starting from the first element of the array, so remember to put rules that you want in order. Also, this is a good time to learn regular expressions especially if you want to do complex rules.

 file: lib/dependencity/dependencity.rb in class Dependencity
    def add_rule name
      @patterns << name
    end

Processing Directories

Alphabetical Sort Operation

The first step in the directory processing part is to get a list of ruby files and then sorting them alphabetically.

 file: lib/dependencity/dependencity.rb in class Dependencity
  def get_files directory
    files = Dir.glob( directory + "/" + File.join("**","*.rb")).sort()
    return files
  end

Prority Sort Operation

For now, we only need one operation to modify a filename's prority. That is, shifting an element of the @files array to the front. In that way, the loader will process the desired file first.

 file: lib/dependencity/dependencity.rb in class Dependencity
    def move_to_front value
      filename = @files[value]
      files.delete_at(value)    #Remove the filename in question
      files.unshift(filename) #Added the filename back in again, this time in the front.
    end

What Rules to Process First?

Rules should be processed in the order of the last and then the next last rule so that the lower priority files will not overtake higher prority files.

 file: lib/dependencity/dependencity.rb in class Dependencity
    def process_rules files
      go_down = @patterns.length()
      step = @patterns.length() - 1
      go_down.times do |rule|
        counter = 0
        files.each do |file|
          if file.match(@patterns[step])
             files = move_to_front(files,counter)
          end
          counter += 1
        end
        step -= 1
      end
      return files
    end

Mass Require

When you're done with sorting, it is time to load these files.

 file: lib/dependencity/dependencity.rb in class Dependencity
    def require_all files
      files.each do |file|
        action = true
        @ignore_list.each do |ignore|
          if file == ignore
            action = false
          end
        end
        if action == true
          require file
          puts"Loaded file: #{file}"
        end
      end
    end

Tying Everything Up

Now that the neccesary ingradient are gathered, it is time to write a method that will make use of everything else to do require on a directory scale.

 file: lib/dependencity/dependencity.rb in class Dependencity
    def load_directory directory
      files = get_files(directory)
      files = process_rules(files)
      require_all(files)
    end

Execute method

For processing directories, we will do this one at a time in the assumption that the developers load the neccessary directories or directories that will be depended on by other directories. Therefore, it is prudent to process a directory by getting all the filenames, sort them alpahbetically, and reorder them to priority, load them, before moving on to the next directory in the list.

 file: lib/dependencity/dependencity.rb in class Dependencity
  def process_directories
    @directories.each do |directory|
      load_directory(directory)
    end
  end

Conclusion

Now that the dependencity project is finished, it is time to integrate dependencity into our application and test drive it. Proceed to Part 4.

Personal tools