Module: CopyTunerClient::DottedHash

Defined in:
lib/copy_tuner_client/dotted_hash.rb

Overview

a.b.c 形式のドット区切りキーを持つ Hash とネストした Hash を相互変換する

Class Method Summary collapse

Class Method Details

.conflict_keys(dotted_hash) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/copy_tuner_client/dotted_hash.rb', line 13

def conflict_keys(dotted_hash)
  all_keys = dotted_hash.keys.sort
  results = {}

  all_keys.each_with_index do |key, index|
    prefix = "#{key}."
    conflict_keys =
      ((index + 1)..Float::INFINITY)
      .take_while { |i| all_keys[i]&.start_with?(prefix) }
      .map { |i| all_keys[i] }

    results[key] = conflict_keys if conflict_keys.present?
  end

  results
end

.to_h(dotted_hash) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/copy_tuner_client/dotted_hash.rb', line 4

def to_h(dotted_hash)
  hash = {}
  dotted_hash.to_h.transform_keys(&:to_s).sort.each do |key, value|
    _hash = key.split('.').reverse.inject(value) { |memo, _key| { _key => memo } }
    hash.deep_merge!(_hash)
  end
  hash
end