<commit_before># frozen_string_literal: true

require "active_support/core_ext/hash/indifferent_access"

module Anyway
  module Rails
    # Enhance config to be more Railsy-like:
    # – accept hashes with indeferent access
    # - load data from secrets
    # - recognize Rails env when loading from YML
    module Config
      module ClassMethods
        # Make defaults to be a Hash with indifferent access
        def defaults
          return @defaults if instance_variable_defined?(:@defaults)

          @defaults = super.with_indifferent_access
        end
      end

      def load_from_sources(config = {})
        config = config.with_indifferent_access
        load_from_file(config)
        load_from_secrets(config)
        load_from_credentials(config)
        load_from_env(config)
      end

      def load_from_file(config)
        config_path = resolve_config_path
        config.deep_merge!(load_from_yml(config_path)[::Rails.env] || {})

        if Anyway::Settings.use_local_files
          local_config_path = config_path.sub(/\.yml/, ".local.yml")
          config.deep_merge!(load_from_yml(local_config_path) || {})
        end

        config
      end

      def load_from_secrets(config)
        if ::Rails.application.respond_to?(:secrets)
          config.deep_merge!(::Rails.application.secrets.public_send(config_name) || {})
        end
        config
      end

      def load_from_credentials(config)
        if ::Rails.application.respond_to?(:credentials)
          config.deep_merge!(::Rails.application.credentials.public_send(config_name) || {})

          load_from_local_credentials(config) if Anyway::Settings.use_local_files
        end
        config
      end

      def load_from_local_credentials(config)
        local_creds_path = ::Rails.root.join("config/credentials/local.yml.enc").to_s<commit_msg>fix: ignore credentials source when running credentials:edit<commit_after># frozen_string_literal: true

require "active_support/core_ext/hash/indifferent_access"

module Anyway
  module Rails
    # Enhance config to be more Railsy-like:
    # – accept hashes with indeferent access
    # - load data from secrets
    # - recognize Rails env when loading from YML
    module Config
      module ClassMethods
        # Make defaults to be a Hash with indifferent access
        def defaults
          return @defaults if instance_variable_defined?(:@defaults)

          @defaults = super.with_indifferent_access
        end
      end

      def load_from_sources(config = {})
        config = config.with_indifferent_access
        load_from_file(config)
        load_from_secrets(config)
        load_from_credentials(config)
        load_from_env(config)
      end

      def load_from_file(config)
        config_path = resolve_config_path
        config.deep_merge!(load_from_yml(config_path)[::Rails.env] || {})

        if Anyway::Settings.use_local_files
          local_config_path = config_path.sub(/\.yml/, ".local.yml")
          config.deep_merge!(load_from_yml(local_config_path) || {})
        end

        config
      end

      def load_from_secrets(config)
        if ::Rails.application.respond_to?(:secrets)
          config.deep_merge!(::Rails.application.secrets.public_send(config_name) || {})
        end
        config
      end

      def load_from_credentials(config)
        # do not load from credentials if we're in the context
        # of the `credentials:edit` command
        return if defined?(::Rails::Command::CredentialsCommand)

        if ::Rails.application.respond_to?(:credentials)
          config.deep_merge!(::Rails.application.credentials.public_send(config_name) || {})

          load_from_local_credentials(config) if Anyway::Settings.use_local_files
        end
        config
      end

      def load_from_local_credentials(config)
        local_creds_path = ::Rails.root.join("config/credentials/local.yml.enc").to_s
{'fraction_non_alphanumeric': 0.08545918367346939, 'fraction_numerical': 0.0, 'mean_word_length': 2.8031037827352083, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2457355', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1120, 'n_words': 224}