Class: OpenAPISourceTools::ApiObjects::Components
- Inherits:
-
Object
- Object
- OpenAPISourceTools::ApiObjects::Components
- Defined in:
- lib/openapi/sourcetools/apiobjects.rb
Overview
A component in the API specification for reference and anchor handling.
Instance Attribute Summary collapse
-
#anchor2ref ⇒ Object
readonly
Returns the value of attribute anchor2ref.
-
#ignored_keys ⇒ Object
Returns the value of attribute ignored_keys.
-
#items ⇒ Object
Returns the value of attribute items.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#retain_ignored ⇒ Object
Returns the value of attribute retain_ignored.
-
#schema_names ⇒ Object
readonly
Returns the value of attribute schema_names.
Instance Method Summary collapse
- #add_options(opts) ⇒ Object
- #add_schema_name(name) ⇒ Object
- #alter_anchors ⇒ Object
- #anchor_ref_replacement(ref) ⇒ Object
- #help ⇒ Object
-
#initialize(path, prefix, ignored_keys = %w[summary description examples example $anchor])) ⇒ Components
constructor
A new instance of Components.
- #ref_string(name) ⇒ Object
- #reference(obj) ⇒ Object
- #store_anchor(obj, ref = nil) ⇒ Object
- #to_reference_object(obj, ref = nil) ⇒ Object
Constructor Details
#initialize(path, prefix, ignored_keys = %w[summary description examples example $anchor])) ⇒ Components
Returns a new instance of Components.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 134 def initialize(path, prefix, ignored_keys = %w[summary description examples example $anchor]) path = "#/#{path.join('/')}/" if path.is_a?(Array) path = "#{path}/" unless path.end_with?('/') @path = path @prefix = prefix @anchor2ref = {} @schema_names = Set.new @items = {} @ignored_keys = Set.new(ignored_keys) @retain_ignored = false end |
Instance Attribute Details
#anchor2ref ⇒ Object (readonly)
Returns the value of attribute anchor2ref.
131 132 133 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 131 def anchor2ref @anchor2ref end |
#ignored_keys ⇒ Object
Returns the value of attribute ignored_keys.
132 133 134 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 132 def ignored_keys @ignored_keys end |
#items ⇒ Object
Returns the value of attribute items.
132 133 134 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 132 def items @items end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
131 132 133 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 131 def path @path end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
131 132 133 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 131 def prefix @prefix end |
#retain_ignored ⇒ Object
Returns the value of attribute retain_ignored.
132 133 134 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 132 def retain_ignored @retain_ignored end |
#schema_names ⇒ Object (readonly)
Returns the value of attribute schema_names.
131 132 133 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 131 def schema_names @schema_names end |
Instance Method Details
#add_options(opts) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 146 def (opts) opts.on('--use FIELD', 'Use FIELD in comparisons.') do |f| @ignored_keys.delete(f) end opts.on('--ignore FIELD', 'Ignore FIELD in comparisons.') do |f| @ignored_keys.add(f) end opts.on('--retain-ignored', 'Retain ignored fields in reference object.') do @retain_ignored = true end end |
#add_schema_name(name) ⇒ Object
162 163 164 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 162 def add_schema_name(name) @schema_names.add(name) end |
#alter_anchors ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 206 def alter_anchors replacements = {} @anchor2ref.each_key do |a| next if @schema_names.member?(a) replacements[a] = ref_string(a) @schema_names.add(a) end replacements.each do |a, r| @anchor2ref[a] = r end end |
#anchor_ref_replacement(ref) ⇒ Object
218 219 220 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 218 def anchor_ref_replacement(ref) @anchor2ref[ref[1...ref.size]] || ref end |
#help ⇒ Object
158 159 160 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 158 def help %(All fields are used in object equality comparisons except:\n#{@ignored_keys.to_a.sort!.join("\n")}) end |
#ref_string(name) ⇒ Object
166 167 168 169 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 166 def ref_string(name) return nil if name.nil? "#{@path}#{name}" end |
#reference(obj) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 171 def reference(obj) # Check if identical schema has been added. If so, return the $ref string. @items.each do |k, v| return ref_string(k) if ApiObjects.same(obj, v, @ignored_keys) end # One of the numbers will not match existing keys. More number than keys. (@items.size + 1).times do |n| # 'x' is to simplify find and replace (Schema1x vs Schema1 and Schema10) cand = "#{@prefix}#{n}x" next if @items.key?(cand) @items[cand] = obj.merge @schema_names.add(cand) return ref_string(cand) end end |
#store_anchor(obj, ref = nil) ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 198 def store_anchor(obj, ref = nil) anchor_name = obj['$anchor'] return if anchor_name.nil? ref = obj['$ref'] if ref.nil? raise StandardError, 'ref is nil and no $ref or it is nil' if ref.nil? @anchor2ref[anchor_name] = ref end |
#to_reference_object(obj, ref = nil) ⇒ Object
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 187 def to_reference_object(obj, ref = nil) ref = reference(obj) if ref.nil? if @retain_ignored obj.delete_if { |k, _v| !@ignored_keys.member?(k) } else obj.clear end obj['$ref'] = ref obj end |