Module: Pendragon::Padrino::ClassMethods

Defined in:
lib/pendragon/padrino/ext/class_methods.rb

Constant Summary collapse

CONTENT_TYPE_ALIASES =
{:htm => :html}
ROUTE_PRIORITY =
{:high => 0, :normal => 1, :low => 2}

Instance Method Summary collapse

Instance Method Details

#compiled_routerObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pendragon/padrino/ext/class_methods.rb', line 20

def compiled_router
  if @deferred_routes
    deferred_routes.each do |routes|
      routes.each do |(route, dest)|
        route.to(&dest)
        route.before_filters.flatten!
        route.after_filters.flatten!
      end
    end
    @deferred_routes = nil
  end
  router
end

#deferred_routesObject



34
35
36
# File 'lib/pendragon/padrino/ext/class_methods.rb', line 34

def deferred_routes
  @deferred_routes ||= ROUTE_PRIORITY.map{[]}
end

#make_path_with_params(args, params) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/pendragon/padrino/ext/class_methods.rb', line 46

def make_path_with_params(args, params)
  names, params_array = args.partition{ |arg| arg.is_a?(Symbol) }
  name = names[0, 2].join(" ").to_sym
  compiled_router.path(name, *(params_array << params))
rescue Pendragon::InvalidRouteException
  raise ::Padrino::Routing::UnrecognizedException, "Route mapping for url(#{name.inspect}) could not be found"
end

#rebase_url(url) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/pendragon/padrino/ext/class_methods.rb', line 59

def rebase_url(url)
  if url.start_with?('/')
    new_url = ''
    new_url << conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI']
    new_url << conform_uri(uri_root) if defined?(uri_root)
    new_url << url
  else
    url.blank? ? '/' : url
  end
end

#recognize_path(path) ⇒ Object



54
55
56
57
# File 'lib/pendragon/padrino/ext/class_methods.rb', line 54

def recognize_path(path)
  responses = @router.recognize_path(path)
  [responses[0], responses[1]]
end

#routerObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pendragon/padrino/ext/class_methods.rb', line 7

def router
  unless @router
    @router = ::Pendragon::Padrino::Router.new
    @router.configuration = Pendragon::Configuration.new
    if settings.respond_to?(:pendragon) && settings.pendragon.instance_of?(Hash)
      settings.pendragon.each_pair do |key, value|
        @router.configuration.send("#{key}=", value)
      end
    end
  end
  block_given? ? yield(@router) : @router
end

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



38
39
40
41
42
43
# File 'lib/pendragon/padrino/ext/class_methods.rb', line 38

def url(*args)
  params = args.extract_options!
  fragment = params.delete(:fragment) || params.delete(:anchor)
  path = make_path_with_params(args, value_to_param(params.symbolize_keys))
  rebase_url(fragment ? path << '#' << fragment : path)
end