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.



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

def initialize type, pattern, resource_names, resource_patterns = []
  @type              = type
  @pattern           = pattern
  @resource_names    = resource_names
  @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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
# File 'lib/gapic/path_pattern/segment.rb', line 122

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
    @resource_patterns = resource_patterns
  end

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

  ##
  # Whether the segment provides a resource pattern
  # @return [Boolean]
  def resource_pattern?
    resource_patterns.any?
  end

  ##
  # Whether the segment provides a nontrivial resource pattern
  # @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
    type == :simple_resource_id ? "\#{#{resource_names[0]}}" : pattern.gsub("{", "\#{")
  end

  ##
  # A pattern template for this segment
  # @return [String]
  def pattern_template
    "*"
  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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
# File 'lib/gapic/path_pattern/segment.rb', line 122

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
    @resource_patterns = resource_patterns
  end

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

  ##
  # Whether the segment provides a resource pattern
  # @return [Boolean]
  def resource_pattern?
    resource_patterns.any?
  end

  ##
  # Whether the segment provides a nontrivial resource pattern
  # @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
    type == :simple_resource_id ? "\#{#{resource_names[0]}}" : pattern.gsub("{", "\#{")
  end

  ##
  # A pattern template for this segment
  # @return [String]
  def pattern_template
    "*"
  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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
# File 'lib/gapic/path_pattern/segment.rb', line 122

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
    @resource_patterns = resource_patterns
  end

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

  ##
  # Whether the segment provides a resource pattern
  # @return [Boolean]
  def resource_pattern?
    resource_patterns.any?
  end

  ##
  # Whether the segment provides a nontrivial resource pattern
  # @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
    type == :simple_resource_id ? "\#{#{resource_names[0]}}" : pattern.gsub("{", "\#{")
  end

  ##
  # A pattern template for this segment
  # @return [String]
  def pattern_template
    "*"
  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.



123
124
125
# File 'lib/gapic/path_pattern/segment.rb', line 123

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:



201
202
203
# File 'lib/gapic/path_pattern/segment.rb', line 201

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>)


166
167
168
# File 'lib/gapic/path_pattern/segment.rb', line 166

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



175
176
177
178
179
180
181
# File 'lib/gapic/path_pattern/segment.rb', line 175

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

Returns:

  • (Boolean)


152
153
154
# File 'lib/gapic/path_pattern/segment.rb', line 152

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

#path_stringString

Path string for this segment

Returns:

  • (String)


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

def path_string
  type == :simple_resource_id ? "\#{#{resource_names[0]}}" : pattern.gsub("{", "\#{")
end

#pattern_templateString

A pattern template for this segment

Returns:

  • (String)


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

def pattern_template
  "*"
end

#positional?Boolean

Whether the segment is positional

Returns:

  • (Boolean)


138
139
140
# File 'lib/gapic/path_pattern/segment.rb', line 138

def positional?
  false
end

#provides_arguments?Boolean

Whether the segment provides arguments

Returns:

  • (Boolean)


159
160
161
# File 'lib/gapic/path_pattern/segment.rb', line 159

def provides_arguments?
  true
end

#resource_pattern?Boolean

Whether the segment provides a resource pattern

Returns:

  • (Boolean)


145
146
147
# File 'lib/gapic/path_pattern/segment.rb', line 145

def resource_pattern?
  resource_patterns.any?
end