Class: ApiSchema::SerializerDefinition
- Inherits:
-
Object
- Object
- ApiSchema::SerializerDefinition
- Includes:
- Swagger::Blocks::ClassMethods
- Defined in:
- lib/api_schema/serializer_definition.rb
Defined Under Namespace
Classes: PriorReference
Instance Attribute Summary collapse
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#description ⇒ Object
Returns the value of attribute description.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#prior_references ⇒ Object
Returns the value of attribute prior_references.
-
#references ⇒ Object
readonly
Returns the value of attribute references.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #build ⇒ Object
- #build_references ⇒ Object
-
#initialize(id, type, api_version, name = nil, parent_id = nil) ⇒ SerializerDefinition
constructor
A new instance of SerializerDefinition.
- #method_missing(type, *args, &block) ⇒ Object
- #reference(refernce_id, type: :object, desc: nil) ⇒ Object
- #required_fields ⇒ Object
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) = args[1] || {} @fields << Field.new(type, args[0], ) end |
Instance Attribute Details
#api_version ⇒ Object (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 |
#description ⇒ Object
Returns the value of attribute description.
8 9 10 |
# File 'lib/api_schema/serializer_definition.rb', line 8 def description @description end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
7 8 9 |
# File 'lib/api_schema/serializer_definition.rb', line 7 def fields @fields end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/api_schema/serializer_definition.rb', line 7 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/api_schema/serializer_definition.rb', line 8 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
7 8 9 |
# File 'lib/api_schema/serializer_definition.rb', line 7 def parent @parent end |
#prior_references ⇒ Object
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 |
#references ⇒ Object (readonly)
Returns the value of attribute references.
7 8 9 |
# File 'lib/api_schema/serializer_definition.rb', line 7 def references @references end |
#type ⇒ Object
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
#build ⇒ Object
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_references ⇒ Object
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_fields ⇒ Object
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 |