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: []) ⇒ Definition

Returns a new instance of Definition.



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

def initialize(id, ancestors: [])
  @id = id
  @title = ''
  @properties = []
  @description = ''
  @ancestors = ancestors
  @ignore_inherited = false
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



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

def add_property(property)
  @properties << property
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @properties.empty?
end

#inherited_properties(definitions) ⇒ Object



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

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



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

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

#to_doc(definitions) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/swaggard/swagger/definition.rb', line 43

def to_doc(definitions)
  {}.tap do |doc|
    doc['title'] = @title if @title.present?
    doc['type']  = 'object'

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

    all_properties = properties(definitions)

    doc['properties'] =  Hash[all_properties.map { |property| [property.id, property.to_doc] }]
    required_properties = all_properties.select(&:required?).map(&:id)
    doc['required'] = required_properties if required_properties.any?
  end

end