Class: OpenAPISourceTools::ApiObjects::ServerPath

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/openapi/sourcetools/apiobjects.rb

Overview

Represents path with fixed parts and variables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts) ⇒ ServerPath

Returns a new instance of ServerPath.



127
128
129
# File 'lib/openapi/sourcetools/apiobjects.rb', line 127

def initialize(parts)
  @parts = parts
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



125
126
127
# File 'lib/openapi/sourcetools/apiobjects.rb', line 125

def parts
  @parts
end

Instance Method Details

#<=>(other) ⇒ Object

Parameters are after fixed strings.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/openapi/sourcetools/apiobjects.rb', line 132

def <=>(other)
  pp = other.is_a?(Array) ? other : other.parts
  @parts.size.times do |k|
    return 1 if pp.size <= k # Longer comes after shorter.
    pk = @parts[k]
    ppk = pp[k]
    if pk.key?('fixed')
      if ppk.key?('fixed')
        c = pk['fixed'] <=> ppk['fixed']
      else
        return -1
      end
    else
      if ppk.key?('fixed')
        return 1
      else
        c = pk.fetch('parameter', '') <=> ppk.fetch('parameter', '')
      end
    end
    return c unless c.zero?
  end
  (@parts.size < pp.size) ? -1 : 0
end

#compare(other, range = nil) ⇒ Object

Not fit for sorting. Variable equals anything.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/openapi/sourcetools/apiobjects.rb', line 157

def compare(other, range = nil)
  pp = other.is_a?(Array) ? other : other.parts
  if range.nil?
    range = 0...@parts.size
  elsif range.is_a? Number
    range = range...(range + 1)
  end
  range.each do |k|
    return 1 if pp.size <= k # Longer comes after shorter.
    ppk = pp[k]
    next unless ppk.key?('fixed')
    pk = parts[k]
    next unless pk.key?('fixed')
    c = pk['fixed'] <=> ppk['fixed']
    return c unless c.zero?
  end
  (@parts.size < pp.size) ? -1 : 0
end