Sha256: 607742289faa5b0370593ef919dcc5cccbfca798d0b6fa3cfeed317b14986325

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Pups
  class MergeCommand < Pups::Command
    attr_reader :filename, :merge_hash

    def self.from_str(command, params)
      new(command, params)
    end

    def self.parse_command(command)
      split = command.split(" ")
      unless split[-1][0] == "$"
        raise ArgumentError, "Invalid merge command #{command}"
      end

      [split[0..-2].join(" "), split[-1][1..-1]]
    end

    def initialize(command, params)
      @params = params

      filename, target_param = Pups::MergeCommand.parse_command(command)
      @filename = interpolate_params(filename)
      @merge_hash = params[target_param]
    end

    def run
      merged = self.class.deep_merge(YAML.load_file(@filename), @merge_hash)
      File.open(@filename, "w") { |f| f.write(merged.to_yaml) }
      Pups.log.info("Merge: #{@filename} with: \n#{@merge_hash.inspect}")
    end

    def self.deep_merge(first, second, *args)
      args ||= []
      merge_arrays = args.include? :merge_arrays

      merger =
        proc do |_key, v1, v2|
          if v1.is_a?(Hash) && v2.is_a?(Hash)
            v1.merge(v2, &merger)
          elsif v1.is_a?(Array) && v2.is_a?(Array)
            merge_arrays ? v1 + v2 : v2
          elsif v2.is_a?(NilClass)
            v1
          else
            v2
          end
        end
      first.merge(second, &merger)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pups-1.2.1 lib/pups/merge_command.rb
pups-1.2.0 lib/pups/merge_command.rb