Class: SwaggerYard::Model

Inherits:
Object
  • Object
show all
Includes:
Example
Defined in:
lib/swagger_yard/model.rb

Overview

Carries id (the class name) and properties for a referenced

complex model object as defined by swagger schema

Constant Summary collapse

TAG_ORDER =
%w(model inherits discriminator property example additional_properties)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Example

#example, #example=

Constructor Details

#initializeModel

Returns a new instance of Model.



27
28
29
30
# File 'lib/swagger_yard/model.rb', line 27

def initialize
  @properties = []
  @inherits = []
end

Instance Attribute Details

#additional_propertiesObject (readonly)

Returns the value of attribute additional_properties.



8
9
10
# File 'lib/swagger_yard/model.rb', line 8

def additional_properties
  @additional_properties
end

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/swagger_yard/model.rb', line 8

def description
  @description
end

#discriminatorObject (readonly)

Returns the value of attribute discriminator.



8
9
10
# File 'lib/swagger_yard/model.rb', line 8

def discriminator
  @discriminator
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/swagger_yard/model.rb', line 8

def id
  @id
end

#inheritsObject (readonly)

Returns the value of attribute inherits.



8
9
10
# File 'lib/swagger_yard/model.rb', line 8

def inherits
  @inherits
end

#propertiesObject (readonly)

Returns the value of attribute properties.



8
9
10
# File 'lib/swagger_yard/model.rb', line 8

def properties
  @properties
end

Class Method Details

.from_yard_object(yard_object) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/swagger_yard/model.rb', line 11

def self.from_yard_object(yard_object)
  new.tap do |model|
    model.add_info(yard_object)
    model.parse_tags(yard_object.tags)
    yard_object.children.each do |child|
      next unless child.is_a?(YARD::CodeObjects::MethodObject)
      prop = Property.from_method(child)
      model.properties << prop if prop
    end
  end
end

.mangle(name) ⇒ Object



23
24
25
# File 'lib/swagger_yard/model.rb', line 23

def self.mangle(name)
  name.gsub(/[^[:alnum:]_]+/, '_')
end

Instance Method Details

#add_info(yard_object) ⇒ Object



36
37
38
39
# File 'lib/swagger_yard/model.rb', line 36

def add_info(yard_object)
  @description = yard_object.docstring
  @id = Model.mangle(yard_object.path)
end

#parse_tags(tags) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/swagger_yard/model.rb', line 47

def parse_tags(tags)
  sorted_tags = tags.each_with_index.sort_by { |t,i|
    [TAG_ORDER.index(t.tag_name), i] }.map(&:first)
  sorted_tags.each do |tag|
    case tag.tag_name
    when "model"
      @has_model_tag = true
      @id = Model.mangle(tag.text) unless tag.text.empty?
    when "property"
      prop = Property.from_tag(tag)
      @properties << prop if prop
    when "discriminator"
      prop = Property.from_tag(tag)
      if prop
        @properties << prop
        @discriminator ||= prop.name
      end
    when "inherits"
      @inherits << tag.text
    when "example"
      if tag.name && !tag.name.empty?
        if (prop = property(tag.name))
          prop.example = tag.text
        else
          SwaggerYard.log.warn("no property '#{tag.name}' defined yet to which to attach example: #{tag.text.inspect}")
        end
      else
        self.example = tag.text
      end
    when "additional_properties"
      @additional_properties = Type.new(tag.text).schema
    end
  end

  self
end

#property(key) ⇒ Object



41
42
43
# File 'lib/swagger_yard/model.rb', line 41

def property(key)
  properties.detect {|prop| prop.name == key }
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/swagger_yard/model.rb', line 32

def valid?
  !id.nil? && @has_model_tag
end