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) ⇒ Definition

Returns a new instance of Definition.



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

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

#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



15
16
17
# File 'lib/swaggard/swagger/definition.rb', line 15

def add_property(property)
  @properties << property
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @properties.empty?
end

#to_docObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/swaggard/swagger/definition.rb', line 23

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

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

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

end