class Ferret::Browser::Controller

Constants

APP_DIR
STATIC_DIR

Public Class Methods

new(reader, path, vars) click to toggle source
# File lib/ferret/browser.rb, line 89
def initialize(reader, path, vars)
  @reader = reader
  @path = path
  vars.each_pair {|key, val| instance_eval("@#{key} = val")}
  @controller_path = pathify(self.class.to_s.gsub(%r.*:/, ''))
end

Public Instance Methods

method_missing(meth_id, *args) click to toggle source
# File lib/ferret/browser.rb, line 96
def method_missing(meth_id, *args)
  render(:action => meth_id)
end

Protected Instance Methods

load_page(page) click to toggle source
# File lib/ferret/browser.rb, line 102
def load_page(page)
  File.read(File.join(APP_DIR, page))
end
paginate(idx, max, url, &b) click to toggle source

takes an optional block to set optional attributes in the links

# File lib/ferret/browser.rb, line 126
def paginate(idx, max, url, &b)
  return '' if max == 0
  url = url.gsub(%r{^/?(.*?)/?$}, '\1')
  b ||= lambda{}
  link = lambda {|*args|
    i, title, text = args
    "<a href=\"/#{url}/#{i}#{'?' + @query_string if @query_string}\" " +
     "#{'onclick="return false;"' if (i == idx)} " +
     "class=\"#{'disabled ' if (i == idx)}#{b.call(i)}\" " +
     "title=\"#{title||"Go to page #{i}"}\">#{text||i}</a>"
  }
  res = '<div class="nav">'
  if (idx > 0)
    res << link.call(idx - 1, "Go to previous page", "&#171; Previous")
  else
    res << "<a href=\"/#{url}/0\" onclick=\"return false;\" " +
            "class=\"disabled\" title=\"Disabled\">&#171; Previous</a>"
  end
  if idx < 10
    idx.times {|i| res << link.call(i)}
  else
    (0..2).each {|i| res << link.call(i)}
    res << '&nbsp;&#8230;&nbsp;'
    ((idx-4)...idx).each {|i| res << link.call(i)}
  end
  res << link.call(idx, 'Current Page')
  if idx > (max - 10)
    ((idx+1)...max).each {|i| res << link.call(i)}
  else
    ((idx+1)..(idx+4)).each {|i| res << link.call(i)}
    res << '&nbsp;&#8230;&nbsp;'
    ((max-3)...max).each {|i| res << link.call(i)}
  end
  if (idx < (max - 1))
    res << link.call(idx + 1, "Go to next page", "Next &#187;")
  else
    res << "<a href=\"/#{url}/#{max-1}\" onclick=\"return false;\" " +
            "class=\"disabled\" title=\"Disabled\"}\">Next &#187;</a>"
  end
  res << '</div>'
end
render(options = {}) click to toggle source
# File lib/ferret/browser.rb, line 106
def render(options = {})
  options = {
    :controller => @controller_path,
    :action => :index,
    :status => 200,
    :content_type => 'text/html',
    :env => nil,
    :layout => 'views/layout.rhtml',
  }.update(options)

  path = "views/#{options[:controller]}/#{options[:action]}.rhtml"
  content = ERB.new(load_page(path)).result(lambda{})
  if options[:layout]
    content = ERB.new(load_page(options[:layout])).result(lambda{})
  end

  return options[:status], options[:content_type], content
end