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.
-
#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
Constructor Details
#initialize(path, prefix, ignored_keys = %w[summary description examples example $anchor])) ⇒ Components
Returns a new instance of Components.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 45 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) end |
Instance Attribute Details
#anchor2ref ⇒ Object (readonly)
Returns the value of attribute anchor2ref.
42 43 44 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 42 def anchor2ref @anchor2ref end |
#ignored_keys ⇒ Object
Returns the value of attribute ignored_keys.
43 44 45 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 43 def ignored_keys @ignored_keys end |
#items ⇒ Object
Returns the value of attribute items.
43 44 45 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 43 def items @items end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
42 43 44 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 42 def path @path end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
42 43 44 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 42 def prefix @prefix end |
#schema_names ⇒ Object (readonly)
Returns the value of attribute schema_names.
42 43 44 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 42 def schema_names @schema_names end |
Instance Method Details
#add_options(opts) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 56 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 end |
#add_schema_name(name) ⇒ Object
69 70 71 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 69 def add_schema_name(name) @schema_names.add(name) end |
#alter_anchors ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 102 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
114 115 116 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 114 def anchor_ref_replacement(ref) @anchor2ref[ref[1...ref.size]] || ref end |
#help ⇒ Object
65 66 67 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 65 def help %(All fields are used in object equality comparisons except:\n#{@ignored_keys.to_a.sort!.join("\n")}) end |
#ref_string(name) ⇒ Object
73 74 75 76 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 73 def ref_string(name) return nil if name.nil? "#{@path}#{name}" end |
#reference(obj) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 78 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
94 95 96 97 98 99 100 |
# File 'lib/openapi/sourcetools/apiobjects.rb', line 94 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 |