Sha256: afbebc2d0a0a9f6da7d4fe7759443806e05bfe69aa6916b98a8d21b335742db7

Contents?: true

Size: 792 Bytes

Versions: 6

Compression:

Stored size: 792 Bytes

Contents

# frozen_string_literal: true

require 'active_support/core_ext/hash/deep_merge'

module BlueprintConfig
  module Backend
    class Base
      def nest_hash(hash, delimiter = '_')
        hash.each_with_object({}) do |(key, value), results|
          steps = key.split(delimiter).reverse
          nested = steps.reduce(value) { |value, key| { key.to_sym => value } }

          results.deep_merge!(nested) do |_key, a, b|
            if a.is_a?(Hash) && b.is_a?(String)
              a.deep_merge(nil => b)
            elsif b.is_a?(Hash) && a.is_a?(String)
              b.deep_merge(nil => a)
            else
              b
            end
          end
        end
      end

      def source
        self.class.name
      end

      def fresh?
        true
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
blueprint_config-1.4.0 lib/blueprint_config/backend/base.rb
blueprint_config-1.3.1 lib/blueprint_config/backend/base.rb
blueprint_config-1.3.0 lib/blueprint_config/backend/base.rb
blueprint_config-1.2.0 lib/blueprint_config/backend/base.rb
blueprint_config-1.1.0 lib/blueprint_config/backend/base.rb
blueprint_config-1.0.0 lib/blueprint_config/backend/base.rb