Module: Reins::Routes::UrlHelpers

Included in:
Controller, View
Defined in:
lib/reins/routes/url_helpers.rb

Overview

Single shared module mixed into Reins::Controller and Reins::View. Methods are added by the DSL at route declaration time, removed on ‘reset!` (called by `Application#route` so each fresh app has a clean helper namespace).

One process is expected to host one Reins::Application; running two in the same process will overwrite each other’s helpers.

Class Method Summary collapse

Class Method Details

.define_for(rule) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/reins/routes/url_helpers.rb', line 16

def define_for(rule)
  return unless rule.name

  name = rule.name.to_s
  pattern = rule.pattern
  vars = rule.vars

  define_path_method("#{name}_path", pattern, vars)
  define_url_method("#{name}_url", pattern, vars)
end

.expand_path(pattern, vars, args, opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/reins/routes/url_helpers.rb', line 27

def expand_path(pattern, vars, args, opts)
  values = extract_values(vars, args, opts)
  path = pattern.dup
  vars.each do |var|
    value = values[var.to_s] || values[var.to_sym]
    raise ArgumentError, "missing value for :#{var}" if value.nil?

    path = path.sub(":#{var}", value.to_s)
  end
  path
end

.reset!Object



12
13
14
# File 'lib/reins/routes/url_helpers.rb', line 12

def reset!
  instance_methods(false).each { |m| remove_method(m) }
end