Module: TypesFromSerializer::DSL::ClassMethods

Defined in:
lib/types_from_serializers/dsl.rb

Instance Method Summary collapse

Instance Method Details

#_serializer_model_nameObject

Internal: The name of the model that will be serialized by this serializer, used to infer field types from the SQL columns.



73
74
75
76
77
78
# File 'lib/types_from_serializers/dsl.rb', line 73

def _serializer_model_name
  unless defined?(@_serializer_model_name)
    @_serializer_model_name = superclass.try(:_serializer_model_name)
  end
  @_serializer_model_name
end

#_serializer_types_fromObject

Internal: The TypeScript interface that will be used by default to infer the serializer field types when not explicitly provided.



82
83
84
85
86
87
# File 'lib/types_from_serializers/dsl.rb', line 82

def _serializer_types_from
  unless defined?(@_serializer_types_from)
    @_serializer_types_from = superclass.try(:_serializer_types_from)
  end
  @_serializer_types_from
end

#_typed_attributesObject

Internal: Contains type information for serializer attributes.



64
65
66
67
68
69
# File 'lib/types_from_serializers/dsl.rb', line 64

def _typed_attributes
  unless defined?(@_typed_attributes)
    @_typed_attributes = superclass.try(:_typed_attributes)&.dup || {}
  end
  @_typed_attributes
end

#method_added(name) ⇒ Object

Internal: Intercept a method definition, tying a type that was previously specified to the name of the attribute.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/types_from_serializers/dsl.rb', line 47

def method_added(name)
  super(name)
  if @_current_attribute_type
    serializer_attributes name

    # NOTE: Avoid taking memory for type information that won't be used.
    if Rails.env.development?
      _typed_attributes[name.to_s] = @_current_attribute_type
    end

    @_current_attribute_type = nil
  end
end

#object_as(name, model: nil, types_from: nil) ⇒ Object

Override: Capture the name of the model related to the serializer.

name - An alias for the internal object in the serializer. model - The name of an ActiveRecord model to infer types from the schema. types_from - The name of a TypeScript interface to infer types from.



16
17
18
19
20
21
22
23
24
25
# File 'lib/types_from_serializers/dsl.rb', line 16

def object_as(name, model: nil, types_from: nil)
  # NOTE: Avoid taking memory for type information that won't be used.
  if Rails.env.development?
    model ||= name.is_a?(Symbol) ? name : try(:_serializer_model_name)
    @_serializer_model_name = model || name
    @_serializer_types_from = types_from if types_from
  end

  super(name)
end

#type(type = :unknown, optional: false) ⇒ Object

Public: Allows to specify the type for a serializer method that will be defined immediately after calling this method.



41
42
43
# File 'lib/types_from_serializers/dsl.rb', line 41

def type(type = :unknown, optional: false)
  @_current_attribute_type = {type: type, optional: optional}
end

#typed_attributes(attrs) ⇒ Object

Public: Like ‘attributes`, but providing type information for each field.



28
29
30
31
32
33
34
35
36
37
# File 'lib/types_from_serializers/dsl.rb', line 28

def typed_attributes(attrs)
  attributes(*attrs.keys)

  # NOTE: Avoid taking memory for type information that won't be used.
  if Rails.env.development?
    _typed_attributes.update(attrs.map { |key, type|
      [key.to_s, type.is_a?(Hash) ? type : {type: type}]
    }.to_h)
  end
end