Class: Gapic::PathPattern::ResourceIdSegment
- Inherits:
-
Object
- Object
- Gapic::PathPattern::ResourceIdSegment
- 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
-
#pattern ⇒ String
readonly
The pattern of the segment, for the positional segment it is also a pattern of its resource.
-
#resource_names ⇒ Array<String>
readonly
The resource names in this segment.
-
#resource_patterns ⇒ Array<String>
readonly
The resource patterns associated with the resource_names of this segment.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.create_simple(name) ⇒ ResourceIdSegment
Initialization helper to create a simple resource without a pattern.
Instance Method Summary collapse
-
#arguments ⇒ Array<String>
Names of the segment's arguments.
-
#expected_path_for_dummy_values(start_index) ⇒ String
Returns a segment's pattern filled with dummy values.
-
#initialize(type, pattern, resource_names, resource_patterns = []) ⇒ ResourceIdSegment
constructor
A new instance of ResourceIdSegment.
-
#nontrivial_resource_pattern? ⇒ Boolean
Whether the segment provides a nontrivial resource pattern (not
*
or**
). -
#path_string ⇒ String
Path string for this segment.
-
#pattern_template ⇒ String
A pattern template for this segment.
-
#positional? ⇒ Boolean
Whether the segment is positional.
-
#provides_arguments? ⇒ Boolean
Whether the segment provides arguments.
-
#resource_id_segment? ⇒ Boolean
Whether the segment is a resource id segment.
-
#simplified_pattern ⇒ String
The pattern with the resource name (e.g.
foo
in{foo=bar/*}
) stripped. -
#to_regex_str ⇒ String
Creates a string with a regex representation of this segment's pattern.
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
#pattern ⇒ String (readonly)
Returns 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_names ⇒ Array<String> (readonly)
Returns 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_patterns ⇒ Array<String> (readonly)
Returns 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 |
#type ⇒ Object (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
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
#arguments ⇒ Array<String>
Names of the segment's arguments
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.
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 **
)
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_string ⇒ String
Path string for this segment
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_template ⇒ String
A pattern template for this segment
225 226 227 |
# File 'lib/gapic/path_pattern/segment.rb', line 225 def pattern_template "*" end |
#positional? ⇒ Boolean
Whether the segment is positional
170 171 172 |
# File 'lib/gapic/path_pattern/segment.rb', line 170 def positional? false end |
#provides_arguments? ⇒ Boolean
Whether the segment provides arguments
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
232 233 234 |
# File 'lib/gapic/path_pattern/segment.rb', line 232 def resource_id_segment? true end |
#simplified_pattern ⇒ String
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}
).
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_str ⇒ String
Creates a string with a regex representation of this segment's pattern
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 |