Class: Gapic::PathPattern::PositionalSegment

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

Overview

A positional segment in a path pattern. positional segments have a pattern of wildcards and do not carry a name

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, pattern) ⇒ PositionalSegment

Returns a new instance of PositionalSegment.



36
37
38
39
40
# File 'lib/gapic/path_pattern/segment.rb', line 36

def initialize position, pattern
  @type       = :positional
  @position   = position
  @pattern    = pattern
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



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

class PositionalSegment
  attr_reader :type
  attr_reader :position
  attr_reader :pattern

  def initialize position, pattern
    @type       = :positional
    @position   = position
    @pattern    = pattern
  end

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

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

  ##
  # Whether the segment provides a nontrivial resource pattern
  # @return [Boolean]
  def nontrivial_resource_pattern?
    false
  end

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

  ##
  # Names of the segment's arguments
  # @return [Array<String>]
  def arguments
    [position.to_s]
  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
    "value#{start_index}"
  end

  ##
  # Path string for this segment
  # @return [String]
  def path_string
    "\#{#{position}}"
  end

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

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

  ##
  # The difference between `simplified_pattern` and `pattern`
  # does not exist for the Positional segments
  # @return [String]
  def simplified_pattern
    pattern
  end

  ##
  # Creates a string with a regex representation of this segment's pattern
  # @return [String]
  def to_regex_str
    if pattern == "**"
      ".*"
    else
      "[^/]+"
    end
  end

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

    pattern == other.pattern && position == other.position
  end
end

#positionInteger (readonly)

Returns The argument position of this segment i.e. it's position if we remove all non-positional segments from the pattern.

Returns:

  • (Integer)

    The argument position of this segment i.e. it's position if we remove all non-positional segments from the 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
# File 'lib/gapic/path_pattern/segment.rb', line 31

class PositionalSegment
  attr_reader :type
  attr_reader :position
  attr_reader :pattern

  def initialize position, pattern
    @type       = :positional
    @position   = position
    @pattern    = pattern
  end

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

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

  ##
  # Whether the segment provides a nontrivial resource pattern
  # @return [Boolean]
  def nontrivial_resource_pattern?
    false
  end

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

  ##
  # Names of the segment's arguments
  # @return [Array<String>]
  def arguments
    [position.to_s]
  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
    "value#{start_index}"
  end

  ##
  # Path string for this segment
  # @return [String]
  def path_string
    "\#{#{position}}"
  end

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

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

  ##
  # The difference between `simplified_pattern` and `pattern`
  # does not exist for the Positional segments
  # @return [String]
  def simplified_pattern
    pattern
  end

  ##
  # Creates a string with a regex representation of this segment's pattern
  # @return [String]
  def to_regex_str
    if pattern == "**"
      ".*"
    else
      "[^/]+"
    end
  end

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

    pattern == other.pattern && position == other.position
  end
end

#typeObject (readonly)

Returns the value of attribute type.



32
33
34
# File 'lib/gapic/path_pattern/segment.rb', line 32

def type
  @type
end

Instance Method Details

#argumentsArray<String>

Names of the segment's arguments

Returns:

  • (Array<String>)


73
74
75
# File 'lib/gapic/path_pattern/segment.rb', line 73

def arguments
  [position.to_s]
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



82
83
84
# File 'lib/gapic/path_pattern/segment.rb', line 82

def expected_path_for_dummy_values start_index
  "value#{start_index}"
end

#nontrivial_resource_pattern?Boolean

Whether the segment provides a nontrivial resource pattern

Returns:

  • (Boolean)


59
60
61
# File 'lib/gapic/path_pattern/segment.rb', line 59

def nontrivial_resource_pattern?
  false
end

#path_stringString

Path string for this segment

Returns:

  • (String)


89
90
91
# File 'lib/gapic/path_pattern/segment.rb', line 89

def path_string
  "\#{#{position}}"
end

#pattern_templateString

A pattern template for this segment

Returns:

  • (String)


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

def pattern_template
  pattern
end

#positional?Boolean

Whether the segment is positional

Returns:

  • (Boolean)


45
46
47
# File 'lib/gapic/path_pattern/segment.rb', line 45

def positional?
  true
end

#provides_arguments?Boolean

Whether the segment provides arguments

Returns:

  • (Boolean)


66
67
68
# File 'lib/gapic/path_pattern/segment.rb', line 66

def provides_arguments?
  true
end

#resource_id_segment?Boolean

Whether the segment is a resource id segment

Returns:

  • (Boolean)


103
104
105
# File 'lib/gapic/path_pattern/segment.rb', line 103

def resource_id_segment?
  false
end

#resource_pattern?Boolean

Whether the segment provides a resource pattern

Returns:

  • (Boolean)


52
53
54
# File 'lib/gapic/path_pattern/segment.rb', line 52

def resource_pattern?
  true
end

#simplified_patternString

The difference between simplified_pattern and pattern does not exist for the Positional segments

Returns:

  • (String)


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

def simplified_pattern
  pattern
end

#to_regex_strString

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

Returns:

  • (String)


118
119
120
121
122
123
124
# File 'lib/gapic/path_pattern/segment.rb', line 118

def to_regex_str
  if pattern == "**"
    ".*"
  else
    "[^/]+"
  end
end