Class: RailsLegacyMapper::Mapper
- Inherits:
-
Object
- Object
- RailsLegacyMapper::Mapper
- 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
- #connect(path, options = {}) ⇒ Object
-
#initialize(set) ⇒ Mapper
constructor
:nodoc:.
-
#method_missing(route_name, *args, &proc) ⇒ Object
:nodoc:.
-
#named_route(name, path, options = {}) ⇒ Object
:nodoc:.
- #namespace(name, options = {}, &block) ⇒ Object
- #resource(*entities, &block) ⇒ Object
- #resources(*entities, &block) ⇒ Object
-
#root(options = {}) ⇒ Object
Creates a named route called “root” for matching the root level request.
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, = {}) = .dup if conditions = .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 = .delete(:path_prefix) name_prefix = .delete(:name_prefix) namespace = .delete(:namespace) name = .delete(:_name) name = "#{name_prefix}#{name}" if name_prefix requirements = .delete(:requirements) || {} defaults = .delete(:defaults) || {} .each do |k, v| if v.is_a?(Regexp) if value = .delete(k) requirements[k.to_sym] = value end else value = .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, = {}) #:nodoc: [:_name] = name connect(path, ) 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, = {}, &block) if [:namespace] ({:path_prefix => "#{.delete(:path_prefix)}/#{name}", :name_prefix => "#{.delete(:name_prefix)}#{name}_", :namespace => "#{.delete(:namespace)}#{name}/" }.merge(), &block) else ({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(), &block) end end |
#resource(*entities, &block) ⇒ Object
336 337 338 339 |
# File 'lib/rails_legacy_mapper/mapper.rb', line 336 def resource(*entities, &block) = entities. entities.each { |entity| map_singleton_resource(entity, .dup, &block) } end |
#resources(*entities, &block) ⇒ Object
331 332 333 334 |
# File 'lib/rails_legacy_mapper/mapper.rb', line 331 def resources(*entities, &block) = entities. entities.each { |entity| map_resource(entity, .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( = {}) if .is_a?(Symbol) if source_route = @set.named_routes.routes[] = source_route.defaults.merge({ :conditions => source_route.conditions }) end end named_route("root", '', ) end |