Class: Typelizer::Config

Inherits:
Struct
  • Object
show all
Defined in:
lib/typelizer/config.rb,
lib/typelizer/config.rb

Overview

Immutable configuration object for a single writer

Use .build to construct from defaults, and #with_overrides to copy with overrides.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#associations_strategyObject

Returns the value of attribute associations_strategy

Returns:

  • (Object)

    the current value of associations_strategy



58
59
60
# File 'lib/typelizer/config.rb', line 58

def associations_strategy
  @associations_strategy
end

#commentsObject

Returns the value of attribute comments

Returns:

  • (Object)

    the current value of comments



58
59
60
# File 'lib/typelizer/config.rb', line 58

def comments
  @comments
end

#filename_mapperObject

Returns the value of attribute filename_mapper

Returns:

  • (Object)

    the current value of filename_mapper



58
59
60
# File 'lib/typelizer/config.rb', line 58

def filename_mapper
  @filename_mapper
end

#imports_sort_orderObject

Returns the value of attribute imports_sort_order

Returns:

  • (Object)

    the current value of imports_sort_order



58
59
60
# File 'lib/typelizer/config.rb', line 58

def imports_sort_order
  @imports_sort_order
end

#inheritance_strategyObject

Returns the value of attribute inheritance_strategy

Returns:

  • (Object)

    the current value of inheritance_strategy



58
59
60
# File 'lib/typelizer/config.rb', line 58

def inheritance_strategy
  @inheritance_strategy
end

#model_pluginObject

Returns the value of attribute model_plugin

Returns:

  • (Object)

    the current value of model_plugin



58
59
60
# File 'lib/typelizer/config.rb', line 58

def model_plugin
  @model_plugin
end

#null_strategyObject

Returns the value of attribute null_strategy

Returns:

  • (Object)

    the current value of null_strategy



58
59
60
# File 'lib/typelizer/config.rb', line 58

def null_strategy
  @null_strategy
end

#output_dirObject

Returns the value of attribute output_dir

Returns:

  • (Object)

    the current value of output_dir



58
59
60
# File 'lib/typelizer/config.rb', line 58

def output_dir
  @output_dir
end

#plugin_configsObject

Returns the value of attribute plugin_configs

Returns:

  • (Object)

    the current value of plugin_configs



58
59
60
# File 'lib/typelizer/config.rb', line 58

def plugin_configs
  @plugin_configs
end

#prefer_double_quotesObject

Returns the value of attribute prefer_double_quotes

Returns:

  • (Object)

    the current value of prefer_double_quotes



58
59
60
# File 'lib/typelizer/config.rb', line 58

def prefer_double_quotes
  @prefer_double_quotes
end

#properties_sort_orderObject

Returns the value of attribute properties_sort_order

Returns:

  • (Object)

    the current value of properties_sort_order



58
59
60
# File 'lib/typelizer/config.rb', line 58

def properties_sort_order
  @properties_sort_order
end

#properties_transformerObject

Returns the value of attribute properties_transformer

Returns:

  • (Object)

    the current value of properties_transformer



58
59
60
# File 'lib/typelizer/config.rb', line 58

def properties_transformer
  @properties_transformer
end

#reject_classObject

Returns the value of attribute reject_class

Returns:

  • (Object)

    the current value of reject_class



58
59
60
# File 'lib/typelizer/config.rb', line 58

def reject_class
  @reject_class
end

#serializer_model_mapperObject

Returns the value of attribute serializer_model_mapper

Returns:

  • (Object)

    the current value of serializer_model_mapper



58
59
60
# File 'lib/typelizer/config.rb', line 58

def serializer_model_mapper
  @serializer_model_mapper
end

#serializer_name_mapperObject

Returns the value of attribute serializer_name_mapper

Returns:

  • (Object)

    the current value of serializer_name_mapper



58
59
60
# File 'lib/typelizer/config.rb', line 58

def serializer_name_mapper
  @serializer_name_mapper
end

#serializer_pluginObject

Returns the value of attribute serializer_plugin

Returns:

  • (Object)

    the current value of serializer_plugin



58
59
60
# File 'lib/typelizer/config.rb', line 58

def serializer_plugin
  @serializer_plugin
end

#type_mappingObject

Returns the value of attribute type_mapping

Returns:

  • (Object)

    the current value of type_mapping



58
59
60
# File 'lib/typelizer/config.rb', line 58

def type_mapping
  @type_mapping
end

#types_globalObject

Returns the value of attribute types_global

Returns:

  • (Object)

    the current value of types_global



58
59
60
# File 'lib/typelizer/config.rb', line 58

def types_global
  @types_global
end

#types_import_pathObject

Returns the value of attribute types_import_path

Returns:

  • (Object)

    the current value of types_import_path



58
59
60
# File 'lib/typelizer/config.rb', line 58

def types_import_path
  @types_import_path
end

#verbatim_module_syntaxObject

Returns the value of attribute verbatim_module_syntax

Returns:

  • (Object)

    the current value of verbatim_module_syntax



58
59
60
# File 'lib/typelizer/config.rb', line 58

def verbatim_module_syntax
  @verbatim_module_syntax
end

Class Method Details

.build(**overrides) ⇒ Object



129
130
131
# File 'lib/typelizer/config.rb', line 129

def self.build(**overrides)
  new(**defaults.merge(overrides))
end

.default_output_dirObject



133
134
135
136
137
138
# File 'lib/typelizer/config.rb', line 133

def self.default_output_dir
  root_path = defined?(Rails) ? Rails.root : Pathname.pwd
  js_root = defined?(ViteRuby) ? ViteRuby.config.source_code_dir : "app/javascript"

  root_path.join(js_root, "types/serializers")
end

.defaultsObject

Returns library defaults (built-in) for building a Config. This method creates a fresh Hash each time to avoid sharing mutable state across builds



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/typelizer/config.rb', line 89

def self.defaults
  {
    serializer_name_mapper: lambda do |serializer|
      name = serializer.name.to_s

      return name if name.empty?

      # remove only the end of the line
      name.sub(/(Serializer|Resource)\z/, "")
    end,

    filename_mapper: nil,

    serializer_model_mapper: lambda do |serializer|
      base_class = serializer_name_mapper.call(serializer)
      Object.const_get(base_class) if Object.const_defined?(base_class)
    end,

    model_plugin: ModelPlugins::Auto,
    serializer_plugin: SerializerPlugins::Auto,
    plugin_configs: {}.freeze,
    type_mapping: TYPE_MAPPING,
    null_strategy: :nullable,
    inheritance_strategy: :none,
    associations_strategy: :database,
    reject_class: ->(serializer:) { false },
    comments: false,
    prefer_double_quotes: false,

    output_dir: -> { default_output_dir },

    types_import_path: "@/types",
    types_global: DEFAULT_TYPES_GLOBAL,
    properties_transformer: nil,
    properties_sort_order: :none,
    imports_sort_order: :none,
    verbatim_module_syntax: false
  }
end

Instance Method Details

#with_overrides(**overrides) ⇒ Object



140
141
142
143
144
145
# File 'lib/typelizer/config.rb', line 140

def with_overrides(**overrides)
  props = to_h
  props.merge!(overrides) unless overrides.empty?

  self.class.new(**props)
end