Sha256: 158f1f7e98172b533ab139fa93150830e31575c183f099dbb3ba97879b850c9d

Contents?: true

Size: 962 Bytes

Versions: 3

Compression:

Stored size: 962 Bytes

Contents

module SwaggerYard
  #
  # Carries id (the class name) and properties for a referenced
  #   complex model object as defined by swagger schema
  #
  class Model
    attr_reader :id

    def self.from_yard_object(yard_object)
      new.tap do |model|
        model.parse_tags(yard_object.tags)
      end
    end

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

    def initialize
      @properties = []
    end

    def valid?
      !id.nil?
    end

    def parse_tags(tags)
      tags.each do |tag|
        case tag.tag_name
        when "model"
          @id = Model.mangle(tag.text)
        when "property"
          @properties << Property.from_tag(tag)
        end
      end

      self
    end

    def to_h
      {}.tap do |h|
        h["properties"] = Hash[@properties.map {|p| [p.name, p.to_h]}]
        h["required"] = @properties.select(&:required?).map(&:name) if @properties.detect(&:required?)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
swagger_yard-0.3.5 lib/swagger_yard/model.rb
swagger_yard-0.3.4 lib/swagger_yard/model.rb
swagger_yard-0.3.3 lib/swagger_yard/model.rb