Class: SwaggerModel::SwaggerV2::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_model/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Model

Returns a new instance of Model.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/swagger_model/model.rb', line 9

def initialize(hash)
  @id = hash['id']
  @type = hash['type']
  @model_name = ActiveSupport::Inflector.classify(@type.gsub('-', '_'))
  if !hash['attributes'].nil?
    @attributes = Attributes.new(hash['attributes'], @model_name)
  end
  if !hash['relationships'].nil?
    @relationships = Relationships.new(hash['relationships'])
  end
end

Instance Attribute Details

#relationshipsObject

Returns the value of attribute relationships.



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

def relationships
  @relationships
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#to_swagger_hash(model) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/swagger_model/model.rb', line 21

def to_swagger_hash(model)
  model[@model_name] = {}
  aModel = model[@model_name]
  hash = {
    'type' => 'object',
    'properties' => {
      'id' => {
        'type' => 'string',
        'example' => @id
      },
      'type' => {
        'type' => 'string',
        'example' => @type
      }
    },
    'required' => [
      'id',
      'type'
    ]
  }
  unless @attributes.nil?
    hash['properties']['attributes'] = @attributes.to_swagger_hash(aModel)
    hash['required'].push('attributes')
  end
  unless @relationships.nil?
    hash['properties']['relationships'] = @relationships.to_swagger_hash(aModel, @model_name)
    hash['required'].push('relationships')
  end
  aModel[@model_name] = hash

  {
    '$ref' => "#/definitions/#{@model_name}"
  }
end