Module: CopyTunerClient::DottedHash

Defined in:
lib/copy_tuner_client/dotted_hash.rb

Class Method Summary collapse

Class Method Details

.conflict_keys(dotted_hash) ⇒ Object



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

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] }

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

  results
end

.to_h(dotted_hash) ⇒ Object



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

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