Module: Remocon::ParameterSorter

Included in:
InterpreterHelper
Defined in:
lib/remocon/sorter/parameter_sorter.rb

Constant Summary collapse

PARAMETER_KEYS =
i[description value file normalizer conditions options]

Instance Method Summary collapse

Instance Method Details

#sort_parameters(parameters) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/remocon/sorter/parameter_sorter.rb', line 7

def sort_parameters(parameters)
  arr = parameters.sort.map do |k, v|
    hash_arr = v.sort { |(a, _), (b, _)| PARAMETER_KEYS.index(a) <=> PARAMETER_KEYS.index(b) }
                .map do |k, v|
      {
        k => k.to_sym == :conditions ? sort_parameters(v) : v
      }
    end

    {
      k => hash_arr.each_with_object({}) { |hash, acc| acc.merge!(hash) }
    }
  end

  arr.each_with_object({}) { |hash, acc| acc.merge!(hash) }.with_indifferent_access
end