class MediTAF::UI::Application

Manages all of the application's pages in the application's root directory

Attributes

app_root[R]
current_page[R]
pages[R]

Public Class Methods

new(app_root) click to toggle source

@param app_root [String] path to the application's page models

# File lib/MediTAF/ui/application.rb, line 12
def initialize(app_root)
  @pages = {}
  @app_root = app_root
end

Public Instance Methods

each(&block) click to toggle source

iterates over the loaded pages collection @param block [Proc]

# File lib/MediTAF/ui/application.rb, line 33
def each(&block)
  @pages.each(&block) if @pages
end
method_missing(page, *args, &block) click to toggle source

references the page as a method name @param page [Symbol] the page name as a symbol @return [Object] the current page

# File lib/MediTAF/ui/application.rb, line 20
def method_missing(page, *args, &block)
  @current_page =
      if @pages.include?(page)
        @pages[page]
      elsif in_namespace?(page)
        new_page(@app_root + '/' + page.to_s)
      else
        load_page(page)
      end
end

Private Instance Methods

in_namespace?(page) click to toggle source
# File lib/MediTAF/ui/application.rb, line 64
def in_namespace?(page)
  ns = /^.+\/[^\/]+\/([^\/]+)$/.match(@app_root)[1].camelize.constantize
  ns.constants.detect { |c| ns.const_get(c).is_a?(Class) && c == page.to_s.camelize.to_sym }
rescue => e
  nil
end
load_page(page) click to toggle source

@param page [Symbol] the page name as a symbol @return [MediTAF::UI:BasePage] the newly accessed page @raise [PageLoadError] when manager cannot fully load the requested page

# File lib/MediTAF/ui/application.rb, line 47
def load_page(page)
  raise "File #{@app_root}/#{page.to_s}.rb not found" unless Dir["#{@app_root}/#{page.to_s}.rb"].detect do |f|
    !File.directory?(f) && File.basename(f, ".*").downcase.to_sym == page
  end
  class_path = @app_root + '/' + page.to_s
  require class_path
  new_page(class_path)
rescue => e
  raise PageLoadError, "Couldn't load page #{page.to_s}. Inner Exception: #{e.to_s}"
end
new_page(class_path) click to toggle source
# File lib/MediTAF/ui/application.rb, line 58
def new_page(class_path)
  @pages[class_path.split('/').last.to_sym] = /^.+\/([^\/]+\/[^\/]+)$/.match(class_path)[1].camelize.constantize.new
rescue => e
  raise PageLoadError, "Couldn't instantiate page #{page.to_s}. Inner Exception #{e.to_s}"
end