Module: Remocon::ParameterSorter

Included in:
Command::Pull
Defined in:
lib/remocon/sorter/parameter_sorter.rb

Constant Summary collapse

PARAMETER_KEYS =
%w(description value file normalizer conditions options).freeze

Instance Method Summary collapse

Instance Method Details

#comparator_of_parameter_keys(left, right) ⇒ Object



7
8
9
# File 'lib/remocon/sorter/parameter_sorter.rb', line 7

def comparator_of_parameter_keys(left, right)
  PARAMETER_KEYS.index(left) <=> PARAMETER_KEYS.index(right)
end

#sort_conditions_of_parameters(conditions) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/remocon/sorter/parameter_sorter.rb', line 30

def sort_conditions_of_parameters(conditions)
  conditions.with_indifferent_access.to_a.each_with_object({}) do |(k, v), acc|
    acc[k] = v.stringify_keys
      .sort { |(a, _), (b, _)| comparator_of_parameter_keys(a, b) }
      .each_with_object({}) do |(inside_key, _), inside_acc|
      inside_acc[inside_key] = v[inside_key]
    end
  end
end

#sort_parameters(parameters) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/remocon/sorter/parameter_sorter.rb', line 11

def sort_parameters(parameters)
  params = parameters.with_indifferent_access

  params.keys.sort.each_with_object({}) do |key, acc|
    param = params[key]

    acc[key] = param
      .stringify_keys
      .sort { |(a, _), (b, _)| comparator_of_parameter_keys(a, b) }
      .each_with_object({}) do |(inside_key, _), inside_acc|
      if inside_key == "conditions"
        inside_acc[inside_key] = sort_conditions_of_parameters(param[inside_key])
      else
        inside_acc[inside_key] = param[inside_key]
      end
    end
  end.with_indifferent_access
end