Class: Gapic::PathPattern::Pattern
- Inherits:
-
Object
- Object
- Gapic::PathPattern::Pattern
- Defined in:
- lib/gapic/path_pattern/pattern.rb
Overview
A parsed pattern.
Instance Attribute Summary collapse
-
#path_pattern ⇒ String
readonly
The path pattern.
-
#segments ⇒ Array<PositionalSegment|ResourceIdSegment|CollectionIdSegment>
readonly
The parsed segments of the path pattern.
Instance Method Summary collapse
-
#arguments ⇒ Array<String>
All argument names from this pattern.
-
#double_star_pattern? ⇒ Boolean
Whether this is a basic double-star ("**") pattern.
-
#initialize(path_pattern, segments) ⇒ Pattern
constructor
A new instance of Pattern.
-
#nontrivial_pattern_segments? ⇒ Boolean
Whether pattern contains a segment with a nontrivial resource pattern.
-
#parent_template ⇒ String
A parent template to this pattern or an empty string if a pattern can not have a parent (too short).
-
#positional_segments? ⇒ Boolean
Whether pattern contains a positional segment.
-
#simplified_pattern ⇒ Object
The pattern with the resource names stripped from the ResourceId segments (e.g.
collections/{resource_id=foo/*}=>collections/foo/*). -
#star_pattern? ⇒ Boolean
Whether this is a basic single-star ("*") pattern.
-
#template ⇒ String
A template of this pattern - all resource id segments are stripped and replaced by '*'.
-
#to_regex_str ⇒ String
Converts the PathPattern into a regex string.
Constructor Details
#initialize(path_pattern, segments) ⇒ Pattern
Returns a new instance of Pattern.
35 36 37 38 |
# File 'lib/gapic/path_pattern/pattern.rb', line 35 def initialize path_pattern, segments @path_pattern = path_pattern @segments = segments end |
Instance Attribute Details
#path_pattern ⇒ String (readonly)
Returns The path pattern.
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 95 96 97 98 99 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 |
# File 'lib/gapic/path_pattern/pattern.rb', line 31 class Pattern attr_reader :path_pattern attr_reader :segments def initialize path_pattern, segments @path_pattern = path_pattern @segments = segments end ## # All argument names from this pattern # @return [Array<String>] def arguments @segments.select(&:provides_arguments?).map(&:arguments).flatten end ## # Whether this is a basic single-star ("*") pattern # @return [Boolean] def star_pattern? @segments.length == 1 && @segments[0].pattern == "*" end ## # Whether this is a basic double-star ("**") pattern # @return [Boolean] def double_star_pattern? @segments.length == 1 && @segments[0].pattern == "**" end ## # Converts the PathPattern into a regex string # @return [String] def to_regex_str regex_str = segments.first.to_regex_str # for double wildcards the leading `/`` is optional # e.g. `foo/**` should match `foo` # this is why segments 'bring' the leading separator # with them as they build the pattern segments.drop(1).each_with_index do |segment, _index| is_double_wildcard = segment.pattern == "**" regex_str = if is_double_wildcard "#{regex_str}(?:/.*)?" else "#{regex_str}/#{segment.to_regex_str}" end end regex_str end ## # Whether pattern contains a positional segment # @return [Boolean] def positional_segments? @segments.any?(&:positional?) end ## # Whether pattern contains a segment with a nontrivial resource pattern # @return [Boolean] def nontrivial_pattern_segments? @segments.any?(&:nontrivial_resource_pattern?) end ## # A template of this pattern - all resource id segments are # stripped and replaced by '*' # @return [String] def template @segments.map(&:pattern_template).join("/") end ## # A parent template to this pattern or an empty string if a pattern # can not have a parent (too short) # @return [String] def parent_template return nil if segments.length <= 2 last_segment = segments.last parent_pattern_segments = last_segment.provides_arguments? ? segments[0...-2] : segments[0...-1] parent_pattern_segments.map(&:pattern_template).join("/") end ## # The pattern with the resource names stripped # from the ResourceId segments # (e.g. `collections/{resource_id=foo/*}` => `collections/foo/*`) # def simplified_pattern @segments.map(&:simplified_pattern).join("/") end end |
#segments ⇒ Array<PositionalSegment|ResourceIdSegment|CollectionIdSegment> (readonly)
Returns The parsed segments of the path pattern.
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 95 96 97 98 99 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 |
# File 'lib/gapic/path_pattern/pattern.rb', line 31 class Pattern attr_reader :path_pattern attr_reader :segments def initialize path_pattern, segments @path_pattern = path_pattern @segments = segments end ## # All argument names from this pattern # @return [Array<String>] def arguments @segments.select(&:provides_arguments?).map(&:arguments).flatten end ## # Whether this is a basic single-star ("*") pattern # @return [Boolean] def star_pattern? @segments.length == 1 && @segments[0].pattern == "*" end ## # Whether this is a basic double-star ("**") pattern # @return [Boolean] def double_star_pattern? @segments.length == 1 && @segments[0].pattern == "**" end ## # Converts the PathPattern into a regex string # @return [String] def to_regex_str regex_str = segments.first.to_regex_str # for double wildcards the leading `/`` is optional # e.g. `foo/**` should match `foo` # this is why segments 'bring' the leading separator # with them as they build the pattern segments.drop(1).each_with_index do |segment, _index| is_double_wildcard = segment.pattern == "**" regex_str = if is_double_wildcard "#{regex_str}(?:/.*)?" else "#{regex_str}/#{segment.to_regex_str}" end end regex_str end ## # Whether pattern contains a positional segment # @return [Boolean] def positional_segments? @segments.any?(&:positional?) end ## # Whether pattern contains a segment with a nontrivial resource pattern # @return [Boolean] def nontrivial_pattern_segments? @segments.any?(&:nontrivial_resource_pattern?) end ## # A template of this pattern - all resource id segments are # stripped and replaced by '*' # @return [String] def template @segments.map(&:pattern_template).join("/") end ## # A parent template to this pattern or an empty string if a pattern # can not have a parent (too short) # @return [String] def parent_template return nil if segments.length <= 2 last_segment = segments.last parent_pattern_segments = last_segment.provides_arguments? ? segments[0...-2] : segments[0...-1] parent_pattern_segments.map(&:pattern_template).join("/") end ## # The pattern with the resource names stripped # from the ResourceId segments # (e.g. `collections/{resource_id=foo/*}` => `collections/foo/*`) # def simplified_pattern @segments.map(&:simplified_pattern).join("/") end end |
Instance Method Details
#arguments ⇒ Array<String>
All argument names from this pattern
43 44 45 |
# File 'lib/gapic/path_pattern/pattern.rb', line 43 def arguments @segments.select(&:provides_arguments?).map(&:arguments).flatten end |
#double_star_pattern? ⇒ Boolean
Whether this is a basic double-star ("**") pattern
57 58 59 |
# File 'lib/gapic/path_pattern/pattern.rb', line 57 def double_star_pattern? @segments.length == 1 && @segments[0].pattern == "**" end |
#nontrivial_pattern_segments? ⇒ Boolean
Whether pattern contains a segment with a nontrivial resource pattern
94 95 96 |
# File 'lib/gapic/path_pattern/pattern.rb', line 94 def nontrivial_pattern_segments? @segments.any?(&:nontrivial_resource_pattern?) end |
#parent_template ⇒ String
A parent template to this pattern or an empty string if a pattern can not have a parent (too short)
110 111 112 113 114 115 |
# File 'lib/gapic/path_pattern/pattern.rb', line 110 def parent_template return nil if segments.length <= 2 last_segment = segments.last parent_pattern_segments = last_segment.provides_arguments? ? segments[0...-2] : segments[0...-1] parent_pattern_segments.map(&:pattern_template).join("/") end |
#positional_segments? ⇒ Boolean
Whether pattern contains a positional segment
87 88 89 |
# File 'lib/gapic/path_pattern/pattern.rb', line 87 def positional_segments? @segments.any?(&:positional?) end |
#simplified_pattern ⇒ Object
The pattern with the resource names stripped
from the ResourceId segments
(e.g. collections/{resource_id=foo/*} => collections/foo/*)
122 123 124 |
# File 'lib/gapic/path_pattern/pattern.rb', line 122 def simplified_pattern @segments.map(&:simplified_pattern).join("/") end |
#star_pattern? ⇒ Boolean
Whether this is a basic single-star ("*") pattern
50 51 52 |
# File 'lib/gapic/path_pattern/pattern.rb', line 50 def star_pattern? @segments.length == 1 && @segments[0].pattern == "*" end |
#template ⇒ String
A template of this pattern - all resource id segments are stripped and replaced by '*'
102 103 104 |
# File 'lib/gapic/path_pattern/pattern.rb', line 102 def template @segments.map(&:pattern_template).join("/") end |
#to_regex_str ⇒ String
Converts the PathPattern into a regex string
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gapic/path_pattern/pattern.rb', line 64 def to_regex_str regex_str = segments.first.to_regex_str # for double wildcards the leading `/`` is optional # e.g. `foo/**` should match `foo` # this is why segments 'bring' the leading separator # with them as they build the pattern segments.drop(1).each_with_index do |segment, _index| is_double_wildcard = segment.pattern == "**" regex_str = if is_double_wildcard "#{regex_str}(?:/.*)?" else "#{regex_str}/#{segment.to_regex_str}" end end regex_str end |