Class: SwaggerYard::Model

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.



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

def initialize
  @properties = []
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.from_tags(tags) ⇒ Object



17
18
19
20
21
# File 'lib/swagger_yard/model.rb', line 17

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

.from_yard_object(yard_object) ⇒ Object



13
14
15
# File 'lib/swagger_yard/model.rb', line 13

def self.from_yard_object(yard_object)
  from_tags(yard_object.tags) if yard_object
end

.from_yard_objects(yard_objects) ⇒ Object



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

def self.from_yard_objects(yard_objects)
  from_yard_object(yard_objects.detect {|o| o.type == :class })
end

Instance Method Details

#model_from_model_list(model_list, model_name) ⇒ Object



55
56
57
# File 'lib/swagger_yard/model.rb', line 55

def model_from_model_list(model_list, model_name)
  model_list.find{|model| model.id == model_name}
end

#parse_tags(tags) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/swagger_yard/model.rb', line 31

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

  self
end

#properties_model_namesObject



44
45
46
# File 'lib/swagger_yard/model.rb', line 44

def properties_model_names
  @properties.map(&:model_name).compact
end

#recursive_properties_model_names(model_list) ⇒ Object



48
49
50
51
52
53
# File 'lib/swagger_yard/model.rb', line 48

def recursive_properties_model_names(model_list)
  properties_model_names + properties_model_names.map do |model_name|
    child_model = model_from_model_list(model_list, model_name)
    child_model.recursive_properties_model_names(model_list) if child_model
  end.compact
end

#to_hObject



59
60
61
62
63
64
65
66
67
# File 'lib/swagger_yard/model.rb', line 59

def to_h
  raise "Model is missing @model tag" if id.nil?

  {
    "id" => id,
    "properties" => Hash[@properties.map {|property| [property.name, property.to_h]}],
    "required" => @properties.select(&:required?).map(&:name)
  }
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  !id.nil?
end