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)

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.



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

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

Instance Attribute Details

#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



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

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



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

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

Instance Method Details

#add_info(yard_object) ⇒ Object



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

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

#parse_tags(tags) ⇒ Object



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
71
72
73
74
75
76
77
78
79
# File 'lib/swagger_yard/model.rb', line 46

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
    end
  end

  self
end

#property(key) ⇒ Object



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

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