Sha256: 4e5240102f5a8815885c8e5b7a7d73c8985a8e44fd8860c6bfaa871d0ac0b6ba

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

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

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

    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

    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
remocon-0.5.1 lib/remocon/sorter/parameter_sorter.rb
remocon-0.5.0 lib/remocon/sorter/parameter_sorter.rb
remocon-0.4.4 lib/remocon/sorter/parameter_sorter.rb
remocon-0.4.3 lib/remocon/sorter/parameter_sorter.rb
remocon-0.4.2 lib/remocon/sorter/parameter_sorter.rb