Class: RailsLegacyMapper::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_legacy_mapper/mapper.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Resource, SingletonResource

Constant Summary collapse

INHERITABLE_OPTIONS =
:namespace, :shallow

Instance Method Summary collapse

Constructor Details

#initialize(set) ⇒ Mapper

:nodoc:



9
10
11
# File 'lib/rails_legacy_mapper/mapper.rb', line 9

def initialize(set) #:nodoc:
  @set = set
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(route_name, *args, &proc) ⇒ Object

:nodoc:



179
180
181
182
# File 'lib/rails_legacy_mapper/mapper.rb', line 179

def method_missing(route_name, *args, &proc) #:nodoc:
  super unless args.length >= 1 && proc.nil?
  named_route(route_name, *args)
end

Instance Method Details

#connect(path, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails_legacy_mapper/mapper.rb', line 13

def connect(path, options = {})
  options = options.dup

  if conditions = options.delete(:conditions)
    conditions = conditions.dup
    subdomain = conditions.delete(:subdomain)
    method = Array.wrap(conditions.delete(:method))
    method.map! { |m|
      if m == :head
        raise ArgumentError, "HTTP method HEAD is invalid in route conditions. Rails processes HEAD requests the same as GETs, returning just the response headers"
      end

      unless ActionDispatch::Routing::HTTP_METHODS.include?(m)
        raise ArgumentError, "Invalid HTTP method specified in route conditions"
      end

      m.to_s.dasherize.upcase
    }
  end

  path_prefix = options.delete(:path_prefix)
  name_prefix = options.delete(:name_prefix)
  namespace  = options.delete(:namespace)

  name = options.delete(:_name)
  name = "#{name_prefix}#{name}" if name_prefix

  requirements = options.delete(:requirements) || {}
  defaults = options.delete(:defaults) || {}
  options.each do |k, v|
    if v.is_a?(Regexp)
      if value = options.delete(k)
        requirements[k.to_sym] = value
      end
    else
      value = options.delete(k)
      defaults[k.to_sym] = value.is_a?(Symbol) ? value : value.to_param
    end
  end

  requirements.each do |_, requirement|
    if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
      raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
    end
    if requirement.multiline?
      raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
    end
  end

  requirements[:controller] ||= @set.controller_constraints

  if defaults[:controller]
    defaults[:action] ||= 'index'
    defaults[:controller] = defaults[:controller].to_s
    defaults[:controller] = "#{namespace}#{defaults[:controller]}" if namespace
  end

  if defaults[:action]
    defaults[:action] = defaults[:action].to_s
  end

  if path.is_a?(String)
    path = "#{path_prefix}/#{path}" if path_prefix
    path = path.gsub('.:format', '(.:format)')
    path = optionalize_trailing_dynamic_segments(path, requirements, defaults)
    glob = $1.to_sym if path =~ /\/\*(\w+)$/
    path = normalize_path(path)

    if glob && !defaults[glob].blank?
      raise ActionController::RoutingError, "paths cannot have non-empty default values"
    end
  end

  app = ActionDispatch::Routing::RouteSet::Dispatcher.new(:defaults => defaults, :glob => glob)

  conditions = {}
  conditions[:request_method] = method if method && !method.empty?
  conditions[:path_info] = path if path
  conditions[:subdomain] = subdomain if subdomain

  @set.add_route(app, conditions, requirements, defaults, name)
end

#named_route(name, path, options = {}) ⇒ Object

:nodoc:



166
167
168
169
# File 'lib/rails_legacy_mapper/mapper.rb', line 166

def named_route(name, path, options = {}) #:nodoc:
  options[:_name] = name
  connect(path, options)
end

#namespace(name, options = {}, &block) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/rails_legacy_mapper/mapper.rb', line 171

def namespace(name, options = {}, &block)
  if options[:namespace]
    with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
  else
    with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
  end
end

#resource(*entities, &block) ⇒ Object



336
337
338
339
# File 'lib/rails_legacy_mapper/mapper.rb', line 336

def resource(*entities, &block)
  options = entities.extract_options!
  entities.each { |entity| map_singleton_resource(entity, options.dup, &block) }
end

#resources(*entities, &block) ⇒ Object



331
332
333
334
# File 'lib/rails_legacy_mapper/mapper.rb', line 331

def resources(*entities, &block)
  options = entities.extract_options!
  entities.each { |entity| map_resource(entity, options.dup, &block) }
end

#root(options = {}) ⇒ Object

Creates a named route called “root” for matching the root level request.



157
158
159
160
161
162
163
164
# File 'lib/rails_legacy_mapper/mapper.rb', line 157

def root(options = {})
  if options.is_a?(Symbol)
    if source_route = @set.named_routes.routes[options]
      options = source_route.defaults.merge({ :conditions => source_route.conditions })
    end
  end
  named_route("root", '', options)
end