Module: Typelizer::DSL::ClassMethods

Defined in:
lib/typelizer/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyless_typeObject

Returns the value of attribute keyless_type.



55
56
57
# File 'lib/typelizer/dsl.rb', line 55

def keyless_type
  @keyless_type
end

Instance Method Details

#_own_typelizer_multi_attributesObject

Returns own Set (initializing if needed) for writing



68
69
70
# File 'lib/typelizer/dsl.rb', line 68

def _own_typelizer_multi_attributes
  @_typelizer_multi_attributes ||= Set.new
end

#_typelizer_multi_attributesObject

Returns union of own + ancestors’ multi attributes



58
59
60
61
62
63
64
65
# File 'lib/typelizer/dsl.rb', line 58

def _typelizer_multi_attributes
  result = @_typelizer_multi_attributes || Set.new
  if superclass.respond_to?(:_typelizer_multi_attributes)
    superclass._typelizer_multi_attributes | result
  else
    result
  end
end

#store_type(attribute_name, name, options) ⇒ Object



76
77
78
79
80
# File 'lib/typelizer/dsl.rb', line 76

def store_type(attribute_name, name, options)
  ensure_type_store(attribute_name)
  instance_variable_get("@#{attribute_name}")[name.to_sym] ||= {}
  instance_variable_get("@#{attribute_name}")[name.to_sym].merge!(options)
end

#typelize(type = nil, type_params = {}, **attributes) ⇒ Object

save association of serializer attributes to type can be invoked multiple times



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/typelizer/dsl.rb', line 43

def typelize(type = nil, type_params = {}, **attributes)
  if type
    # Parse type shortcuts like 'string?', 'string[]'
    parsed = TypeParser.parse(type)
    merged_params = parsed.merge(type_params).merge(attributes)
    actual_type = merged_params.delete(:type)
    @keyless_type = [actual_type, merged_params]
  else
    assign_type_information(:_typelizer_attributes, attributes)
  end
end

#typelize_from(model) ⇒ Object

save association of serializer to model



35
36
37
38
39
# File 'lib/typelizer/dsl.rb', line 35

def typelize_from(model)
  return unless Typelizer.enabled?

  define_singleton_method(:_typelizer_model_name) { model }
end

#typelize_meta(**attributes) ⇒ Object



72
73
74
# File 'lib/typelizer/dsl.rb', line 72

def typelize_meta(**attributes)
  assign_type_information(:_typelizer_meta_attributes, attributes)
end

#typelizer_config(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/typelizer/dsl.rb', line 22

def typelizer_config(&block)
  # Lazily initializes and memoizes the hash for local overrides at the class level.
  # This ensures that all subsequent DSL calls for this specific serializer class
  # modify the same single hash, allowing settings to be accumulated
  @serializer_overrides ||= {}

  @config_layer ||= SerializerConfigLayer.new(@serializer_overrides)
  @config_layer.instance_eval(&block) if block

  @config_layer
end