Class: Grape::Router::BaseRoute

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/grape/router/base_route.rb

Direct Known Subclasses

GreedyRoute, Route

Defined Under Namespace

Classes: CaptureIndexCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, options = {}, namespace: nil, prefix: nil, settings: nil) ⇒ BaseRoute

Returns a new instance of BaseRoute.



17
18
19
20
21
22
23
# File 'lib/grape/router/base_route.rb', line 17

def initialize(pattern, options = {}, namespace: nil, prefix: nil, settings: nil)
  @pattern = pattern
  @options = options.is_a?(ActiveSupport::OrderedOptions) ? options : ActiveSupport::OrderedOptions.new.update(options)
  @namespace = namespace
  @prefix = prefix
  @settings = settings
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/grape/router/base_route.rb', line 10

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/grape/router/base_route.rb', line 10

def options
  @options
end

#patternObject (readonly)

Returns the value of attribute pattern.



10
11
12
# File 'lib/grape/router/base_route.rb', line 10

def pattern
  @pattern
end

#prefixObject (readonly)

Returns the value of attribute prefix.



10
11
12
# File 'lib/grape/router/base_route.rb', line 10

def prefix
  @prefix
end

#regexp_capture_groupObject (readonly)

The number of the group this route occupies in the union the router compiled it into. Only the union can say what it is, so it is written back by Grape::Router#compile! right after building one, and like regexp_capture_index it is never assigned at request time.



58
59
60
# File 'lib/grape/router/base_route.rb', line 58

def regexp_capture_group
  @regexp_capture_group
end

#regexp_capture_indexObject (readonly)

Assigned eagerly in #to_regexp (router compilation) rather than memoized here: this reader is called from request-time route matching on instances shared across threads, so it must not write state.



52
53
54
# File 'lib/grape/router/base_route.rb', line 52

def regexp_capture_index
  @regexp_capture_index
end

#settingsObject (readonly)

Returns the value of attribute settings.



10
11
12
# File 'lib/grape/router/base_route.rb', line 10

def settings
  @settings
end

Instance Method Details

#defaultObject

Deprecated.

Use Grape::Router#default_response, the name grape-swagger asks for. This one has to be written out rather than delegated like the rest: an ActiveSupport::OrderedOptions answers an unknown name with that key's value, but default is not unknown to it — it is Hash#default, the Hash's own default value — so a delegator would report nil for every route.



44
45
46
47
# File 'lib/grape/router/base_route.rb', line 44

def default
  Grape.deprecator.warn('`Grape::Router::Route#default` is deprecated. Use `#default_response` instead.')
  default_response
end

#failureObject



34
35
36
# File 'lib/grape/router/base_route.rb', line 34

def failure
  @options[:http_codes] || @options[:failure]
end

#pattern_regexpObject



60
61
62
# File 'lib/grape/router/base_route.rb', line 60

def pattern_regexp
  @pattern.to_regexp
end

#resolve_capture_group!(union_named_captures) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



71
72
73
# File 'lib/grape/router/base_route.rb', line 71

def resolve_capture_group!(union_named_captures)
  @regexp_capture_group = union_named_captures.fetch(regexp_capture_index).first
end

#successObject

success and failure are the desc DSL's names for entity and http_codes: the block form writes the canonical key, a keyword option keeps the name it was written with, so both spellings are read here. Without this they resolve through delegate_missing_to, which answers nil for whichever of the two keys the description did not use.



30
31
32
# File 'lib/grape/router/base_route.rb', line 30

def success
  @options[:entity] || @options[:success]
end

#to_regexp(index) ⇒ Object



64
65
66
67
# File 'lib/grape/router/base_route.rb', line 64

def to_regexp(index)
  @regexp_capture_index = CaptureIndexCache[index]
  Regexp.new("(?<#{regexp_capture_index}>#{pattern_regexp})")
end