Module: Natsuzora::DataNormalizable

Included in:
Payload
Defined in:
lib/natsuzora/data_normalizable.rb

Overview

Mixin that gives the including class a ‘normalize_data(data)` method.

Instance Method Summary collapse

Instance Method Details

#normalize_data(data) ⇒ Object

performs only pure transformations:

  • Symbol Hash keys are converted to Strings (recursively).

  • Whole-number Float values are converted to Integers.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/natsuzora/data_normalizable.rb', line 9

def normalize_data(data)
  case data
  when Hash
    data.transform_keys(&:to_s).transform_values { |v| normalize_data(v) }
  when Array
    data.map { |v| normalize_data(v) }
  when Float
    normalize_float(data)
  else
    data
  end
end