Class: Components

Inherits:
Object
  • Object
show all
Defined in:
lib/apiobjects.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keysObject

Returns the value of attribute ignored_keys.



37
38
39
# File 'lib/apiobjects.rb', line 37

def ignored_keys
  @ignored_keys
end

#itemsObject

Returns the value of attribute items.



37
38
39
# File 'lib/apiobjects.rb', line 37

def items
  @items
end

#pathObject (readonly)

Returns the value of attribute path.



36
37
38
# File 'lib/apiobjects.rb', line 36

def path
  @path
end

#prefixObject (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 add_options(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

#helpObject



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