Module: TypesFromSerializers

Defined in:
lib/types_from_serializers/generator.rb,
lib/types_from_serializers/version.rb

Overview

Public: Automatically generates TypeScript interfaces for Ruby serializers.

Defined Under Namespace

Modules: SerializerRefinements Classes: Changes, Config, FieldMetadata, Railtie, SerializerMetadata

Constant Summary collapse

VERSION =

Public: This library adheres to semantic versioning.

"0.1.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.force_generationObject (readonly)

Returns the value of attribute force_generation.



194
195
196
# File 'lib/types_from_serializers/generator.rb', line 194

def force_generation
  @force_generation
end

Class Method Details

.configObject

Public: Configuration of the code generator.



197
198
199
200
201
# File 'lib/types_from_serializers/generator.rb', line 197

def config
  (@config ||= default_config(root)).tap do |config|
    yield(config) if block_given?
  end
end

.generate(force: ) ⇒ Object

Public: Generates code for all serializers in the app.



204
205
206
207
208
209
210
211
212
# File 'lib/types_from_serializers/generator.rb', line 204

def generate(force: ENV["SERIALIZER_TYPES_FORCE"])
  @force_generation = force
  config.output_dir.rmtree if force && config.output_dir.exist?
  generate_index_file

  loaded_serializers.each do |serializer|
    generate_interface_for(serializer)
  end
end

.generate_changedObject



214
215
216
217
218
219
220
221
# File 'lib/types_from_serializers/generator.rb', line 214

def generate_changed
  if changes.updated?
    config.output_dir.rmtree if changes.any_removed?
    load_serializers(changes.modified_files)
    generate
    changes.clear
  end
end

.generate_index_fileObject

Internal: Allows to import all serializer types from a single file.



235
236
237
238
239
240
241
# File 'lib/types_from_serializers/generator.rb', line 235

def generate_index_file
  cache_key = all_serializer_files.map { |file| file.delete_prefix(root.to_s) }.join
  write_if_changed(filename: "index", cache_key: cache_key) {
    load_serializers(all_serializer_files)
    serializers_index_content(loaded_serializers)
  }
end

.generate_interface_for(serializer) ⇒ Object

Internal: Defines a TypeScript interface for the serializer.



224
225
226
227
228
229
230
231
232
# File 'lib/types_from_serializers/generator.rb', line 224

def generate_interface_for(serializer)
   = serializer.
  filename = serializer.typescript_interface_basename

  write_if_changed(filename: filename, cache_key: .inspect) {
    serializer.typescript_infer_types()
    serializer_interface_content(serializer, )
  }
end

.skip_serializer?(name) ⇒ Boolean

Internal: Checks if it should avoid generating an interface.

Returns:

  • (Boolean)


244
245
246
# File 'lib/types_from_serializers/generator.rb', line 244

def skip_serializer?(name)
  name.include?("BaseSerializer") || name.in?(config.base_serializers)
end

.track_changesObject

Internal: Returns an object compatible with FileUpdateChecker.



249
250
251
# File 'lib/types_from_serializers/generator.rb', line 249

def track_changes
  changes
end