Class: ApiSchema::SerializerDefinition

Inherits:
Object
  • Object
show all
Includes:
Swagger::Blocks::ClassMethods
Defined in:
lib/api_schema/serializer_definition.rb

Defined Under Namespace

Classes: PriorReference

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, type, api_version, name = nil, parent_id = nil) ⇒ SerializerDefinition

Returns a new instance of SerializerDefinition.



10
11
12
13
14
15
16
17
18
19
# File 'lib/api_schema/serializer_definition.rb', line 10

def initialize(id, type, api_version, name=nil, parent_id = nil)
  @id = id
  @type = type
  @name = name || id
  @api_version = api_version
  @parent = api_version.serializers.detect { |s| s.id == parent_id } if parent_id
  @fields = parent&.fields || []
  @prior_references = parent&.prior_references || []
  @references = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(type, *args, &block) ⇒ Object



45
46
47
48
# File 'lib/api_schema/serializer_definition.rb', line 45

def method_missing(type, *args, &block)
  options = args[1] || {}
  @fields << Field.new(type, args[0], options)
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



7
8
9
# File 'lib/api_schema/serializer_definition.rb', line 7

def api_version
  @api_version
end

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/api_schema/serializer_definition.rb', line 8

def description
  @description
end

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/api_schema/serializer_definition.rb', line 7

def fields
  @fields
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/api_schema/serializer_definition.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/api_schema/serializer_definition.rb', line 8

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/api_schema/serializer_definition.rb', line 7

def parent
  @parent
end

#prior_referencesObject

Returns the value of attribute prior_references.



8
9
10
# File 'lib/api_schema/serializer_definition.rb', line 8

def prior_references
  @prior_references
end

#referencesObject (readonly)

Returns the value of attribute references.



7
8
9
# File 'lib/api_schema/serializer_definition.rb', line 7

def references
  @references
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/api_schema/serializer_definition.rb', line 8

def type
  @type
end

Instance Method Details

#buildObject



29
30
31
32
33
# File 'lib/api_schema/serializer_definition.rb', line 29

def build
  build_references
  sd = self
  swagger_schema(id) { schema_for(sd) }
end

#build_referencesObject



35
36
37
38
39
40
41
42
43
# File 'lib/api_schema/serializer_definition.rb', line 35

def build_references
  @prior_references.each do |pr|
    reference = api_version.serializers.detect { |s| s.id == pr.id }
    reference.type = pr.type
    reference.description = pr.desc
    reference.name = reference.name.to_s.pluralize if reference.type == :array
    @references << reference
  end
end

#reference(refernce_id, type: :object, desc: nil) ⇒ Object



25
26
27
# File 'lib/api_schema/serializer_definition.rb', line 25

def reference(refernce_id, type: :object, desc: nil)
  @prior_references << PriorReference.new(refernce_id, type, desc)
end

#required_fieldsObject



21
22
23
# File 'lib/api_schema/serializer_definition.rb', line 21

def required_fields
  fields.select { |f| f.required? }.map(&:name) + references.map(&:name)
end