module RHNH module Magellan class Navigator attr_reader :pages attr_reader :links @@_navigator = nil def initialize @state = {} @links = {} @pages = {} end def self.instance if !@@_navigator @@_navigator = Navigator.new end @@_navigator end def self.draw @@_navigator = Navigator.new yield instance end def get_page_link state, link_name link = link_name ? link_name : @links[link_name] if link.respond_to?('call') page_obj, params = link.call(@pages, state) else page_obj, params = @pages[link], nil end [page_obj, params] end def get_url page, link_name = nil, state = {} return(@pages[page] ? @pages[page].url : nil) if !link_name state = {} if !state page_obj, params = get_page_link(state, @pages[page][link_name]) if @pages[page] page_obj, params = get_page_link(state, @links[link_name]) if !page_obj if page_obj if params if page_obj.url.has_key?(:params) ret = page_obj.url.merge({:params => page_obj.url[:params].merge(params)}) else ret = page_obj.url.merge({:params => params}) end else ret = page_obj.url end end ret end def add_page page_name, url = nil new_page = NavPage.new new_page.url = url if url yield new_page if block_given? @pages[page_name.to_sym] = new_page end def add_link link_name, target_page @links[link_name.to_sym] = target_page end end class NavPage attr_accessor :url def initialize @links = {} @url = {} end def add_link link_name, target_page @links[link_name.to_sym] = target_page end def [] key @links[key.to_sym] end end end end