Class: SwaggerYard::Model
- Inherits:
-
Object
- Object
- SwaggerYard::Model
- 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
-
#discriminator ⇒ Object
readonly
Returns the value of attribute discriminator.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#inherits ⇒ Object
readonly
Returns the value of attribute inherits.
Class Method Summary collapse
Instance Method Summary collapse
- #inherits_references ⇒ Object
-
#initialize ⇒ Model
constructor
A new instance of Model.
- #parse_tags(tags) ⇒ Object
- #to_h ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ Model
Returns a new instance of Model.
19 20 21 22 |
# File 'lib/swagger_yard/model.rb', line 19 def initialize @properties = [] @inherits = [] end |
Instance Attribute Details
#discriminator ⇒ Object (readonly)
Returns the value of attribute discriminator.
7 8 9 |
# File 'lib/swagger_yard/model.rb', line 7 def discriminator @discriminator end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/swagger_yard/model.rb', line 7 def id @id end |
#inherits ⇒ Object (readonly)
Returns the value of attribute inherits.
7 8 9 |
# File 'lib/swagger_yard/model.rb', line 7 def inherits @inherits end |
Class Method Details
.from_yard_object(yard_object) ⇒ Object
9 10 11 12 13 |
# File 'lib/swagger_yard/model.rb', line 9 def self.from_yard_object(yard_object) new.tap do |model| model.(yard_object.) end end |
.mangle(name) ⇒ Object
15 16 17 |
# File 'lib/swagger_yard/model.rb', line 15 def self.mangle(name) name.gsub(/[^[:alnum:]_]+/, '_') end |
Instance Method Details
#inherits_references ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/swagger_yard/model.rb', line 47 def inherits_references @inherits.map do |name| { "$ref" => "#/definitions/#{name}" } end end |
#parse_tags(tags) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/swagger_yard/model.rb', line 28 def () .each do |tag| case tag.tag_name when "model" @id = Model.mangle(tag.text) when "property" @properties << Property.from_tag(tag) when "discriminator" prop = Property.from_tag(tag) @properties << prop @discriminator ||= prop.name when "inherits" @inherits << Model.mangle(tag.text) end end self end |
#to_h ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/swagger_yard/model.rb', line 55 def to_h h = { "type" => "object", "properties" => Hash[@properties.map {|p| [p.name, p.to_h]}] } h["required"] = @properties.select(&:required?).map(&:name) if @properties.detect(&:required?) h["discriminator"] = @discriminator if @discriminator # Polymorphism h = { "allOf" => inherits_references + [h] } unless @inherits.empty? # Description h["description"] = @description if @description h end |
#valid? ⇒ Boolean
24 25 26 |
# File 'lib/swagger_yard/model.rb', line 24 def valid? !id.nil? end |