class Dugway::Controller

Public Class Methods

routes() click to toggle source
# File lib/dugway/controller.rb, line 8
def routes
  @routes ||= Rack::Mount::RouteSet.new
end

Private Class Methods

any(path, &block) click to toggle source
# File lib/dugway/controller.rb, line 41
def any(path, &block)
  get(path, &block)
  post(path, &block)
end
cart() click to toggle source
# File lib/dugway/controller.rb, line 62
def cart
  Dugway.cart
end
error(msg) click to toggle source
# File lib/dugway/controller.rb, line 79
def error(msg)
  request.session[:errors] ||= []
  request.session[:errors] << msg
end
get(path, &block) click to toggle source
# File lib/dugway/controller.rb, line 33
def get(path, &block)
  route('GET', path, &block)
end
page() click to toggle source
# File lib/dugway/controller.rb, line 66
def page
  @page ||= begin
    if page = Dugway.store.page(request.page_permalink)
      page['url'] = request.path
      page['full_url'] = request.url
      page['full_path'] = request.fullpath
      page
    else
      nil
    end
  end
end
params() click to toggle source
# File lib/dugway/controller.rb, line 54
def params
  request.params
end
post(path, &block) click to toggle source
# File lib/dugway/controller.rb, line 37
def post(path, &block)
  route('POST', path, &block)
end
redirect_to(path) click to toggle source
# File lib/dugway/controller.rb, line 113
def redirect_to(path)
  response.redirect(path)
  response.finish
end
render_file(file_name, variables={}) click to toggle source
# File lib/dugway/controller.rb, line 84
def render_file(file_name, variables={})
  if errors = request.session.delete(:errors)
    variables.merge!(:errors => errors)
  end

  template = Template.new(file_name)
  content = template.render(request, variables)

  response.write(content)
  response['Content-Type'] = template.content_type
  response.finish
end
render_json(object) click to toggle source
# File lib/dugway/controller.rb, line 101
def render_json(object)
  response.write(object.to_json)
  response['Content-Type'] = 'application/json'
  response.finish
end
render_not_found() click to toggle source
# File lib/dugway/controller.rb, line 107
def render_not_found
  response.write('Not Found')
  response.status = 404
  response.finish
end
render_page(variables={}) click to toggle source
# File lib/dugway/controller.rb, line 97
def render_page(variables={})
  render_file("#{ page['permalink'] }.html", variables.update({ :page => page }))
end
request() click to toggle source
# File lib/dugway/controller.rb, line 46
def request
  @request
end
response() click to toggle source
# File lib/dugway/controller.rb, line 50
def response
  @response
end
route(method, path) { || ... } click to toggle source
# File lib/dugway/controller.rb, line 14
def route(method, path, &block)
  routes.add_route(Proc.new { |env|
    @page = nil
    @request = Request.new(env)
    @response = Rack::Response.new

    if request.html? && page.blank?
      render_not_found
    else
      yield
    end
  }, {
    :request_method => method,
    :path_info => PathInterpreter.new(path).call
  })

  routes.rehash
end
store() click to toggle source
# File lib/dugway/controller.rb, line 58
def store
  Dugway.store
end

Public Instance Methods

call(env) click to toggle source
# File lib/dugway/controller.rb, line 119
def call(env)
  self.class.routes.call(env)
end