Class: Grape::Router::Pattern

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

Defined Under Namespace

Classes: Path, PatternCache

Constant Summary collapse

DEFAULT_CAPTURES =
%w[format version].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin:, suffix:, anchor:, params:, version:, requirements:) ⇒ Pattern

Returns a new instance of Pattern.



24
25
26
27
28
29
30
31
32
33
# File 'lib/grape/router/pattern.rb', line 24

def initialize(origin:, suffix:, anchor:, params:, version:, requirements:)
  @origin = origin
  @anchor = anchor
  @version = version
  @requirements = requirements
  @path = PatternCache[[build_path_from_pattern(@origin, anchor), suffix]]
  @pattern = MustermannPattern.new(@path, uri_decode: true, params:, capture: extract_capture(version, requirements))
  @to_regexp = @pattern.to_regexp
  @captures = @to_regexp.names.any?
end

Instance Attribute Details

#anchorObject (readonly)

Returns the value of attribute anchor.



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

def anchor
  @anchor
end

#originObject (readonly)

Returns the value of attribute origin.



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

def origin
  @origin
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#patternObject (readonly)

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



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

def requirements
  @requirements
end

#to_regexpObject (readonly)

Returns the value of attribute to_regexp.



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

def to_regexp
  @to_regexp
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.build(path:, namespace:, settings:, anchor:, params:, version:, requirements:) ⇒ Object

Build a Pattern from a raw path, namespace and the API's inheritable settings. Path owns the settings-aware assembly of +origin+/+suffix+; the Pattern itself stays value-based (see #initialize).



19
20
21
22
# File 'lib/grape/router/pattern.rb', line 19

def self.build(path:, namespace:, settings:, anchor:, params:, version:, requirements:)
  built_path = Path.new(path, namespace, settings)
  new(origin: built_path.origin, suffix: built_path.suffix, anchor:, params:, version:, requirements:)
end

Instance Method Details

#captures?Boolean

True when the compiled pattern has named captures to extract from a matched path. A fully static path has none — not even format, which the suffix spells as a literal — so asking Mustermann for its params would run the regexp a second time (the router's union already matched it) only to hand back an empty Hash.

Resolved once here rather than per request: Regexp#names builds an Array and a String per capture, and Ruby offers no predicate that skips that (+named_captures+ builds a Hash, and scanning the source for (?< would count lookbehinds, which name nothing).

Returns:

  • (Boolean)


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

def captures?
  @captures
end

#captures_defaultObject



49
50
51
52
53
# File 'lib/grape/router/pattern.rb', line 49

def captures_default
  to_regexp.names
           .delete_if { |n| DEFAULT_CAPTURES.include?(n) }
           .to_h { |k| [k, ''] }
end