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
-
#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(serializers) ⇒ Object
- #build_references(serializers) ⇒ Object
-
#initialize(id, type, serializers, 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, serializers, name = nil, parent_id = nil) ⇒ SerializerDefinition
Returns a new instance of SerializerDefinition.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/api_schema/serializer_definition.rb', line 10 def initialize(id, type, serializers, name=nil, parent_id = nil) @id = id @type = type @name = name || id @parent = serializers[parent_id] @fields = parent&.fields || [] @prior_references = parent&.prior_references || [] @references = [] serializers[id] = self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(type, *args, &block) ⇒ Object
47 48 49 50 |
# File 'lib/api_schema/serializer_definition.rb', line 47 def method_missing(type, *args, &block) = args[1] || {} @fields << Field.new(type, args[0], ) end |
Instance Attribute Details
#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(serializers) ⇒ Object
30 31 32 33 34 |
# File 'lib/api_schema/serializer_definition.rb', line 30 def build(serializers) build_references(serializers) sd = self swagger_schema(id) { schema_for(sd) } end |
#build_references(serializers) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/api_schema/serializer_definition.rb', line 36 def build_references(serializers) @prior_references.each do |pr| raise "Model #{pr.id} is not defined" unless serializers[pr.id] reference = serializers[pr.id].clone 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
26 27 28 |
# File 'lib/api_schema/serializer_definition.rb', line 26 def reference(refernce_id, type: :object, desc: nil) @prior_references << PriorReference.new(refernce_id, type, desc) end |
#required_fields ⇒ Object
22 23 24 |
# File 'lib/api_schema/serializer_definition.rb', line 22 def required_fields fields.select { |f| f.required? }.map(&:name) + references.map(&:name) end |