Class: SpecForge::Normalizer::Transformers

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/spec_forge/normalizer/transformers.rb

Overview

Provides transformation functions for normalizer structure definitions

Transformers modify values during normalization, such as converting shorthand syntax into full structures or normalizing type definitions.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(method_name, value) ⇒ Object

Calls a transformer method with the given value

Parameters:

  • method_name (Symbol, String)

    The transformer method to call

  • value (Object)

    The value to transform

Returns:

  • (Object)

    The transformed value



22
23
24
# File 'lib/spec_forge/normalizer/transformers.rb', line 22

def self.call(method_name, value)
  instance.public_send(method_name, value)
end

Instance Method Details

#abs(value) ⇒ Numeric?

Returns the absolute value of a number

Parameters:

  • value (Numeric, nil)

    The value to convert

Returns:

  • (Numeric, nil)

    The absolute value, or nil if input is nil



163
164
165
# File 'lib/spec_forge/normalizer/transformers.rb', line 163

def abs(value)
  value&.abs
end

#normalize_callback(value) ⇒ Array<Hash>

Normalizes callback shorthand into full hash format

Converts string callback names into the full hash structure with a :name key. Hashes pass through unchanged. Arrays are processed recursively to normalize each element.

Parameters:

  • value (String, Hash, Array)

    Callback name, full definition, or array of callbacks

Returns:

  • (Array<Hash>)

    Normalized callback hash(es) with :name key



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/spec_forge/normalizer/transformers.rb', line 48

def normalize_callback(value)
  return if value.blank?

  case value
  when Hash
    [value]
  when Array
    value.map { |v| normalize_callback(v) }.flatten
  else
    [{name: value}]
  end
end

#normalize_includes(value) ⇒ Array<String>

Normalizes include values to an array of blueprint names

Parameters:

  • value (String, Array<String>)

    Include value(s)

Returns:

  • (Array<String>)

    Normalized blueprint names without extensions



33
34
35
# File 'lib/spec_forge/normalizer/transformers.rb', line 33

def normalize_includes(value)
  Array(value).map! { |name| name.delete_suffix(".yml").delete_suffix(".yaml") }
end

#normalize_schema(value) ⇒ Hash, Array

Normalizes a schema definition by converting type strings to classes

Recursively processes schema definitions, converting string type specifications into their Ruby class equivalents.

Parameters:

  • value (Array, Hash, String)

    The schema definition to normalize

Returns:

  • (Hash, Array)

    Normalized schema with type classes

Raises:

  • (ArgumentError)

    If value is nil



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/spec_forge/normalizer/transformers.rb', line 112

def normalize_schema(value)
  raise ArgumentError, "Schema cannot be nil" if value.nil?

  case value
  when Array
    value.each { |v| normalize_schema(v) }
  when Hash
    if (type = value[:type]) && type.is_a?(String)
      result = Type.from_string(type)

      value[:type] = result[:types]
      value[:optional] = result[:optional] unless value.key?(:optional)
    end

    # Handle explicit nullable: true (sugar for adding NilClass to type)
    if value.delete(:nullable)
      value[:type] ||= []
      value[:type] << NilClass unless value[:type].include?(NilClass)
    end

    # Default optional to false if not set
    value[:optional] ||= false

    if (structure = value[:structure])
      value[:structure] =
        case structure
        when Array
          structure.map { |v| normalize_schema(v) }
        when Hash
          structure.transform_values { |v| normalize_schema(v) }
        end
    end

    if (pattern = value[:pattern])
      value[:pattern] = normalize_schema(pattern)
    end

    value
  when String
    result = Type.from_string(value)
    {type: result[:types], optional: result[:optional]}
  end
end

#normalize_shape(value) ⇒ Hash

Normalizes a shape definition into a structured schema format

Converts shorthand shape syntax (arrays, hashes, type strings) into the full schema structure with :type, :pattern, and :structure keys.

Parameters:

  • value (Array, Hash, String)

    The shape definition to normalize

Returns:

  • (Hash)

    Normalized schema structure

Raises:

  • (ArgumentError)

    If value is nil



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/spec_forge/normalizer/transformers.rb', line 73

def normalize_shape(value)
  raise ArgumentError, "Shape cannot be nil" if value.nil?

  case value
  when Array
    shape = {type: [Array]}

    if value.size == 1
      shape[:pattern] = normalize_shape(value.first)
    elsif value.size > 1
      shape[:structure] = value.map { |i| normalize_shape(i) }
    else
      []
    end

    shape
  when Hash
    {
      type: [Hash],
      structure: value.transform_values { |v| normalize_shape(v) }
    }
  when String
    result = Type.from_string(value)
    {type: result[:types], optional: result[:optional]}
  end
end