Class: Components
- Inherits:
-
Object
- Object
- Components
- Defined in:
- lib/apiobjects.rb
Instance Attribute Summary collapse
-
#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.
Instance Method Summary collapse
- #add_options(opts) ⇒ Object
- #help ⇒ Object
-
#initialize(path, prefix, ignored_keys = %w[summary description examples example])) ⇒ Components
constructor
A new instance of Components.
- #ref_string(name) ⇒ Object
- #reference(obj) ⇒ Object
Constructor Details
#initialize(path, prefix, ignored_keys = %w[summary description examples example])) ⇒ Components
39 40 41 42 43 44 45 46 |
# File 'lib/apiobjects.rb', line 39 def initialize(path, prefix, ignored_keys = %w[summary description examples example]) @items = {} path = "#/#{path.join('/')}/" if path.is_a?(Array) path = "#{path}/" unless path.end_with?('/') @path = path @prefix = prefix @ignored_keys = Set.new(ignored_keys) end |
Instance Attribute Details
#ignored_keys ⇒ Object
Returns the value of attribute ignored_keys.
37 38 39 |
# File 'lib/apiobjects.rb', line 37 def ignored_keys @ignored_keys end |
#items ⇒ Object
Returns the value of attribute items.
37 38 39 |
# File 'lib/apiobjects.rb', line 37 def items @items end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
36 37 38 |
# File 'lib/apiobjects.rb', line 36 def path @path end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
36 37 38 |
# File 'lib/apiobjects.rb', line 36 def prefix @prefix end |
Instance Method Details
#add_options(opts) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/apiobjects.rb', line 48 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 |
#help ⇒ Object
57 58 59 |
# File 'lib/apiobjects.rb', line 57 def help %(All fields are used in object equality comparisons except:\n#{@ignored_keys.to_a.sort!.join("\n")}) end |
#ref_string(name) ⇒ Object
61 62 63 |
# File 'lib/apiobjects.rb', line 61 def ref_string(name) "#{@path}#{name}" end |
#reference(obj) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/apiobjects.rb', line 65 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 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 return ref_string(cand) end end |