<commit_before># frozen_string_literal: true

module CfnDsl
  # Handles all external parameters
  class ExternalParameters
    extend Forwardable

    def_delegators :parameters, :fetch, :keys, :values, :each_pair

    attr_reader :parameters

    class << self
      def defaults(params = {})
        @defaults ||= {}
        @defaults.merge! params
        @defaults
      end

      def current
        @current || refresh!
      end

      def refresh!
        @current = new
      end
    end

    def initialize
      @parameters = self.class.defaults.clone
    end

    def set_param(key, val)
      parameters[key.to_sym] = val
    end

    def merge_param(xray)
      parameters.deep_merge!(xray)
    end

    def get_param(key)
      parameters[key.to_sym]
    end
    alias [] get_param

    def to_h
      parameters
    end

    def add_to_binding(bind, logstream)
      parameters.each_pair do |key, val|
        # rubocop:disable Style/SafeNavigation
        logstream.puts("Setting local variable #{key} to #{val}") if logstream
        # rubocop:enable Style/SafeNavigation
        if bind.eval "defined? #{key}"
          bind.eval "#{key}.merge(#{val.inspect})"
        else
          bind.eval "#{key} = #{val.inspect}"
        end
      end
    end

    def load_file(fname)
      format = File.extname fname
      case format
      when /ya?ml/
        params = YAML.load_file fname
      when /json/
        params = JSON.parse File.read(fname)
      else
        raise "Unrecognized extension #{format}"<commit_msg>improve test scenario for a basic merge in binding of multiple yaml files. Fixes #422 (#423)<commit_after># frozen_string_literal: true

module CfnDsl
  # Handles all external parameters
  class ExternalParameters
    extend Forwardable

    def_delegators :parameters, :fetch, :keys, :values, :each_pair

    attr_reader :parameters

    class << self
      def defaults(params = {})
        @defaults ||= {}
        @defaults.merge! params
        @defaults
      end

      def current
        @current || refresh!
      end

      def refresh!
        @current = new
      end
    end

    def initialize
      @parameters = self.class.defaults.clone
    end

    def set_param(key, val)
      parameters[key.to_sym] = val
    end

    def merge_param(xray)
      parameters.deep_merge!(xray)
    end

    def get_param(key)
      parameters[key.to_sym]
    end
    alias [] get_param

    def to_h
      parameters
    end

    def add_to_binding(bind, logstream)
      parameters.each_pair do |key, val|
        # rubocop:disable Style/SafeNavigation
        logstream.puts("Setting local variable #{key} to #{val}") if logstream
        # rubocop:enable Style/SafeNavigation
        if defined?(key) && val.is_a?(Hash)
          bind.eval "#{key}.merge(#{val.inspect})"
        else
          bind.eval "#{key} = #{val.inspect}"
        end
      end
    end

    def load_file(fname)
      format = File.extname fname
      case format
      when /ya?ml/
        params = YAML.load_file fname
      when /json/
        params = JSON.parse File.read(fname)
      else
        raise "Unrecognized extension #{format}"
{'fraction_non_alphanumeric': 0.0913978494623656, 'fraction_numerical': 0.0018975332068311196, 'mean_word_length': 2.14726368159204, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6254245', 'n_tokens_mistral': 971, 'n_tokens_neox': 937, 'n_words': 253}