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.



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

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



119
120
121
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
# File 'lib/gapic/path_pattern/segment.rb', line 119

class ResourceIdSegment
  attr_reader :type, :pattern, :resource_names, :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



119
120
121
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
# File 'lib/gapic/path_pattern/segment.rb', line 119

class ResourceIdSegment
  attr_reader :type, :pattern, :resource_names, :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



119
120
121
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
# File 'lib/gapic/path_pattern/segment.rb', line 119

class ResourceIdSegment
  attr_reader :type, :pattern, :resource_names, :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.



120
121
122
# File 'lib/gapic/path_pattern/segment.rb', line 120

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:



195
196
197
# File 'lib/gapic/path_pattern/segment.rb', line 195

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


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

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



169
170
171
172
173
174
175
# File 'lib/gapic/path_pattern/segment.rb', line 169

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)


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

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

#path_stringString

Path string for this segment

Returns:

  • (String)


180
181
182
# File 'lib/gapic/path_pattern/segment.rb', line 180

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

#pattern_templateString

A pattern template for this segment

Returns:

  • (String)


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

def pattern_template
  "*"
end

#positional?Boolean

Whether the segment is positional

Returns:

  • (Boolean)


132
133
134
# File 'lib/gapic/path_pattern/segment.rb', line 132

def positional?
  false
end

#provides_arguments?Boolean

Whether the segment provides arguments

Returns:

  • (Boolean)


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

def provides_arguments?
  true
end

#resource_pattern?Boolean

Whether the segment provides a resource pattern

Returns:

  • (Boolean)


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

def resource_pattern?
  resource_patterns.any?
end