Class: ActionDispatch::Routing::Mapper::Mapping

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

Overview

:nodoc:

Constant Summary collapse

ANCHOR_CHARACTERS_REGEX =
%r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
OPTIONAL_FORMAT_REGEX =
%r{(?:\(\.:format\)+|\.:format|/)\Z}
JOINED_SEPARATORS =

:nodoc:

SEPARATORS.join

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(set, ast, defaults, controller, default_action, modyoule, to, formatted, scope_constraints, blocks, via, options_constraints, anchor, options) ⇒ Mapping

Returns a new instance of Mapping.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/action_dispatch/routing/mapper.rb', line 100

def initialize(set, ast, defaults, controller, default_action, modyoule, to, formatted, scope_constraints, blocks, via, options_constraints, anchor, options)
  @defaults = defaults
  @set = set

  @to                 = to
  @default_controller = controller
  @default_action     = default_action
  @ast                = ast
  @anchor             = anchor
  @via                = via
  @internal           = options[:internal]

  path_params = ast.find_all(&:symbol?).map(&:to_sym)

  options = add_wildcard_options(options, formatted, ast)

  options = normalize_options!(options, path_params, modyoule)

  split_options = constraints(options, path_params)

  constraints = scope_constraints.merge Hash[split_options[:constraints] || []]

  if options_constraints.is_a?(Hash)
    @defaults = Hash[options_constraints.find_all { |key, default|
      URL_OPTIONS.include?(key) && (String === default || Integer === default)
    }].merge @defaults
    @blocks = blocks
    constraints.merge! options_constraints
  else
    @blocks = blocks(options_constraints)
  end

  requirements, conditions = split_constraints path_params, constraints
  verify_regexp_requirements requirements.map(&:last).grep(Regexp)

  formats = normalize_format(formatted)

  @requirements = formats[:requirements].merge Hash[requirements]
  @conditions = Hash[conditions]
  @defaults = formats[:defaults].merge(@defaults).merge(normalize_defaults(options))

  if path_params.include?(:action) && !@requirements.key?(:action)
    @defaults[:action] ||= 'index'
  end

  @required_defaults = (split_options[:required_defaults] || []).map(&:first)
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



61
62
63
# File 'lib/action_dispatch/routing/mapper.rb', line 61

def ast
  @ast
end

#default_actionObject (readonly)

Returns the value of attribute default_action.



60
61
62
# File 'lib/action_dispatch/routing/mapper.rb', line 60

def default_action
  @default_action
end

#default_controllerObject (readonly)

Returns the value of attribute default_controller.



60
61
62
# File 'lib/action_dispatch/routing/mapper.rb', line 60

def default_controller
  @default_controller
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



59
60
61
# File 'lib/action_dispatch/routing/mapper.rb', line 59

def defaults
  @defaults
end

#required_defaultsObject (readonly)

Returns the value of attribute required_defaults.



61
62
63
# File 'lib/action_dispatch/routing/mapper.rb', line 61

def required_defaults
  @required_defaults
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



59
60
61
# File 'lib/action_dispatch/routing/mapper.rb', line 59

def requirements
  @requirements
end

#toObject (readonly)

Returns the value of attribute to.



60
61
62
# File 'lib/action_dispatch/routing/mapper.rb', line 60

def to
  @to
end

Class Method Details

.build(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/action_dispatch/routing/mapper.rb', line 63

def self.build(scope, set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options)
  options = scope[:options].merge(options) if scope[:options]

  defaults = (scope[:defaults] || {}).dup
  scope_constraints = scope[:constraints] || {}

  new set, ast, defaults, controller, default_action, scope[:module], to, formatted, scope_constraints, scope[:blocks] || [], via, options_constraints, anchor, options
end

.check_via(via) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/action_dispatch/routing/mapper.rb', line 72

def self.check_via(via)
  if via.empty?
    msg = "You should not use the `match` method in your router without specifying an HTTP method.\n" \
      "If you want to expose your action to both GET and POST, add `via: [:get, :post]` option.\n" \
      "If you want to expose your action to GET, use `get` in the router:\n" \
      "  Instead of: match \"controller#action\"\n" \
      "  Do: get \"controller#action\""
    raise ArgumentError, msg
  end
  via
end

.normalize_path(path, format) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/action_dispatch/routing/mapper.rb', line 84

def self.normalize_path(path, format)
  path = Mapper.normalize_path(path)

  if format == true
    "#{path}.:format"
  elsif optional_format?(path, format)
    "#{path}(.:format)"
  else
    path
  end
end

.optional_format?(path, format) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/action_dispatch/routing/mapper.rb', line 96

def self.optional_format?(path, format)
  format != false && path !~ OPTIONAL_FORMAT_REGEX
end

Instance Method Details

#applicationObject



162
163
164
# File 'lib/action_dispatch/routing/mapper.rb', line 162

def application
  app(@blocks)
end

#conditionsObject



170
171
172
# File 'lib/action_dispatch/routing/mapper.rb', line 170

def conditions
  build_conditions @conditions, @set.request_class
end

#make_route(name, precedence) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/action_dispatch/routing/mapper.rb', line 148

def make_route(name, precedence)
  route = Journey::Route.new(name,
                    application,
                    path,
                    conditions,
                    required_defaults,
                    defaults,
                    request_method,
                    precedence,
                    @internal)

  route
end

#pathObject



166
167
168
# File 'lib/action_dispatch/routing/mapper.rb', line 166

def path
  build_path @ast, requirements, @anchor
end