Class: Swaggard::Swagger::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/swaggard/swagger/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, ancestors: [], collection: false) ⇒ Definition

Returns a new instance of Definition.



8
9
10
11
12
13
14
15
16
# File 'lib/swaggard/swagger/definition.rb', line 8

def initialize(id, ancestors: [], collection: false)
  @id = id
  @title = ''
  @properties = []
  @description = ''
  @ancestors = ancestors
  @ignore_inherited = false
  @collection = collection
end

Instance Attribute Details

#description=(value) ⇒ Object (writeonly)

Sets the attribute description

Parameters:

  • value

    the value to set the attribute description to.



6
7
8
# File 'lib/swaggard/swagger/definition.rb', line 6

def description=(value)
  @description = value
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/swaggard/swagger/definition.rb', line 5

def id
  @id
end

#ignore_inherited=(value) ⇒ Object (writeonly)

Sets the attribute ignore_inherited

Parameters:

  • value

    the value to set the attribute ignore_inherited to.



6
7
8
# File 'lib/swaggard/swagger/definition.rb', line 6

def ignore_inherited=(value)
  @ignore_inherited = value
end

#title=(value) ⇒ Object (writeonly)

Sets the attribute title

Parameters:

  • value

    the value to set the attribute title to.



6
7
8
# File 'lib/swaggard/swagger/definition.rb', line 6

def title=(value)
  @title = value
end

Instance Method Details

#add_property(property) ⇒ Object



18
19
20
# File 'lib/swaggard/swagger/definition.rb', line 18

def add_property(property)
  @properties << property
end

#empty?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/swaggard/swagger/definition.rb', line 22

def empty?
  @properties.empty?
end

#inherited_properties(definitions) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/swaggard/swagger/definition.rb', line 32

def inherited_properties(definitions)
  return [] if @ignore_inherited

  @ancestors.flat_map do |ancestor|
    definition = definitions[ancestor]

    next unless definition && definition.id != id

    definition.properties(definitions)
  end.compact
end

#properties(definitions) ⇒ Object



26
27
28
29
30
# File 'lib/swaggard/swagger/definition.rb', line 26

def properties(definitions)
  inherited_properties(definitions)
    .concat(@properties)
    .uniq { |property| property.id }
end

#to_doc(definitions) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/swaggard/swagger/definition.rb', line 44

def to_doc(definitions)
  all_properties = properties(definitions)
  properties_hash = Hash[all_properties.map { |property| [property.id, property.to_doc] }]
  required_properties = all_properties.select(&:required?).map(&:id)

  if @collection
    items = { 'type' => 'object', 'properties' => properties_hash }
    items['required'] = required_properties if required_properties.any?

    {}.tap do |doc|
      doc['title'] = @title if @title.present?
      doc['type'] = 'array'
      doc['description'] = @description if @description.present?
      doc['items'] = items
    end
  else
    {}.tap do |doc|
      doc['title'] = @title if @title.present?
      doc['type']  = 'object'

      doc['description'] = @description if @description.present?

      doc['properties'] = properties_hash
      doc['required'] = required_properties if required_properties.any?
    end
  end
end