Sha256: f59fc9af660291783a3a64d5575d10a7d56bd74b6ad1cc68d22019f6a4dca5cb

Contents?: true

Size: 1.54 KB

Versions: 39

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module RSpec
  module Terraform
    module Configuration
      class Merger
        def merge(left, right)
          merge_top_level(left, right)
            .merge(merge_accumulating_maps(left, right))
            .merge(merge_accumulating_lists(left, right))
        end

        private

        def merge_top_level(left, right)
          left.merge(right)
        end

        def merge_accumulating_maps(left, right)
          %i[vars backend_config].inject({}) do |merged, parameter|
            merge_accumulating_map(parameter, merged, left, right)
          end
        end

        def merge_accumulating_map(parameter, accumulator, left, right)
          left_value = left[parameter] || {}
          right_value = right[parameter] || {}
          merged_value = left_value.merge(right_value)

          return accumulator if merged_value == {}

          accumulator.merge(parameter => merged_value)
        end

        def merge_accumulating_lists(left, right)
          %i[var_files targets replaces plugin_dirs platforms]
            .inject({}) do |merged, parameter|
            merge_accumulating_list(parameter, merged, left, right)
          end
        end

        def merge_accumulating_list(parameter, accumulator, left, right)
          left_value = left[parameter] || []
          right_value = right[parameter] || []
          merged_value = left_value + right_value

          return accumulator if merged_value == []

          accumulator.merge(parameter => merged_value)
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
rspec-terraform-0.5.0.pre.9 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.5.0.pre.8 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.5.0.pre.7 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.5.0.pre.6 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.5.0.pre.5 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.5.0.pre.4 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.5.0.pre.3 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.5.0.pre.2 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.5.0.pre.1 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.4.0 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.3.0.pre.1 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.2.0 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.1.0.pre.49 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.1.0.pre.48 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.1.0.pre.47 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.1.0.pre.46 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.1.0.pre.45 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.1.0.pre.44 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.1.0.pre.43 lib/rspec/terraform/configuration/merger.rb
rspec-terraform-0.1.0.pre.42 lib/rspec/terraform/configuration/merger.rb