Class: Gapic::PathPattern::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/gapic/path_pattern/pattern.rb

Overview

A parsed pattern.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_patternString (readonly)

Returns The path pattern.

Returns:

  • (String)

    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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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 from this pattern, including ids for positional arguments
  # @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

  ##
  # Whether this pattern ends with a double-star ("**")
  # @return [Boolean]
  def ends_with_double_star_pattern?
    segments.last.pattern.end_with? "**"
  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

  ##
  # Converts the PathPattern into a regex string that matches a whole field
  # - adds markers for the beginning and end of the string
  # - adds handling of an optional tail `/` if needed
  # @return [String]
  def to_field_regex_str
    tail_slash_accept = ends_with_double_star_pattern? ? "" : "/?"
    "^#{to_regex_str}#{tail_slash_accept}$"
  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

#segmentsArray<PositionalSegment|ResourceIdSegment|CollectionIdSegment> (readonly)

Returns The parsed segments of the path pattern.

Returns:



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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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 from this pattern, including ids for positional arguments
  # @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

  ##
  # Whether this pattern ends with a double-star ("**")
  # @return [Boolean]
  def ends_with_double_star_pattern?
    segments.last.pattern.end_with? "**"
  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

  ##
  # Converts the PathPattern into a regex string that matches a whole field
  # - adds markers for the beginning and end of the string
  # - adds handling of an optional tail `/` if needed
  # @return [String]
  def to_field_regex_str
    tail_slash_accept = ends_with_double_star_pattern? ? "" : "/?"
    "^#{to_regex_str}#{tail_slash_accept}$"
  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

#argumentsArray<String>

All argument from this pattern, including ids for positional arguments

Returns:

  • (Array<String>)


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

Returns:

  • (Boolean)


57
58
59
# File 'lib/gapic/path_pattern/pattern.rb', line 57

def double_star_pattern?
  @segments.length == 1 && @segments[0].pattern == "**"
end

#ends_with_double_star_pattern?Boolean

Whether this pattern ends with a double-star ("**")

Returns:

  • (Boolean)


64
65
66
# File 'lib/gapic/path_pattern/pattern.rb', line 64

def ends_with_double_star_pattern?
  segments.last.pattern.end_with? "**"
end

#nontrivial_pattern_segments?Boolean

Whether pattern contains a segment with a nontrivial resource pattern

Returns:

  • (Boolean)


111
112
113
# File 'lib/gapic/path_pattern/pattern.rb', line 111

def nontrivial_pattern_segments?
  @segments.any?(&:nontrivial_resource_pattern?)
end

#parent_templateString

A parent template to this pattern or an empty string if a pattern can not have a parent (too short)

Returns:

  • (String)


127
128
129
130
131
132
# File 'lib/gapic/path_pattern/pattern.rb', line 127

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

Returns:

  • (Boolean)


104
105
106
# File 'lib/gapic/path_pattern/pattern.rb', line 104

def positional_segments?
  @segments.any?(&:positional?)
end

#simplified_patternObject

The pattern with the resource names stripped from the ResourceId segments (e.g. collections/{resource_id=foo/*} => collections/foo/*)



139
140
141
# File 'lib/gapic/path_pattern/pattern.rb', line 139

def simplified_pattern
  @segments.map(&:simplified_pattern).join("/")
end

#star_pattern?Boolean

Whether this is a basic single-star ("*") pattern

Returns:

  • (Boolean)


50
51
52
# File 'lib/gapic/path_pattern/pattern.rb', line 50

def star_pattern?
  @segments.length == 1 && @segments[0].pattern == "*"
end

#templateString

A template of this pattern - all resource id segments are stripped and replaced by '*'

Returns:

  • (String)


119
120
121
# File 'lib/gapic/path_pattern/pattern.rb', line 119

def template
  @segments.map(&:pattern_template).join("/")
end

#to_field_regex_strString

Converts the PathPattern into a regex string that matches a whole field

  • adds markers for the beginning and end of the string
  • adds handling of an optional tail / if needed

Returns:

  • (String)


96
97
98
99
# File 'lib/gapic/path_pattern/pattern.rb', line 96

def to_field_regex_str
  tail_slash_accept = ends_with_double_star_pattern? ? "" : "/?"
  "^#{to_regex_str}#{tail_slash_accept}$"
end

#to_regex_strString

Converts the PathPattern into a regex string

Returns:

  • (String)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gapic/path_pattern/pattern.rb', line 71

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