Module: CopyTunerClient::DottedHash
- Defined in:
- lib/copy_tuner_client/dotted_hash.rb
Class Method Summary collapse
- .conflict_keys(dotted_hash) ⇒ Object
- .convert_value_type(key, value) ⇒ Object
- .to_h(dotted_hash) ⇒ Object
Class Method Details
.conflict_keys(dotted_hash) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/copy_tuner_client/dotted_hash.rb', line 14 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 |
.convert_value_type(key, value) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/copy_tuner_client/dotted_hash.rb', line 34 def convert_value_type(key, value) return value unless value.is_a?(String) # Rails i18n標準で数値型として扱われるキー if key.end_with?('.precision') value.to_i # Rails i18n標準で真偽値として扱われるキー elsif key.end_with?('.significant', '.strip_insignificant_zeros') value == 'true' else value end end |
.to_h(dotted_hash) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# 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| # Rails i18n標準との互換性のため、特定のキーを適切な型に変換 converted_value = convert_value_type(key, value) _hash = key.split('.').reverse.inject(converted_value) { |memo, _key| { _key => memo } } hash.deep_merge!(_hash) end hash end |