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 $anchor])) ⇒ Components

Returns a new instance of Components.



39
40
41
42
43
44
45
46
47
48
# File 'lib/apiobjects.rb', line 39

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

#anchor2refObject (readonly)

Returns the value of attribute anchor2ref.



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

def anchor2ref
  @anchor2ref
end

#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

#schema_namesObject (readonly)

Returns the value of attribute schema_names.



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

def schema_names
  @schema_names
end

Instance Method Details

#add_options(opts) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/apiobjects.rb', line 50

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

#add_schema_name(name) ⇒ Object



63
64
65
# File 'lib/apiobjects.rb', line 63

def add_schema_name(name)
  @schema_names.add(name)
end

#alter_anchorsObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/apiobjects.rb', line 96

def alter_anchors
  replacements = {}
  @anchor2ref.each do |a, r|
    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



108
109
110
# File 'lib/apiobjects.rb', line 108

def anchor_ref_replacement(ref)
  @anchor2ref[ref[1...ref.size]] || ref
end

#helpObject



59
60
61
# File 'lib/apiobjects.rb', line 59

def help
  %(All fields are used in object equality comparisons except:\n#{@ignored_keys.to_a.sort!.join("\n")})
end

#ref_string(name) ⇒ Object



67
68
69
70
# File 'lib/apiobjects.rb', line 67

def ref_string(name)
  return nil if name.nil?
  "#{@path}#{name}"
end

#reference(obj) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apiobjects.rb', line 72

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
    @schema_names.add(cand)
    return ref_string(cand)
  end
end

#store_anchor(obj, ref = nil) ⇒ Object

Raises:

  • (Exception)


88
89
90
91
92
93
94
# File 'lib/apiobjects.rb', line 88

def store_anchor(obj, ref = nil)
  anchor_name = obj['$anchor']
  return if anchor_name.nil?
  ref = obj['$ref'] if ref.nil?
  raise Exception, 'ref is nil and no $ref or it is nil' if ref.nil?
  @anchor2ref[anchor_name] = ref
end