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
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .from_tags(tags) ⇒ Object
- .from_yard_object(yard_object) ⇒ Object
- .from_yard_objects(yard_objects) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Model
constructor
A new instance of Model.
- #model_from_model_list(model_list, model_name) ⇒ Object
- #parse_tags(tags) ⇒ Object
- #properties_model_names ⇒ Object
- #recursive_properties_model_names(model_list) ⇒ Object
- #to_h ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ Model
Returns a new instance of Model.
23 24 25 |
# File 'lib/swagger_yard/model.rb', line 23 def initialize @properties = [] end |
Instance Attribute Details
#id ⇒ Object (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.() new.tap do |model| model.() 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) (yard_object.) 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 () .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_names ⇒ Object
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_h ⇒ Object
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
27 28 29 |
# File 'lib/swagger_yard/model.rb', line 27 def valid? !id.nil? end |