Class: Swagui::JsonSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/swagui/json_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_hash, name) ⇒ JsonSchema

Returns a new instance of JsonSchema.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/swagui/json_schema.rb', line 6

def initialize(schema_hash, name)
  @name = name
  @schema_hash = schema_hash
  @nested_objects = []

  @schema_hash['properties'].each do |pname, pattributes|
    if pattributes['type'] == 'object'
      nested_object_name = "#{@name}-#{pname}"
      @schema_hash['properties'][pname] = {'$ref' => nested_object_name }
      @nested_objects << JsonSchema.new(pattributes, nested_object_name)
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/swagui/json_schema.rb', line 4

def name
  @name
end

Instance Method Details

#all_objectsObject



33
34
35
# File 'lib/swagui/json_schema.rb', line 33

def all_objects
  ([self] + @nested_objects.map {|x| x.all_objects}).flatten
end

#modelsObject



24
25
26
27
28
29
30
31
# File 'lib/swagui/json_schema.rb', line 24

def models
  all_objects.map do |schema|
    {
      'id' => schema.name,
      'properties' => schema.properties
    }
  end
end

#propertiesObject



20
21
22
# File 'lib/swagui/json_schema.rb', line 20

def properties
  @schema_hash['properties']
end