Class: SwaggerModel::SwaggerV2::Relationships

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Relationships

Returns a new instance of Relationships.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/swagger_model/relationships.rb', line 9

def initialize(hash)
  @relationships = []
  hash.keys.each do |key|
    relation_data = hash[key]['data']
    data = ''
    case relation_data.class.to_s
    when 'Hash'
      data = RelationData.new(relation_data)
    when 'Array'
      data = RelationDataArray.new(relation_data)
    end
    relationship = {
      'key' => key,
      'data' => data
    }
    @relationships.push(relationship)
  end
end

Instance Attribute Details

#relationshipsObject

Returns the value of attribute relationships.



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

def relationships
  @relationships
end

Instance Method Details

#to_swagger_hash(model, model_name) ⇒ Object



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
# File 'lib/swagger_model/relationships.rb', line 27

def to_swagger_hash(model, model_name)
  hash = {
    'type' => 'object'
  }
  properties = {}
  @relationships.each do |r|
    next if r['data'].nil? || r['data'] == ""
    parent_name = model_name + 'Relationships'
    swagger_hash = r['data'].to_swagger_hash(r['key'], parent_name)
    unless swagger_hash.nil?
      name = swagger_hash.keys.first
      properties[r['key']] = {
        '$ref' => "#/definitions/#{name}"
      }
      model[name] = swagger_hash[name]
    end
  end
  hash['properties'] = properties
  hash['required'] = properties.keys
  name = model_name + 'Relationships'
  model[name] = hash

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