Module: Padrino::Routing::InstanceMethods

Defined in:
lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb

Overview

Instance methods related to recognizing and processing routes and serving static files.

Instance Method Summary collapse

Instance Method Details

#content_type(type = nil, params = {}) ⇒ Object

Return the request format, this is useful when we need to respond to a given Content-Type.

Examples:

get :index, :provides => :any do
  case content_type
    when :js    then ...
    when :json  then ...
    when :html  then ...
  end
end

Parameters:

  • type (Symbol, nil) (defaults to: nil)
  • params (Hash) (defaults to: {})


983
984
985
986
987
988
989
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 983

def content_type(type=nil, params={})
  unless type.nil?
    super(type, params)
    @_content_type = type
  end
  @_content_type
end

#current_path(*path_params) ⇒ Object

Returns the current path within a route from specified path_params.



922
923
924
925
926
927
928
929
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 922

def current_path(*path_params)
  if path_params.last.is_a?(Hash)
    path_params[-1] = params.merge(path_params[-1])
  else
    path_params << params
  end
  @route.path(*path_params)
end

#recognize_path(path) ⇒ Object

Returns the recognized path for a route.



915
916
917
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 915

def recognize_path(path)
  settings.recognize_path(path)
end

#routeObject

Returns the current route

Examples:

-if route.controller == :press
  %li=show_article


938
939
940
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 938

def route
  @route
end

#static!Object

Method for deliver static files.



958
959
960
961
962
963
964
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 958

def static!
  if path = static_file?(request.path_info)
    env['sinatra.static_file'] = path
    cache_control *settings.static_cache_control if settings.static_cache_control?
    send_file(path, :disposition => nil)
  end
end

#static_file?(path_info) ⇒ Boolean

This is mostly just a helper so request.path_info isn’t changed when serving files from the public directory.

Returns:

  • (Boolean)


946
947
948
949
950
951
952
953
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 946

def static_file?(path_info)
  return if (public_dir = settings.public_folder).nil?
  public_dir = File.expand_path(public_dir)
  path = File.expand_path(public_dir + unescape(path_info))
  return if path[0, public_dir.length] != public_dir
  return unless File.file?(path)
  return path
end

#url(*args) ⇒ Object Also known as: url_for

Instance method for url generation.

Examples:

url(:show, :id => 1)
url(:show, :name => :test)
url(:show, 1)
url("/foo")

See Also:



902
903
904
905
906
907
908
909
# File 'lib/vendored-middleman-deps/padrino-core-0.11.2/lib/padrino-core/application/routing.rb', line 902

def url(*args)
  # Delegate to Sinatra 1.2 for simple url("/foo")
  # http://www.sinatrarb.com/intro#Generating%20URLs
  return super if args.first.is_a?(String) && !args[1].is_a?(Hash)

  # Delegate to Padrino named route url generation
  settings.url(*args)
end