Class: Gapic::PathPattern::ResourceIdSegment

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

Overview

A ResourceId segment in a path pattern. ResourceId segments can be simple, with one resource name or complex, with multiple resource names divided by separators

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, pattern, resource_names, resource_patterns = []) ⇒ ResourceIdSegment

Returns a new instance of ResourceIdSegment.



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gapic/path_pattern/segment.rb', line 154

def initialize type, pattern, resource_names, resource_patterns = []
  @type              = type
  @pattern           = pattern
  @resource_names    = resource_names

  # For the segments specified like `{foo}`, the implied resource pattern is `*`
  # `{foo}` === `{foo=*}`
  if resource_patterns.empty?
    resource_patterns = ["*"]
  end
  @resource_patterns = resource_patterns
end

Instance Attribute Details

#patternString (readonly)

Returns The pattern of the segment, for the positional segment it is also a pattern of its resource.

Returns:

  • (String)

    The pattern of the segment, for the positional segment it is also a pattern of its resource



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/gapic/path_pattern/segment.rb', line 148

class ResourceIdSegment
  attr_reader :type
  attr_reader :pattern
  attr_reader :resource_names
  attr_reader :resource_patterns

  def initialize type, pattern, resource_names, resource_patterns = []
    @type              = type
    @pattern           = pattern
    @resource_names    = resource_names

    # For the segments specified like `{foo}`, the implied resource pattern is `*`
    # `{foo}` === `{foo=*}`
    if resource_patterns.empty?
      resource_patterns = ["*"]
    end
    @resource_patterns = resource_patterns
  end

  ##
  # Whether the segment is positional
  # @return [Boolean]
  def positional?
    false
  end

  ##
  # Whether the segment provides a nontrivial resource pattern
  # (not `*` or `**`)
  # @return [Boolean]
  def nontrivial_resource_pattern?
    resource_patterns.any? { |res_pattern| !res_pattern.match?(/^\*+$/) }
  end

  ##
  # Whether the segment provides arguments
  # @return [Boolean]
  def provides_arguments?
    true
  end

  ##
  # Names of the segment's arguments
  # @return [Array<String>]
  def arguments
    resource_names
  end

  ##
  # Returns a segment's pattern filled with dummy values.
  # Names of the values are generated starting from the index provided.
  # @param start_index [Integer] a starting index for dummy value generation
  # @return [String] a pattern filled with dummy values
  def expected_path_for_dummy_values start_index
    return "value#{start_index}" if type == :simple_resource_id

    resource_names.each_with_index.reduce pattern do |exp_path, (res_name, index)|
      exp_path.sub "{#{res_name}}", "value#{start_index + index}"
    end
  end

  ##
  # Path string for this segment
  # @return [String]
  def path_string
    if type == :simple_resource_id
      name = resource_names[0]
      name = "binding.local_variable_get :#{name}" if Gapic::RubyInfo.keywords.include? name
      "\#{#{name}}"
    else
      pattern.gsub "{", "\#{"
    end
  end

  ##
  # A pattern template for this segment
  # @return [String]
  def pattern_template
    "*"
  end

  ##
  # Whether the segment is a resource id segment
  # @return [Boolean]
  def resource_id_segment?
    true
  end

  ##
  # The pattern with the resource name
  # (e.g. `foo` in `{foo=bar/*}`) stripped.
  # So `{foo=bar/*}` -> `bar/*`.
  #
  # Not implemented for multivariate segments
  # (e.g `{foo}~{bar}`).
  #
  # @return [String]
  def simplified_pattern
    if resource_patterns.count > 1
      raise "Not implemented for multivariate ResourceId segments"
    end
    resource_patterns[0]
  end

  ##
  # Creates a string with a regex representation of this segment's pattern
  # @return [String]
  def to_regex_str
    raise "Not implemented for multivariate ResourceId segments" if resource_patterns.count > 1

    resource_pattern = if resource_patterns[0].nil?
                         "*"
                       else
                         resource_patterns[0]
                       end

    resource_pattern_regex = Gapic::PathPattern::Parser.parse(resource_pattern).to_regex_str

    "(?<#{resource_names[0]}>#{resource_pattern_regex})"
  end

  ##
  # Initialization helper to create a simple resource without a pattern
  # @param name [String] resource name
  # @return [ResourceIdSegment]
  def self.create_simple name
    ResourceIdSegment.new :simple_resource_id, "{#{name}}", [name]
  end

  # @private
  def == other
    return false unless other.is_a? self.class

    type == other.type &&
      pattern == other.pattern &&
      resource_names == other.resource_names &&
      resource_patterns == other.resource_patterns
  end
end

#resource_namesArray<String> (readonly)

Returns The resource names in this segment.

Returns:

  • (Array<String>)

    The resource names in this segment



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/gapic/path_pattern/segment.rb', line 148

class ResourceIdSegment
  attr_reader :type
  attr_reader :pattern
  attr_reader :resource_names
  attr_reader :resource_patterns

  def initialize type, pattern, resource_names, resource_patterns = []
    @type              = type
    @pattern           = pattern
    @resource_names    = resource_names

    # For the segments specified like `{foo}`, the implied resource pattern is `*`
    # `{foo}` === `{foo=*}`
    if resource_patterns.empty?
      resource_patterns = ["*"]
    end
    @resource_patterns = resource_patterns
  end

  ##
  # Whether the segment is positional
  # @return [Boolean]
  def positional?
    false
  end

  ##
  # Whether the segment provides a nontrivial resource pattern
  # (not `*` or `**`)
  # @return [Boolean]
  def nontrivial_resource_pattern?
    resource_patterns.any? { |res_pattern| !res_pattern.match?(/^\*+$/) }
  end

  ##
  # Whether the segment provides arguments
  # @return [Boolean]
  def provides_arguments?
    true
  end

  ##
  # Names of the segment's arguments
  # @return [Array<String>]
  def arguments
    resource_names
  end

  ##
  # Returns a segment's pattern filled with dummy values.
  # Names of the values are generated starting from the index provided.
  # @param start_index [Integer] a starting index for dummy value generation
  # @return [String] a pattern filled with dummy values
  def expected_path_for_dummy_values start_index
    return "value#{start_index}" if type == :simple_resource_id

    resource_names.each_with_index.reduce pattern do |exp_path, (res_name, index)|
      exp_path.sub "{#{res_name}}", "value#{start_index + index}"
    end
  end

  ##
  # Path string for this segment
  # @return [String]
  def path_string
    if type == :simple_resource_id
      name = resource_names[0]
      name = "binding.local_variable_get :#{name}" if Gapic::RubyInfo.keywords.include? name
      "\#{#{name}}"
    else
      pattern.gsub "{", "\#{"
    end
  end

  ##
  # A pattern template for this segment
  # @return [String]
  def pattern_template
    "*"
  end

  ##
  # Whether the segment is a resource id segment
  # @return [Boolean]
  def resource_id_segment?
    true
  end

  ##
  # The pattern with the resource name
  # (e.g. `foo` in `{foo=bar/*}`) stripped.
  # So `{foo=bar/*}` -> `bar/*`.
  #
  # Not implemented for multivariate segments
  # (e.g `{foo}~{bar}`).
  #
  # @return [String]
  def simplified_pattern
    if resource_patterns.count > 1
      raise "Not implemented for multivariate ResourceId segments"
    end
    resource_patterns[0]
  end

  ##
  # Creates a string with a regex representation of this segment's pattern
  # @return [String]
  def to_regex_str
    raise "Not implemented for multivariate ResourceId segments" if resource_patterns.count > 1

    resource_pattern = if resource_patterns[0].nil?
                         "*"
                       else
                         resource_patterns[0]
                       end

    resource_pattern_regex = Gapic::PathPattern::Parser.parse(resource_pattern).to_regex_str

    "(?<#{resource_names[0]}>#{resource_pattern_regex})"
  end

  ##
  # Initialization helper to create a simple resource without a pattern
  # @param name [String] resource name
  # @return [ResourceIdSegment]
  def self.create_simple name
    ResourceIdSegment.new :simple_resource_id, "{#{name}}", [name]
  end

  # @private
  def == other
    return false unless other.is_a? self.class

    type == other.type &&
      pattern == other.pattern &&
      resource_names == other.resource_names &&
      resource_patterns == other.resource_patterns
  end
end

#resource_patternsArray<String> (readonly)

Returns The resource patterns associated with the resource_names of this segment.

Returns:

  • (Array<String>)

    The resource patterns associated with the resource_names of this segment



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/gapic/path_pattern/segment.rb', line 148

class ResourceIdSegment
  attr_reader :type
  attr_reader :pattern
  attr_reader :resource_names
  attr_reader :resource_patterns

  def initialize type, pattern, resource_names, resource_patterns = []
    @type              = type
    @pattern           = pattern
    @resource_names    = resource_names

    # For the segments specified like `{foo}`, the implied resource pattern is `*`
    # `{foo}` === `{foo=*}`
    if resource_patterns.empty?
      resource_patterns = ["*"]
    end
    @resource_patterns = resource_patterns
  end

  ##
  # Whether the segment is positional
  # @return [Boolean]
  def positional?
    false
  end

  ##
  # Whether the segment provides a nontrivial resource pattern
  # (not `*` or `**`)
  # @return [Boolean]
  def nontrivial_resource_pattern?
    resource_patterns.any? { |res_pattern| !res_pattern.match?(/^\*+$/) }
  end

  ##
  # Whether the segment provides arguments
  # @return [Boolean]
  def provides_arguments?
    true
  end

  ##
  # Names of the segment's arguments
  # @return [Array<String>]
  def arguments
    resource_names
  end

  ##
  # Returns a segment's pattern filled with dummy values.
  # Names of the values are generated starting from the index provided.
  # @param start_index [Integer] a starting index for dummy value generation
  # @return [String] a pattern filled with dummy values
  def expected_path_for_dummy_values start_index
    return "value#{start_index}" if type == :simple_resource_id

    resource_names.each_with_index.reduce pattern do |exp_path, (res_name, index)|
      exp_path.sub "{#{res_name}}", "value#{start_index + index}"
    end
  end

  ##
  # Path string for this segment
  # @return [String]
  def path_string
    if type == :simple_resource_id
      name = resource_names[0]
      name = "binding.local_variable_get :#{name}" if Gapic::RubyInfo.keywords.include? name
      "\#{#{name}}"
    else
      pattern.gsub "{", "\#{"
    end
  end

  ##
  # A pattern template for this segment
  # @return [String]
  def pattern_template
    "*"
  end

  ##
  # Whether the segment is a resource id segment
  # @return [Boolean]
  def resource_id_segment?
    true
  end

  ##
  # The pattern with the resource name
  # (e.g. `foo` in `{foo=bar/*}`) stripped.
  # So `{foo=bar/*}` -> `bar/*`.
  #
  # Not implemented for multivariate segments
  # (e.g `{foo}~{bar}`).
  #
  # @return [String]
  def simplified_pattern
    if resource_patterns.count > 1
      raise "Not implemented for multivariate ResourceId segments"
    end
    resource_patterns[0]
  end

  ##
  # Creates a string with a regex representation of this segment's pattern
  # @return [String]
  def to_regex_str
    raise "Not implemented for multivariate ResourceId segments" if resource_patterns.count > 1

    resource_pattern = if resource_patterns[0].nil?
                         "*"
                       else
                         resource_patterns[0]
                       end

    resource_pattern_regex = Gapic::PathPattern::Parser.parse(resource_pattern).to_regex_str

    "(?<#{resource_names[0]}>#{resource_pattern_regex})"
  end

  ##
  # Initialization helper to create a simple resource without a pattern
  # @param name [String] resource name
  # @return [ResourceIdSegment]
  def self.create_simple name
    ResourceIdSegment.new :simple_resource_id, "{#{name}}", [name]
  end

  # @private
  def == other
    return false unless other.is_a? self.class

    type == other.type &&
      pattern == other.pattern &&
      resource_names == other.resource_names &&
      resource_patterns == other.resource_patterns
  end
end

#typeObject (readonly)

Returns the value of attribute type.



149
150
151
# File 'lib/gapic/path_pattern/segment.rb', line 149

def type
  @type
end

Class Method Details

.create_simple(name) ⇒ ResourceIdSegment

Initialization helper to create a simple resource without a pattern

Parameters:

  • name (String)

    resource name

Returns:



273
274
275
# File 'lib/gapic/path_pattern/segment.rb', line 273

def self.create_simple name
  ResourceIdSegment.new :simple_resource_id, "{#{name}}", [name]
end

Instance Method Details

#argumentsArray<String>

Names of the segment's arguments

Returns:

  • (Array<String>)


192
193
194
# File 'lib/gapic/path_pattern/segment.rb', line 192

def arguments
  resource_names
end

#expected_path_for_dummy_values(start_index) ⇒ String

Returns a segment's pattern filled with dummy values. Names of the values are generated starting from the index provided.

Parameters:

  • start_index (Integer)

    a starting index for dummy value generation

Returns:

  • (String)

    a pattern filled with dummy values



201
202
203
204
205
206
207
# File 'lib/gapic/path_pattern/segment.rb', line 201

def expected_path_for_dummy_values start_index
  return "value#{start_index}" if type == :simple_resource_id

  resource_names.each_with_index.reduce pattern do |exp_path, (res_name, index)|
    exp_path.sub "{#{res_name}}", "value#{start_index + index}"
  end
end

#nontrivial_resource_pattern?Boolean

Whether the segment provides a nontrivial resource pattern (not * or **)

Returns:

  • (Boolean)


178
179
180
# File 'lib/gapic/path_pattern/segment.rb', line 178

def nontrivial_resource_pattern?
  resource_patterns.any? { |res_pattern| !res_pattern.match?(/^\*+$/) }
end

#path_stringString

Path string for this segment

Returns:

  • (String)


212
213
214
215
216
217
218
219
220
# File 'lib/gapic/path_pattern/segment.rb', line 212

def path_string
  if type == :simple_resource_id
    name = resource_names[0]
    name = "binding.local_variable_get :#{name}" if Gapic::RubyInfo.keywords.include? name
    "\#{#{name}}"
  else
    pattern.gsub "{", "\#{"
  end
end

#pattern_templateString

A pattern template for this segment

Returns:

  • (String)


225
226
227
# File 'lib/gapic/path_pattern/segment.rb', line 225

def pattern_template
  "*"
end

#positional?Boolean

Whether the segment is positional

Returns:

  • (Boolean)


170
171
172
# File 'lib/gapic/path_pattern/segment.rb', line 170

def positional?
  false
end

#provides_arguments?Boolean

Whether the segment provides arguments

Returns:

  • (Boolean)


185
186
187
# File 'lib/gapic/path_pattern/segment.rb', line 185

def provides_arguments?
  true
end

#resource_id_segment?Boolean

Whether the segment is a resource id segment

Returns:

  • (Boolean)


232
233
234
# File 'lib/gapic/path_pattern/segment.rb', line 232

def resource_id_segment?
  true
end

#simplified_patternString

The pattern with the resource name (e.g. foo in {foo=bar/*}) stripped. So {foo=bar/*} -> bar/*.

Not implemented for multivariate segments (e.g {foo}~{bar}).

Returns:

  • (String)


245
246
247
248
249
250
# File 'lib/gapic/path_pattern/segment.rb', line 245

def simplified_pattern
  if resource_patterns.count > 1
    raise "Not implemented for multivariate ResourceId segments"
  end
  resource_patterns[0]
end

#to_regex_strString

Creates a string with a regex representation of this segment's pattern

Returns:

  • (String)


255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/gapic/path_pattern/segment.rb', line 255

def to_regex_str
  raise "Not implemented for multivariate ResourceId segments" if resource_patterns.count > 1

  resource_pattern = if resource_patterns[0].nil?
                       "*"
                     else
                       resource_patterns[0]
                     end

  resource_pattern_regex = Gapic::PathPattern::Parser.parse(resource_pattern).to_regex_str

  "(?<#{resource_names[0]}>#{resource_pattern_regex})"
end