class SwaggerYard::Model

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

complex model object as defined by swagger schema

Constants

TAG_ORDER

Attributes

additional_properties[R]
description[R]
discriminator[R]
id[R]
inherits[R]
properties[R]

Public Class Methods

from_yard_object(yard_object) click to toggle source
# 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) click to toggle source
# File lib/swagger_yard/model.rb, line 23
def self.mangle(name)
  name.gsub(/[^[:alnum:]_]+/, '_')
end
new() click to toggle source
# File lib/swagger_yard/model.rb, line 27
def initialize
  @properties = []
  @inherits = []
end

Public Instance Methods

add_info(yard_object) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/swagger_yard/model.rb, line 41
def property(key)
  properties.detect {|prop| prop.name == key }
end
valid?() click to toggle source
# File lib/swagger_yard/model.rb, line 32
def valid?
  !id.nil? && @has_model_tag
end