Sha256: a9d74d606bbb5870fef22d778160df0ab95d6618bf717388f16108d90cf96b6e

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Gametel
  #
  # Module to facilitate create new gametel screen objects in
  # definitions.
  #
  module Navigation

    class << self
      attr_reader :navigation_routes

      def routes=(routes)
        @navigation_routes = routes
      end
    end

    #
    # create a new screen given a class name
    #
    def on(cls, &block)
      @current_screen = cls.new
      block.call @current_screen if block
      @current_screen
    end

    #
    # Navigate to a specific screen following a predefined path.
    #
    def navigate_to(screen_cls, how = {:using => :default}, &block)
      path = path_for how
      from = @current_screen ? @current_screen.class : path[0][0]
      from_index = find_index_for(path, from)
      to_index = find_index_for(path, screen_cls) - 1
      navigate_through_pages(path[from_index..to_index])
      on(screen_cls, &block)
    end

    private

    def path_for(how)
      path = Gametel::Navigation.navigation_routes[how[:using]]
      fail("Gametel::Navigation route :#{how[:using].to_s} not found") unless path
      path
    end

    def find_index_for(path, item)
      path.each_with_index { |each, index| return index if each[0] == item}
    end

    def navigate_through_pages(pages)
      pages.each do |cls, method|
        page = on(cls)
        fail("Navigation method not specified on #{cls}.  Please call the ") unless page.respond_to? method
        page.send method
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gametel-0.5.4 lib/gametel/navigation.rb
gametel-0.5.3 lib/gametel/navigation.rb
gametel-0.5.2 lib/gametel/navigation.rb