Module: TypesFromSerializers::DSL::ClassMethods

Defined in:
lib/types_from_serializers/dsl.rb

Instance Method Summary collapse

Instance Method Details

#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) || name
    define_singleton_method(:_serializer_model_name) { model }
    define_singleton_method(:_serializer_types_from) { types_from } if types_from
  end

  super(name)
end

#type(type, **options) ⇒ Object

Public: Shortcut for typing a serializer attribute.

It specifies the type for a serializer method that will be defined immediately after calling this method.



31
32
33
# File 'lib/types_from_serializers/dsl.rb', line 31

def type(type, **options)
  attribute type: type, **options
end